Android. Volley. volleyError.networkResponse is null
Asked Answered
B

3

8

I'm using Volley to make calls to a webservice So ... I'm 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":"[email protected]"}

And I'm getting this reponse (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 looks like this:

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's presume it's null. It doesn't even enter here, so no use considering what's 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's below ... it's 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 enters parseNetworkError as it should, but why in the world is volleyError.networkResponse null ? volleyError.networkResponse.data should contain the string {"error":"User is not activated"}. Why is this no happening? Any ideas?

If you need some additional code or clarifications, let me know ...

Bushbuck answered 6/8, 2015 at 18:3 Comment(2)
This appears to be a permission issue. The device with 4.2.2, is it a virtual device or not? If it is a real device, then post the manifest, if its virtual, "destroy" it, then create again. I had issues where programs would not behave as expected in virtual devices. Finally, check if you have this issue as wellBifocals
this is at least related: code.google.com/p/android/issues/detail?id=143649Tipster
B
6

So I've tested it on devices before 4.3 and it seems it doesn't work, on the ones after 4.3. it does.

I've asked the backend guy to change the error response from 401 to 403 and it seems it works now. I've read somewhere that the header for 401 might be wrong or might not contain the appropriate data and it makes the code co berzerk.

The exact reasonn for this is unknown to me. We've left it as a 403 error and everything's fine.

Bushbuck answered 13/8, 2015 at 13:18 Comment(0)
N
3

In this Case: CHeck the following

  1. Internet Connection is active or not?
  2. The url you are trying to connect is correct or not?
  3. At server side, Check whether you are allowing for Mobile connectivity or not? There is a configuration of web services at web end to allow connections only from mobile or from web browser or both. I think this might be your problem.
Nigritude answered 6/8, 2015 at 18:29 Comment(3)
Other calls work. So if the call returns a 200 code, then everything is fine. It goes where it should and it's ok.Bushbuck
@Bushbuck Try to check all the responses(I mean the format of response. In Case of JSON try to validate the response from codebeautify.org/jsonviewer) that you are receiving at device end from server end.Nigritude
So here's the thing. I've tested it on a device running 5.0 and it works fine. On 4.2.2. it doesn't. OH COME ON !!!!Bushbuck
H
0

At times, null responses can be caused by what you have inside your getHeaders() method.

@Override
    public Map<String, String> getHeaders() {
        HashMap<String, String> headers = new HashMap<>();
            //headers.put("Content-Type", "application/json");
            //headers.put(VolleyUtils.KEY_API_KEY, "...............");
            
            return headers;
}

The headers should properly correspond with what is set in the back-end.

Hydraulics answered 18/7, 2021 at 15:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.