我用的是安卓截取图书馆。我得到了以下错误。
BasicNetwork.performRequest:意外响应代码404
如何处理错误?请给我解决办法。
代码:
私有空insertRegistration(最终字符串userId,最终字符串密码,最终字符串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
发布于 2015-01-25 03:24:53
总是在异常发生时获取状态代码-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);
}
}发布于 2015-01-13 05:34:10
我想基于异常来解析错误。
@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
}
}还请看一下这门课的截击:
/**
* 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);
} https://stackoverflow.com/questions/27914972
复制相似问题