I'm having a problem with Retrofit.
When I make a call to my REST API, every time the response.body()
is not successfull and null
I tried to make the same call with Postman and the body contains the correct answer.
This is my api Interface:
public interface Api{
//User Login
@FormUrlEncoded
@POST("standard")
Call<ResponseBody> standard(
@Field("latitude") String latitude,
@Field("longitude") String longitude
);
}
My request:
public void updateListView(String latitude, String longitude) {
Call<ResponseBody> call = RetrofitClient
.getInstance()
.getApi()
.standard(
latitude,
longitude
);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if(response.isSuccessful()) {
//the response-body is already parseable to your ResponseBody object
ResponseBody responseBody = (ResponseBody) response.body();
//you can do whatever with the response body now...
String responseBodyString= null;
try {
responseBodyString = responseBody.string();
} catch (IOException e) {
e.printStackTrace();
}
Log.d("Response body", responseBodyString);
}
else {
Log.d("Response errorBody", String.valueOf(response.errorBody()));
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.e("onFalure", t.getMessage());
Toast.makeText(TestRequest.this, t.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
Error Code:
D/Response errorBody: okhttp3.ResponseBody$1@7ea6319
My successfull postman request: