如何使用 android-async-http:1.4.7 在 http post 设置超时?
how to set timeout at http post using android-async-http:1.4.7?
我正在尝试使用 android-async-http:1.4.7 在 http post 设置超时来管理服务器,但设置 httpClient.setTimeout(1000) 它不会无法正常工作并且 onFailure 回调未捕获异常。
这是我的代码
AsyncHttpClient httpClient = new AsyncHttpClient();
RequestParams requestParams = new RequestParams();
String s= sharedPrefs.getString("type_nn", "VGG");
requestParams.put("network", s);
if (destination== null) {
destination = new File(path);
}
requestParams.put("file", destination);
httpClient.setTimeout(1000);
httpClient.post("http://url", requestParams, new JsonHttpResponseHandler() {
@Override
public void onFailure(int statusCode, Header[] headers, java.lang.Throwable throwable, org.json.JSONArray response) {
Toast.makeText(activity, "Error", Toast.LENGTH_SHORT).show();
progressChargePhoto.setVisibility(View.GONE);
}
@Override
public void onSuccess(int statusCode, Header[] headers, org.json.JSONArray response) {
...
}
}
尝试添加此覆盖:
@Override
public void onFailure(int statusCode, Header[] headers, java.lang.Throwable throwable, org.json.JSONObject response) {
Toast.makeText(getBaseContext(), "Error", Toast.LENGTH_SHORT).show();
....
}
还有一个你可以覆盖,但我认为你不需要它,但以防万一:
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
super.onFailure(statusCode, headers, responseString, throwable);
....
}
注意你的成功,也许你需要在签名中添加带有 JSONObject 的那个。
我正在尝试使用 android-async-http:1.4.7 在 http post 设置超时来管理服务器,但设置 httpClient.setTimeout(1000) 它不会无法正常工作并且 onFailure 回调未捕获异常。 这是我的代码
AsyncHttpClient httpClient = new AsyncHttpClient();
RequestParams requestParams = new RequestParams();
String s= sharedPrefs.getString("type_nn", "VGG");
requestParams.put("network", s);
if (destination== null) {
destination = new File(path);
}
requestParams.put("file", destination);
httpClient.setTimeout(1000);
httpClient.post("http://url", requestParams, new JsonHttpResponseHandler() {
@Override
public void onFailure(int statusCode, Header[] headers, java.lang.Throwable throwable, org.json.JSONArray response) {
Toast.makeText(activity, "Error", Toast.LENGTH_SHORT).show();
progressChargePhoto.setVisibility(View.GONE);
}
@Override
public void onSuccess(int statusCode, Header[] headers, org.json.JSONArray response) {
...
}
}
尝试添加此覆盖:
@Override
public void onFailure(int statusCode, Header[] headers, java.lang.Throwable throwable, org.json.JSONObject response) {
Toast.makeText(getBaseContext(), "Error", Toast.LENGTH_SHORT).show();
....
}
还有一个你可以覆盖,但我认为你不需要它,但以防万一:
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
super.onFailure(statusCode, headers, responseString, throwable);
....
}
注意你的成功,也许你需要在签名中添加带有 JSONObject 的那个。