我使用Android的http客户端库,可在此处找到:http://loopj.com/android-async-http/。 问题是当我想处理互联网连接错误等故障时。我无法从处理程序方法中获取错误消息:
AsyncHttpClient client = new AsyncHttpClient();
client.get(..., params, new JsonHttpResponseHandler() {
@Override
public void onSuccess(JSONObject response) {
}
@Override
public void onFailure(Throwable e) {
Toast.makeText(getApplicationContext(), e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
}
为什么e.getLocalizedMessage()或e.getMessage()为空?
答案 0 :(得分:1)
这应该有所帮助:
@Override
public void onFailure ( Throwable e, JSONObject errorResponse ) {
String msg = "Object *" + e.toString() + "*" + errorResponse.toString();
new AlertDialog.Builder(MyActivity.this).setMessage(msg).setCancelable(false).setTitle("Fehler").setPositiveButton(R.string.ok, null).create().show();
}
@Override
public void onFailure ( Throwable e, JSONArray errorResponse ) {
String msg = "Array *" + e.toString() + "*" + errorResponse.toString();
new AlertDialog.Builder(MyActivity.this).setMessage(msg).setCancelable(false).setTitle("Fehler").setPositiveButton(R.string.ok, null).create().show();
}
@Override
public void onFailure ( Throwable e, String errorResponse ) {
String msg = "String *" + e.toString() + "*" + errorResponse;
new AlertDialog.Builder(MyActivity.this).setMessage(msg).setCancelable(false).setTitle("Fehler").setPositiveButton(R.string.ok, null).create().show();
}