Volley Not Parsing 404 response

后端 未结 2 2139
旧时难觅i
旧时难觅i 2021-02-19 06:49

Volley returns an error when a 404 response is returned from the server even if that 404 response contains json based error codes. It does not parse the 404 response which cont

2条回答
  •  醉话见心
    2021-02-19 07:29

    If you get a 404 response it should get into whatever error listener you set. You get a VolleyError object in the error listener. You can get the network response from this object and then the data from the response body. It's given as a char array so you need to convert it to something else yourself.

    The snippet below is a simple request that does that, you'll need to put in your own URL though.

        StringRequest request = new StringRequest( Request.Method.GET, "yourURL", new Response.Listener() {
            @Override
            public void onResponse( String s ) {
                //Do whatever
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse( VolleyError volleyError ) {
                try {
                    String responseBody = new String( volleyError.networkResponse.data, "utf-8" );
                    JSONObject jsonObject = new JSONObject( responseBody );
                } catch ( JSONException e ) {
                    //Handle a malformed json response
                } catch (UnsupportedEncodingException error){
    
                }
            }
        }
        );
    

提交回复
热议问题