在onErrorResponse方法中处理BasicNetwork.performRequest

时间:2015-01-13 04:39:05

标签: android android-volley

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

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

2 个答案:

答案 0 :(得分:0)

  

我想根据异常

解析错误
   @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的这个课程:

/**
 * 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 :(得分:0)

发生异常时,我总是收到状态码-1。我正在以这种方式解析Volley Error并得到我预期的结果。

@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);
           }
               }