【问题标题】:how to set timeout at http post using android-async-http:1.4.7?如何使用 android-async-http:1.4.7 在 http post 设置超时?
【发布时间】:2015-06-26 14:23:36
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    尝试添加此覆盖:

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

    【讨论】:

    • 非常感谢!我使用第一个 onFailure 解决了我的问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-21
    • 1970-01-01
    • 2018-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-27
    相关资源
    最近更新 更多