Http Status Code in Android Volley when error.networkResponse is null

前端 未结 8 1143
忘了有多久
忘了有多久 2020-11-29 00:44

I am using Google Volley on the Android platform. I am having a problem in which the error parameter in onErrorResponse is returning a null n

8条回答
  •  南方客
    南方客 (楼主)
    2020-11-29 00:52

    This is how I check and grep error.

                    // TimeoutError => most likely server is down or network is down.
                    Log.e(TAG, "TimeoutError: " + (e instanceof TimeoutError));
    
                    Log.e(TAG, "NoConnectionError: " + (e instanceof NoConnectionError));
                    /*if(error.getCause() instanceof UnknownHostException ||
                        error.getCause() instanceof EOFException ) {
                        errorMsg = resources.getString(R.string.net_error_connect_network);
                    } else {
                        if(error.getCause().toString().contains("Network is unreachable")) {
                            errorMsg = resources.getString(R.string.net_error_no_network);
                        } else {
                            errorMsg = resources.getString(R.string.net_error_connect_network);
                        }
                    }*/
    
                    Log.e(TAG, "NetworkError: " + (e instanceof NetworkError));
                    Log.e(TAG, "AuthFailureError: " + (e instanceof AuthFailureError));
                    Log.e(TAG, "ServerError: " + (e instanceof ServerError));
                    //error.networkResponse.statusCode
    
                    // inform dev
                    Log.e(TAG, "ParseError: " + (e instanceof ParseError));
                    //error.getCause() instanceof JsonSyntaxException
    
                    Log.e(TAG, "NullPointerException: " + (e.getCause() instanceof NullPointerException));
    
    
                    if (e.networkResponse != null) {
                        // 401 => login again
                        Log.e(TAG, String.valueOf(e.networkResponse.statusCode));
    
                        if (e.networkResponse.data != null) {
                            // most likely JSONString
                            Log.e(TAG, new String(e.networkResponse.data, StandardCharsets.UTF_8));
    
                            Toast.makeText(getApplicationContext(),
                                    new String(e.networkResponse.data, StandardCharsets.UTF_8),
                                    Toast.LENGTH_LONG).show();
                        }
                    }
                    else if (e.getMessage() == null) {
                        Log.e(TAG, "e.getMessage");
                        Log.e(TAG, "" + e.getMessage());
    
                        if (e.getMessage() != null && e.getMessage() != "")
                            Toast.makeText(getApplicationContext(),
                                    e.getMessage(), Toast.LENGTH_LONG).show();
                        else
                            Toast.makeText(getApplicationContext(),
                                    "could not reach server", Toast.LENGTH_LONG).show();
                    }
                    else if (e.getCause() != null) {
                        Log.e(TAG, "e.getCause");
                        Log.e(TAG, "" + e.getCause().getMessage());
    
                        if (e.getCause().getMessage() != null && e.getCause().getMessage() != "")
                            Toast.makeText(getApplicationContext(),
                                    e.getCause().getMessage(), Toast.LENGTH_LONG).show();
                        else
                            Toast.makeText(getApplicationContext(),
                                    "could not reach server", Toast.LENGTH_LONG).show();
                    }
    

提交回复
热议问题