HttpConnectionParams.setConnectionTimeout混乱

时间:2013-03-18 07:50:17

标签: android http connection-timeout

我得到HttpConnectionParams用于设置http请求的超时,但我不知道如何正确使用它。每个请求都有setParams(HttpParams p) - 我认为在每个请求上设置http参数有点多余。我宁愿做的是以全局方式配置AndroidHttpClient - 以便每个请求共享相同的HttpParams配置。

这个HttpConnectionParams.setConnectionTimeout(params, 1500);是否在全球范围内运作?

1 个答案:

答案 0 :(得分:2)

HttpConnectionParams是要使用的类:

示例代码(未完成的代码: - ))

HttpClient httpClient = new DefaultHttpClient();
final HttpParams httpParams = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
HttpConnectionParams.setSoTimeout(httpParams, 5000);

所以你必须这样做(没有深入检查你的代码):

    public class Client {    

        public static void sendRequest(final HttpUriRequest request, 
                                       int RequestType,
                                       ResponseListener callback) {
        HttpClient httpClient = new DefaultHttpClient();
        final HttpParams httpParams = httpClient.getParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
        HttpConnectionParams.setSoTimeout(httpParams, 5000);
        (new AsynchronousSender(httpClient, request, RequestType, 
                            new Handler(), new CallbackWrapper(callback))).start();

}

修改

setSoTimeout()本质上是一回事,但它不仅仅等待初始连接。因此,您将检查每个数据包以查看它是否超时,而setConnectionTimeout仅在初始连接时超时。