如何使用android-async-http:1.4.7在http post设置超时?

时间:2015-06-26 14:23:37

标签: android timeout http-post android-async-http

我正在尝试使用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) {
                ...
            }
         }

1 个答案:

答案 0 :(得分:3)

尝试添加此覆盖:

@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的那个。