Android volley: how to handle the error response

volley error response
com android-volley servererror
com.android.volley.volleyerror network disconnected
volley client error
volley android
volley callback android example
error com android volley noconnectionerror
android volley get request example

I implemented an android app with volley library to connect my database. The connection with 'get' and 'post' request it works, but the servers response not working. This happens because the Error Response catch automatically the errors (for example my server response with code 201 for the login successful, but volley untersand it like error).

I override the parseNetworkError from the class Request:

 @Override
    protected VolleyError parseNetworkError(VolleyError volleyError) {
        String parsed;
        NetworkResponse networkResponse = volleyError.networkResponse;
        if(networkResponse != null && networkResponse.data != null) {
            try {
                parsed = new String(networkResponse.data, HttpHeaderParser.parseCharset(networkResponse.headers));
            } catch (UnsupportedEncodingException var4) {
                parsed = new String(networkResponse.data);
            }
            NetworkResponse response = new NetworkResponse(networkResponse.data);
            Response<String> parsedResponse;
            switch(response.statusCode){
                case 204:                        
                    ...
                case 401:
                    ...
                default:
                    return volleyError;
            }
        }

        return super.parseNetworkError(volleyError);
    }

The problem is the VolleyError. This class extends Exception and not contains the information (code error).

How can i solve this?


you can handle like this

@Override
public void onErrorResponse(VolleyError error) {
    // Handle your error types accordingly.For Timeout & No connection error, you can show 'retry' button.
    // For AuthFailure, you can re login with user credentials.
    // In this case you can check how client is forming the api and debug accordingly.
    // For ServerError 5xx, you can do retry or handle accordingly.
    if( error instanceof NetworkError) {
    //handle your network error here.
    } else if( error instanceof ServerError) {
    //handle if server error occurs with 5** status code
    } else if( error instanceof AuthFailureError) {
    //handle if authFailure occurs.This is generally because of invalid credentials
    } else if( error instanceof ParseError) {
    //handle if the volley is unable to parse the response data.
    } else if( error instanceof NoConnectionError) {
    //handle if no connection is occurred
    } else if( error instanceof TimeoutError) {
    //handle if socket time out is occurred.
    }

}

Android Volley example with Error Handling, Complete example of implementing error handling in Volley Network request for Android.Works well with both GET and POST request. Name *. Email *. Website. Save my name, email, and website in this browser for the next time I comment. Notify me of follow-up comments by email. Notify me of new posts by email.


This is what I am using in my projects:

    @Override
    public void onErrorResponse(VolleyError error) {
        if(error instanceof NoConnectionError){
            ConnectivityManager cm = (ConnectivityManager)mContext
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetwork = null;
            if (cm != null) {
                activeNetwork = cm.getActiveNetworkInfo();
            }
            if(activeNetwork != null && activeNetwork.isConnectedOrConnecting()){
                Toast.makeText(getActivity(), "Server is not connected to internet.",
                        Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getActivity(), "Your device is not connected to internet.",
                        Toast.LENGTH_SHORT).show();
            }
        } else if (error instanceof NetworkError || error.getCause() instanceof ConnectException 
                || (error.getCause().getMessage() != null 
                && error.getCause().getMessage().contains("connection"))){
            Toast.makeText(getActivity(), "Your device is not connected to internet.", 
                    Toast.LENGTH_SHORT).show();
        } else if (error.getCause() instanceof MalformedURLException){
            Toast.makeText(getActivity(), "Bad Request.", Toast.LENGTH_SHORT).show();
        } else if (error instanceof ParseError || error.getCause() instanceof IllegalStateException
                || error.getCause() instanceof JSONException
                || error.getCause() instanceof XmlPullParserException){
            Toast.makeText(getActivity(), "Parse Error (because of invalid json or xml).", 
                    Toast.LENGTH_SHORT).show();
        } else if (error.getCause() instanceof OutOfMemoryError){
            Toast.makeText(getActivity(), "Out Of Memory Error.", Toast.LENGTH_SHORT).show();
        }else if (error instanceof AuthFailureError){
            Toast.makeText(getActivity(), "server couldn't find the authenticated request.", 
                    Toast.LENGTH_SHORT).show();
        } else if (error instanceof ServerError || error.getCause() instanceof ServerError) {
            Toast.makeText(getActivity(), "Server is not responding.", Toast.LENGTH_SHORT).show();
        }else if (error instanceof TimeoutError || error.getCause() instanceof SocketTimeoutException
                || error.getCause() instanceof ConnectTimeoutException 
                || error.getCause() instanceof SocketException
                || (error.getCause().getMessage() != null 
                && error.getCause().getMessage().contains("Connection timed out"))) {
            Toast.makeText(getActivity(), "Connection timeout error", 
                    Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(getActivity(), "An unknown error occurred.", 
                    Toast.LENGTH_SHORT).show();
        }
    }

Android Volley example with Error Handling – Android Clarified, Complete example of implementing error handling in Volley Network request for Android.Works well with both GET and POST request. In volley you have separate success and error listeners, handle all the errors in the error listener block. Display a alert or so in which you can show a proper message for user, so that he gets notified that there was an error and he needs to try again.


        public void onErrorResponse(VolleyError error) {
            String message=null;
            if(error instanceof NetworkError)
            {
                message = "Cannot connect to Internet...Please check your connection!";
                Toast.makeText(ScanQrCodeActivity.this, message, Toast.LENGTH_SHORT).show();
            }
            else if(error instanceof ServerError)
            {
                message = "The server could not be found. Please try again after some time!!";
                Toast.makeText(ScanQrCodeActivity.this, message, Toast.LENGTH_SHORT).show();
            }
            else if (error instanceof ParseError) {
                message = "Parsing error! Please try again after some time!!";
                Toast.makeText(ScanQrCodeActivity.this, message, Toast.LENGTH_SHORT).show();
            }

            swipeRefreshLayout.setRefreshing(false);
            HideSwipeDialog();

        }

Best Way To Show the Error in Volley Response But Don't put the direct msg to end user be creative and show msg something like this "Wait For The Moment We Connect To Server"

252 Android Volley Error Handling |, This page provides Java code examples for com.android.volley.error. Listener<​String>() { @Override public void onResponse(String response) //Find location //Handle response Log.w("Json", response); JsonParser parser = new  Overview. Volley is a library that makes networking for Android apps easier and most importantly, faster. Volley Library was announced by Ficus Kirkpatrick at Google I/O '13.


Java Code Examples com.android.volley.error.VolleyError, Handles the server error, tries to determine whether to show a stock message or to * show a message retrieved from the server. * * @param err Volley error  Return response data from function “onResponse” in Volley Library Volley East, Fast Networking for Android Android volley is a networking library, which was introduced to make networking calls easier, faster without writing a lot of codes.


Java Code Examples com.android.volley.VolleyError.getMessage, @Override public void postError(Request<?> request, VolleyError error) { request​.addMarker("post-error"); Response<?> response = Response.error(error);  The get() method of ImageLoader has onResponse() method to handle the response. The return type of the method is an ImageContainer object, for demo purpose we are simply showing the images in AlertDialog. Volley has additional method to handle the cache. We will describe these methods briefly.


com.android.volley.Response.error java code examples, Android: How handle message error from the server using Volley? //In your extended request class @Override protected VolleyError  This lesson describes how to use the common request types that Volley supports: StringRequest. Specify a URL and receive a raw string in response. See Setting Up a Request Queue for an example. JsonObjectRequest and JsonArrayRequest (both subclasses of JsonRequest). Specify a URL and get a JSON object or array (respectively) in response.