Android 即使在 AsyncHttpClient 超时设置后应用程序超时
Android Application Timeout even after AsyncHttpClient timeout set
即使设置了超时,我的应用也会超时。请看下面:
final int DEFAULT_TIMEOUT = 200000 * 1000000000;
// Make RESTful webservice call using AsyncHttpClient object
AsyncHttpClient client = new AsyncHttpClient();
client.setTimeout(DEFAULT_TIMEOUT);
//client.get("http://182.188.33.27:9009/useraccount/login/dologin",params ,new AsyncHttpResponseHandler() {
client.get("http://koha.cdtl.com.sg/cgi-bin/koha/ilsdi.pl?service=AuthenticatePatron",params ,new AsyncHttpResponseHandler() {
// When the response returned by REST has Http response code '200'
@Override
public void onSuccess(String response) {
// Hide Progress Dialog
prgDialog.hide();
try {
// JSON Object
JSONObject obj = new JSONObject(response);
// When the JSON response has status boolean value assigned with true
if(obj.getBoolean("status")){
Toast.makeText(getApplicationContext(), "You are successfully logged in!", Toast.LENGTH_LONG).show();
// Navigate to Home screen
navigatetoHomeActivity();
}
// Else display error message
else{
errorMsg.setText(obj.getString("error_msg"));
Toast.makeText(getApplicationContext(), obj.getString("error_msg"), Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
// TODO Auto-generated catch block
Toast.makeText(getApplicationContext(), "Error Occured [Server's JSON response might be invalid]!", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
// When the response returned by REST has Http response code other than '200'
@Override
public void onFailure(int statusCode, Throwable error,
String content) {
// Hide Progress Dialog
prgDialog.hide();
// When Http response code is '404'
if(statusCode == 404){
Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
}
// When Http response code is '500'
else if(statusCode == 500){
Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
}
// When Http response code other than 404, 500
else{
Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet or remote server is not up and running]", Toast.LENGTH_LONG).show();
}
}
});
很奇怪,有时会显示想要的结果:
Toast.makeText(getApplicationContext(), "You are successfully logged in!", Toast.LENGTH_LONG).show();
但主要是:
Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet or remote server is not up and running]", Toast.LENGTH_LONG).show();
我也尝试过清理和重建项目,但没有帮助。
欢迎提出任何建议。
谢谢
最后,设法找出所有不同的错误场景只是 error.getMessage()、error.toString() 和 error.getCause()
的组合
Toast.makeText(getApplicationContext(), "Status code :"+statusCode +"errmsg : "+error.getMessage(), Toast.LENGTH_LONG).show();
当前显示的是自定义消息,首先查看是哪个错误状态码和错误内容,状态码很多你只处理两种情况
即使设置了超时,我的应用也会超时。请看下面:
final int DEFAULT_TIMEOUT = 200000 * 1000000000;
// Make RESTful webservice call using AsyncHttpClient object
AsyncHttpClient client = new AsyncHttpClient();
client.setTimeout(DEFAULT_TIMEOUT);
//client.get("http://182.188.33.27:9009/useraccount/login/dologin",params ,new AsyncHttpResponseHandler() {
client.get("http://koha.cdtl.com.sg/cgi-bin/koha/ilsdi.pl?service=AuthenticatePatron",params ,new AsyncHttpResponseHandler() {
// When the response returned by REST has Http response code '200'
@Override
public void onSuccess(String response) {
// Hide Progress Dialog
prgDialog.hide();
try {
// JSON Object
JSONObject obj = new JSONObject(response);
// When the JSON response has status boolean value assigned with true
if(obj.getBoolean("status")){
Toast.makeText(getApplicationContext(), "You are successfully logged in!", Toast.LENGTH_LONG).show();
// Navigate to Home screen
navigatetoHomeActivity();
}
// Else display error message
else{
errorMsg.setText(obj.getString("error_msg"));
Toast.makeText(getApplicationContext(), obj.getString("error_msg"), Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
// TODO Auto-generated catch block
Toast.makeText(getApplicationContext(), "Error Occured [Server's JSON response might be invalid]!", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
// When the response returned by REST has Http response code other than '200'
@Override
public void onFailure(int statusCode, Throwable error,
String content) {
// Hide Progress Dialog
prgDialog.hide();
// When Http response code is '404'
if(statusCode == 404){
Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
}
// When Http response code is '500'
else if(statusCode == 500){
Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
}
// When Http response code other than 404, 500
else{
Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet or remote server is not up and running]", Toast.LENGTH_LONG).show();
}
}
});
很奇怪,有时会显示想要的结果:
Toast.makeText(getApplicationContext(), "You are successfully logged in!", Toast.LENGTH_LONG).show();
但主要是:
Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet or remote server is not up and running]", Toast.LENGTH_LONG).show();
我也尝试过清理和重建项目,但没有帮助。
欢迎提出任何建议。
谢谢
最后,设法找出所有不同的错误场景只是 error.getMessage()、error.toString() 和 error.getCause()
的组合Toast.makeText(getApplicationContext(), "Status code :"+statusCode +"errmsg : "+error.getMessage(), Toast.LENGTH_LONG).show(); 当前显示的是自定义消息,首先查看是哪个错误状态码和错误内容,状态码很多你只处理两种情况