Volley Not Parsing 404 response

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 contains jason { code: resourceNotFound, msg:message_index }

Is there anyway to get Volley to process the JSon in a 404 message? The service I am integrating with returns a 404 when a resource cannot be found.

  • How to create animated background gradient like Instagram app?
  • access (faster polling) accelerometer via NativeActivity NDK
  • Android - Get the visible area of a view?
  • How to get elements(findViewById) for a layout which is dynamically loaded(setView) in a dialog?
  • Custom SeekBar thumb showing unwanted background
  • onInterceptTouchEvent only gets ACTION_DOWN
  • Least invasive way to uniquely identify Android user
  • Ideal way to set global uncaught exception Handler in Android
  • Converting string to MySQL timestamp format in php
  • How to limit the height of Spinner drop down view in Android
  • Using Kotlin in a library module without using it in the app module
  • ImageView ScaleType ignores padding
  • 2 Solutions collect form web for “Volley Not Parsing 404 response”

    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<String>() {
            @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){
    
                }
            }
        }
        );
    

    I am posting this since, I put my one day on this and don’t want anyone to struggle the same.

    ANS: If someone want to add the Body/Headers to the POST call then add the code below

    @Override
                    public HashMap<String, String> getHeaders() throws AuthFailureError {
                        HashMap<String, String> params = new HashMap<>();
                        params.put("apikey", Constants.apiKey);
                        params.put("env", Constants.enviroment);
                        params.put("Content-Type", "application/json");
                        return params;
                    }
    
                    @Override
                    public byte[] getBody() throws AuthFailureError {
                        return gson.toJson(user).getBytes();
                    }
    

    Where user is the Object you can pass to the Body.

    Happy Coding 🙂

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.