Handle Volley error

后端 未结 6 1225
[愿得一人]
[愿得一人] 2020-12-07 12:57

I want to handle and show some message in onErrorResponse

below is my code.

String url = MainActivity.strHostUrl+\"api/delete_picture\";         


        
6条回答
  •  感动是毒
    2020-12-07 13:29

    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
        }
    }
    

提交回复
热议问题