Http Status Code in Android Volley when error.networkResponse is null

前端 未结 8 1145
忘了有多久
忘了有多久 2020-11-29 00:44

I am using Google Volley on the Android platform. I am having a problem in which the error parameter in onErrorResponse is returning a null n

8条回答
  •  孤城傲影
    2020-11-29 00:48

    401 Not Supported by Volley

    It turns out that it is impossible to guarantee that error.networkResponse is non-null without modifying Google Volley code because of a bug in Volley that throws the Exception NoConnectionError for Http Status Code 401 (HttpStatus.SC_UNAUTHORIZED) in BasicNetwork.java (134) prior to setting the value of networkResponse.

    Work-Around

    Instead of fixing the Volley code, our solution in this case was to modify the Web Service API to send Http Error Code 403 (HttpStatus.SC_FORBIDDEN) for the particular case in question.

    For this Http Status Code, the value of error.networkResponse is non-null in the Volley error handler: public void onErrorResponse(VolleyError error). And, error.networkResponse.httpStatusCode correctly returns HttpStatus.SC_FORBIDDEN.

    Other-Suggestions

    Rperryng's suggestion of extending the Request class may have provided a solution, and is a creative and excellent idea. Thank you very much for the detailed example. I found the optimal solution for our case is to use the work-around because we are fortunate enough to have control of the web services API.

    I might opt for fixing the Volley code in one location within BasicNetwork.java if I did not have access to making a simple change at the server.

提交回复
热议问题