0

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.

Om Prakash Gupta
  • 111
  • 2
  • 15

2 Answers2

0

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.

Atman Bhatt
  • 1,245
  • 8
  • 14
  • Yes, I know 400 is a response status. But my problem is how to handle request length because when request length is more than 1024 then it give me "Bad Request" status. Below 1024 it works fine. – Om Prakash Gupta Aug 09 '18 at 09:57
0

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.

Om Prakash Gupta
  • 111
  • 2
  • 15