Android 在onErrorResponse方法中处理BasicNetwork.performRequest

Android 在onErrorResponse方法中处理BasicNetwork.performRequest,android,android-volley,Android,Android Volley,我正在使用安卓截击库。我得到以下错误 BasicNetwork.performRequest:意外响应代码404 我如何处理这个错误?请给我一个解决方案 代码: 私有void insertRegistration(最终字符串userId、最终字符串密码、最终字符串mobileNo){ showProgressDialog() StringRequest putRequest=新的StringRequest(Request.Method.POST,Constants.REGISTRATION\u

我正在使用安卓截击库。我得到以下错误

BasicNetwork.performRequest:意外响应代码404

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

代码:

私有void insertRegistration(最终字符串userId、最终字符串密码、最终字符串mobileNo){ showProgressDialog()

StringRequest putRequest=新的StringRequest(Request.Method.POST,Constants.REGISTRATION\u URL,
新的Response.Listener()
{
@凌驾
公共void onResponse(字符串响应){
//回应
Log.d(“响应::”,响应);
if(response!=null&&response.length()>3){
如果(SharedReferencesHelper.getUser(context).length()>5){
hideProgressDialog();
Toast.makeText(上下文,“成功注册”,Toast.LENGTH_LONG.show();
registrationActivity.this.finish();
}否则{
hideProgressDialog();
Toast.makeText(上下文,“注册失败”,Toast.LENGTH_LONG.show();
}
}
}, 
新的Response.ErrorListener()
{
@凌驾
公共无效onErrorResponse(截击错误){
hideProgressDialog();
}
}
) {
@凌驾
受保护的映射getParams()
{       
Map params=newhashmap();
参数put(“empid”,userId);
参数put(“通过”,密码);
参数put(“移动”,mobileNo);
返回参数;
}
};
//将请求添加到请求队列
Log.d(“putRequest::”,“”+putRequest);
AppController.getInstance().addToRequestQueue(putRequest,tag_reg_obj);
}
Logcat:: [1722]BasicNetwork.performRequest:的意外响应代码404


我想基于异常分析错误

再看看这节课的截击:

/**
 * 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;
}

当异常发生时,我总是获取状态代码-1。我以这种方式解析截击错误,并得到了预期的结果。

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

没有评论。为什么我得到-1。我搜索了两天来寻找我的解决方案,但失败了。没有处理基本网络404错误的具体解决方案是的。我知道URL无效。我想测试和处理该URL无效,我想用“OneErrorResponse”来处理。因此,我如何处理不同类型的错误。我不明白您想要什么,是否要显示从服务器返回的错误消息?我想根据exceptionstatusCode=Error.networkResponse.statusCode;statusCode ia始终为0来分析错误。但NetworkError的错误实例是一种解决方案。我解决了这样的问题那样。
/**
 * 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);
         } 
@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);
           }
               }