Android:如何将ConnectionTimeOut值设置为HttpClient

时间:2012-09-28 07:30:29

标签: android httpclient

我将HttpParameters设置为HttpClient。

现在我想测试它们是否设置正确。实际上我是这样做的:

public class MyHttpClient extends DefaultHttpClient {

    public MyHttpClient(int tc, int ts) {

        HttpParams httpParameters = new BasicHttpParams();
        // Set the timeout in milliseconds until a connection is established.
        int timeoutConnection = tc;
        HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
        // Set the default socket timeout (SO_TIMEOUT) 
        // in milliseconds which is the timeout for waiting for data.
        int timeoutSocket = ts;
        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
        ClientConnectionManager mgr = this.getConnectionManager();
        this.setParams(httpParameters);
    }
}

并像这样创建一个实例:

public static MyHttpClient httpclient = new MyHttpClient(5000, 5000);

如何知道这是否真的有效。

我试过了:

httpclient.getParams()的toString();

我把它作为输出:

org.apache.http.params.BasicHttpParams@40520f20

似乎是Default Params而不是我设置的那个。

2 个答案:

答案 0 :(得分:1)

使用这些代码行

HttpParams httpParameters = new BasicHttpParams();
    int timeoutConnection = 120000;
            HttpConnectionParams.getConnectionTimeout(httpParameters));

答案 1 :(得分:1)

我用过:

HttpParams params = new BasicHttpParams();
params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, CONNECTION_TIMEOUT);
params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, CONNECTION_TIMEOUT);
params.setLongParameter(ConnManagerPNames.TIMEOUT, CONNECTION_TIMEOUT);
HttpClient httpClient = new DefaultHttpClient(params);