Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle Volley error

I want to handle and show some message in onErrorResponse

below is my code.

String url = MainActivity.strHostUrl+"api/delete_picture";  jobjDeleteImage = new JsonObjectRequest(Request.Method.POST, url, jobj, new Response.Listener<JSONObject>() {      @Override     public void onResponse(JSONObject response) {         Log.e("Image response", response.toString());       } },  new Response.ErrorListener() {      @Override     public void onErrorResponse(VolleyError error) {          Log.e("Volly Error", error.toString());          NetworkResponse networkResponse = error.networkResponse;         if (networkResponse != null) {             Log.e("Status code", String.valueOf(networkResponse.statusCode));         }     } }); 

I want to handle com.android.volley.TimeoutError and also some other error code like 404, 503 etc and Toast message here.

like image 281
Girish Bhutiya Avatar asked Jul 11 '14 14:07

Girish Bhutiya


People also ask

How to handle Volley error in Android?

Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { Log. e("Image response", response. toString()); } }, new Response. ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.

How do I get volley error code?

you have to override parseNetworkError and deliverError methods and you can get errormessage from them. Show activity on this post.

What is volley client error?

Indicates that the server responded with an error response indicating that the client has erred. For backwards compatibility, extends ServerError which used to be thrown for all server errors, including 4xx error codes indicating a client error.

What is the volley method?

Volley is a networking library managed by the RequestQueue and mainly used for smaller Networking purposes in Android. To use it, first you need to instantiate the RequestQueue and later on you can start or stop request, add or cancel request and access the response cache(s). RequestQueue queue = Volley.


2 Answers

The networkResponse is null because in a TimeoutError no data is received from the server -- hence the timeout. Instead, you need generic client side strings to display when one of these events occur. You can check for the VolleyError's type using instanceof to differentiate between error types since you have no network response to work with -- for example:

@Override public void onErrorResponse(VolleyError error) {      if (error instanceof TimeoutError || error instanceof NoConnectionError) {         Toast.makeText(context,                 context.getString(R.string.error_network_timeout),                 Toast.LENGTH_LONG).show();     } else if (error instanceof AuthFailureError) {         //TODO     } else if (error instanceof ServerError) {        //TODO     } else if (error instanceof NetworkError) {       //TODO     } else if (error instanceof ParseError) {        //TODO     } } 
like image 159
Submersed Avatar answered Sep 28 '22 00:09

Submersed


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();             }         } 
like image 33
Naimatullah Avatar answered Sep 28 '22 00:09

Naimatullah