Android Volley volleyError.networkResponse - null

I am using Volley to call a web service So ... I am making this call:

POST /api/user/login HTTP/1.1 Content-Type: application/json User-Agent: Dalvik/1.6.0 (Linux; U; Android 4.2.2; Samsung Galaxy S3 - 4.2.2 - API 17 - 720x1280 Build/JDQ39E) Host: /*No need to show this here.*/ Connection: Keep-Alive Accept-Encoding: gzip Content-Length: 43 {"password":"sg","email":" string1@str.com "} 

And I get this answer (note the 401 error):

 HTTP/1.1 401 Unauthorized Server: nginx/1.6.3 Date: Thu, 06 Aug 2015 17:39:15 GMT Content-Type: application/json; charset=utf-8 Transfer-Encoding: chunked Connection: keep-alive X-Powered-By: PHP/5.5.27 {"error":"User is not activated"} 

My request object is as follows:

 public class CustomJsonObjectRequest extends JsonRequest<JSONObject> { private Class responseType = null; public CustomJsonObjectRequest(int method, String url, JSONObject jsonRequest, Response.Listener<org.json.JSONObject> listener, Response.ErrorListener errorListener) { super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener, errorListener); } @Override protected Response<JSONObject> parseNetworkResponse(NetworkResponse resp) { return null; //For the purpose of this question, let presume it null. It doesn't even enter here, so no use considering what in here } @Override protected VolleyError parseNetworkError(VolleyError volleyError) { /** IF I SET A BREAKPOINT ANYWHERE IN HERE ... volleyError.networkResponse WILL BE NULL !!! WHY ?! You can ignore what below ... it not really important*/ JSONObject jsonResp = null; String message = "Could not connect to the server.";// default response if (volleyError != null && volleyError.networkResponse != null && volleyError.networkResponse.data != null) { try { /* get the object */ jsonResp = new JSONObject(new String(volleyError.networkResponse.data)); message = jsonResp.getString("detail"); } catch (JSONException e) { e.printStackTrace(); } UDebug.log("PARSED NETWORK ERROR: [" + new VolleyError(message) + "]"); return new VolleyError(message); } return new VolleyError(message); } } 

It introduces parseNetworkError as it should, but why is volleyError.networkResponse null in the world? volleyError.networkResponse.data should contain the line {"error":"User is not activated"} . Why is this not happening? Any ideas?

If you need any additional code or explanations, let me know ...

+7
android android-volley
source share
2 answers

So, I tested it on devices before 4.3 and it seems that it does not work, on those after 4.3. he does.

I asked the backend to change the error response from 401 to 403, and it seems to be working now. I read somewhere that the header for 401 may be incorrect or may not contain the relevant data, and it does the code co berzerk.

The exact reason for this is unknown to me. We left it as error 403 and everything is in order.

+5
source share

In this case: CHeck:

  • Is your Internet connection active or not?
  • The correct url you are trying to connect?
  • On the server side, check whether you allow the use of mobile communications or not? The configuration of web services at the web end allows you to connect only mobile devices or a web browser, or both. I think this may be your problem.
+3
source share

All Articles