Android:AndroidHttpClient - 如何设置超时?

时间:2011-04-20 12:15:47

标签: java android http networking timeout

我已按照kuester2000's answer的说明操作,但我的超时设置似乎无效。

try
{
    int timeout = 3000;
    URL myURL = //some valid URL

    AndroidHttpClient = AndroidHttpClient.newInstance("name");
    HttpGet httpGet = new HttpGet(myURL.toExternalForm());

    HttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, timeout);
    HttpConnectionParams.setSoTimeout(httpParams, timeout);

    HttpResponse response = httpClient.execute(httpGet);

    //...
}
catch (SocketTimeoutException e)
{
    e.printStackTrace();
}
catch (ConnectTimeoutException e)
{
    e.printStackTrace();
}
catch (IOException e)
{
    e.printStackTrace();
}
//...

但是,超时值不会改变任何内容。

在我联系的答案中,它还说:

  

连接超时抛出“java.net.SocketTimeoutException:Socket未连接”和套接字超时“java.net.SocketTimeoutException:操作超时”。

但我没有得到。相反,我得到“org.apache.http.conn.ConnectTimeoutException:连接到...超时”

所以有人可以帮助我吗?错误在哪里?

5 个答案:

答案 0 :(得分:9)

您不使用httpParams参数,必须将它们提供给HTTPClient。所以它不会像这样工作。在您链接的anwer中,订单是正确的!请尝试以下顺序:首先创建Params并将它们提供给HTTPClient。

HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
HttpConnectionParams.setSoTimeout(httpParameters, 10000);

HttpClient client = new DefaultHttpClient(httpParameters);
HttpGet request = new HttpGet(url);

HttpResponse response = client.execute(request);

答案 1 :(得分:6)

错过了将params附加到我的http请求中,但在我的示例中执行此操作的正确方法是

httpGet.setParams(httpParams);

在调用httpClient.execute(httpGet)之前。

刚刚添加该行并且工作正常。

答案 2 :(得分:5)

在客户端上设置的另一个选项:

AndroidHttpClient client = AndroidHttpClient.newInstance("Client V/1.0");
HttpConnectionParams.setConnectionTimeout(this.client.getParams(), 3000);
HttpConnectionParams.setSoTimeout(this.client.getParams(), 5000);

这应该导致设置那些特定的参数......

HTH

答案 3 :(得分:0)

            try {
                HttpGet httpGet = new HttpGet("your_uri/test.json");

                HttpParams httpParameters = new BasicHttpParams();
                // Set the timeout in milliseconds until a connection is established.
                // The default value is zero, that means the timeout is not used.
                int timeoutConnection = 5000;
                HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
                // Set the default socket timeout (SO_TIMEOUT)
                // in milliseconds which is the timeout for waiting for data.
                int timeoutSocket = 10000;
                HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

                HttpClient httpclient = new DefaultHttpClient();

                httpGet.setParams(httpParameters);

                HttpResponse response = httpclient.execute(httpGet);

                HttpEntity entity = response.getEntity();

                BufferedReader br = null;

                if(entity != null) {
                    Log.i("read", "nube");

                    br = new BufferedReader(new InputStreamReader(entity.getContent()));

                } else {
                    Log.i("read", "local");

                    AssetManager am = getApplicationContext().getAssets();
                    InputStream is = am.open("test.json");

                    br = new BufferedReader(new InputStreamReader(is));
                }

                String line;
                String texto = "";

                while ((line = br.readLine()) != null) {
                    texto += line;
                }


            } catch (IOException e) {
                e.printStackTrace();
            }

答案 4 :(得分:0)

在阅读完之后,这里是我如何直接使用默认客户端的params:

HttpClient client = new DefaultHttpClient();
HttpParams params = client.getParams();
HttpConnectionParams.setConnectionTimeout(params, 3000);
HttpConnectionParams.setSoTimeout(params, 3000);

原始信用转到http://www.jayway.com/2009/03/17/configuring-timeout-with-apache-httpclient-40/