When I am making GET request to retrofit it gives me an error 400 - Bad Request.
This error is thrown when request length is greater than 1024.
It works fine when request length is less than 1024. I don't know how to fix this issue.
When I am making GET request to retrofit it gives me an error 400 - Bad Request.
This error is thrown when request length is greater than 1024.
It works fine when request length is less than 1024. I don't know how to fix this issue.
You can do it like in your onResponse method, remember 400 is a response status not an error:
if (response.code() == 400) {
Log.v("Error code 400",response.errorBody().string());
}
And you can handle any response code except 200-300 with Gson like :
if (response.code() == 400) {
Gson gson = new GsonBuilder().create();
ErrorPojoClass mError=new ErrorPojoClass();
try {
mError= gson.fromJson(response.errorBody().string(),ErrorPojoClass .class);
Toast.makeText(getApplicationContext(), mError.getErrorDescription(),
Toast.LENGTH_LONG).show();
} catch (IOException e) {
// handle failure to read error
}
}
Add this to your build.gradle : compile 'com.google.code.gson:gson:2.7'
If you want to create Pojo class go to JSON Schema 2 Pojo and paste your example Json response. Select source type Json and annotation Gson.
Solution : It was not an issue of retrofit. The problem was inside Api(WCF) which I was using. In web config, the value of maxQueryStringLength was set to 1024. I got the exact issue when I sent the request using Postman.