在 onErrorResponse 方法中处理 BasicNetwork.performRequest

Handle BasicNetwork.performRequest in onErrorResponse method

我正在使用 android volley 库。我收到以下错误。

BasicNetwork.performRequest: 意外的响应代码 404

我该如何处理这个错误?请给我解决方案。

代码:

private void insertRegistration(final String userId,final String password,final String mobileNo) { showProgressDialog();

    StringRequest putRequest = new StringRequest(Request.Method.POST, Constants.REGISTRATION_URL, 
        new Response.Listener<String>() 
        {
            @Override
            public void onResponse(String response) {
                // response
                Log.d("Response:::::::", response);          

                    if(response != null && response.length() > 3){
                        if(SharedPreferencesHelper.getUser(context).length() > 5){  

                         hideProgressDialog();
                         Toast.makeText(context, "Successfully Registered.", Toast.LENGTH_LONG).show(); 
                         RegistratonActivity.this.finish();                          
                    }else{

                         hideProgressDialog();
                         Toast.makeText(context, " Registration Failed.", Toast.LENGTH_LONG).show();                            
                    }
            }
        }, 
        new Response.ErrorListener() 
        {
             @Override
             public void onErrorResponse(VolleyError error) {               

                 hideProgressDialog();

           }
        }
    ) {

        @Override
        protected Map<String, String> getParams() 
        {       
                Map<String, String>  params = new HashMap<String, String> ();  
                params.put("empid", userId);  
                params.put("pass", password);   
                params.put("mobile",mobileNo);  
                return params;  
        }

    };

    // Adding request to request queue
    Log.d("putRequest::", ""+putRequest);
    AppController.getInstance().addToRequestQueue(putRequest,tag_reg_obj);

}

Logcat:: [1722] BasicNetwork.performRequest:

的意外响应代码 404

I want to parse the error based on exception

   @Override
   public void onErrorResponse(VolleyError error) {  

        NetworkResponse response;
        int  statusCode = -1;
        if( error.networkResponse != null){
            NetworkResponse response = error.networkResponse;
            statusCode = error.networkResponse.statusCode;
        }



        if( error instanceof NetworkError) {
              // do your work here
        } else if( error instanceof ClientError) {
              // do your work here
        } else if( error instanceof ServerError) {
              // do your work here
        } else if( error instanceof AuthFailureError) {
              // do your work here
        } else if( error instanceof ParseError) {
             // do your work here
        } else if( error instanceof NoConnectionError) {
            // do your work here
        } else if( error instanceof TimeoutError) {
            // do your work here
        }
   }

还要看看 Volley 中的 class:

/**
 * Data and headers returned from {@link Network#performRequest(Request)}.
 */
public class NetworkResponse {
    /**
     * Creates a new network response.
     * @param statusCode the HTTP status code
     * @param data Response body
     * @param headers Headers returned with this response, or null for none
     * @param notModified True if the server returned a 304 and the data was already in cache
     */
    public NetworkResponse(int statusCode, byte[] data, Map<String, String> headers,
            boolean notModified) {
        this.statusCode = statusCode;
        this.data = data;
        this.headers = headers;
        this.notModified = notModified;
    }

    public NetworkResponse(byte[] data) {
        this(HttpStatus.SC_OK, data, Collections.<String, String>emptyMap(), false);
    }

    public NetworkResponse(byte[] data, Map<String, String> headers) {
        this(HttpStatus.SC_OK, data, headers, false);
    }

    /** The HTTP status code. */
    public final int statusCode;

    /** Raw data from this response. */
    public final byte[] data;

    /** Response headers. */
    public final Map<String, String> headers;

    /** True if the server returned a 304 (Not Modified). */
    public final boolean notModified;
}

例如你可以这样做:

         String message = "";
         if(response.statusCode == 404){
               message = new String(response.data);
         } 

发生异常时我总是得到状态码-1。我以这种方式解析 VolleyError 并得到了我预期的结果。

@Override
        public void onErrorResponse(VolleyError error) {                    
              Log.d("Error.Response",  error.getMessage());  



           if (error instanceof NoConnectionError) {
                         Log.d("NoConnectionError>>>>>>>>>", "NoConnectionError.......");
                            ErrorMessage.logErrorMessage(getString(R.string.connection_error_msg), context);
           } else if (error instanceof AuthFailureError) {
                             Log.d("AuthFailureError>>>>>>>>>", "AuthFailureError.......");
                             ErrorMessage.logErrorMessage(getString(R.string.authFailure_error_msg), context);
            } else if (error instanceof ServerError) {
                             Log.d("ServerError>>>>>>>>>", "ServerError.......");
                             ErrorMessage.logErrorMessage(getString(R.string.server_connection_error_msg), context);
            } else if (error instanceof NetworkError) {
                             Log.d("NetworkError>>>>>>>>>", "NetworkError.......");
                             ErrorMessage.logErrorMessage(getString(R.string.network_error_msg), context);
                        } else if (error instanceof ParseError) {
                             Log.d("ParseError>>>>>>>>>", "ParseError.......");
                             ErrorMessage.logErrorMessage(getString(R.string.parse_error_msg), context);
           }else if (error instanceof TimeoutError) {
                             Log.d("TimeoutError>>>>>>>>>", "TimeoutError.......");
                             ErrorMessage.logErrorMessage(getString(R.string.timeout_error_msg), context);
           }
               }