1

I'm doing a simple GET request using Volley library in Android. Lately I've discovered that in Android 6 (Marshmallow) I always get to the onErrorResponse callback, there is no error message but the error code is 400. All my other nearly identical requests work perfectly in Android 6. Only this one is the problematic. When I paste the request URL into a browser, it works. So the problem is not from server-side.

I also tried adding headers, but that didn't help.

BVtp
  • 2,308
  • 2
  • 29
  • 68

3 Answers3

1

This problem came to me also, and not the content-type was the problem. For me also on Android 7 and up the URL what I used was working correctly, on browser also, but when checked it on Android 6 it threw me the same problem what you wrote. It's because probably your URL contains spaces.

Resolution:

String url = "http://your_url.com/file.php?data=" + sendJson;
url = url.replaceAll(" ", "%20");

And the problem is gone!

L_J
  • 2,351
  • 10
  • 23
  • 28
1

I also use Volley and had the same issue.
The problem is that on Android 6.X Marshmallow the url parameters are not being encoded in UTF-8 on GET requests. Dunno why...

So if you send a GET request like

http://example.com/api?key=my value

or

http://example.com/api?place=café

it will be sent as follows:

On Android 6.X

http://example.com/api?key=my value
http://example.com/api?place=café

(returns: 400 - HTTP_BAD_REQUEST)


Other versions

http://example.com/api?key=my+value
http://example.com/api?place=caf%C3%A9

(if your serve is working properly, returns: 200 - HTTP_OK)


So, my workaround was call

URLEncoder.encode(value, "UTF-8");

for every single parameter.

Giovanne
  • 152
  • 9
0

Replace empty character with "%20" from your URL. String url = "https://zxasd/asd"; url = url.replaceAll(" " , "%20");