Android: AndroidHttpClient - how to set timeout?

后端 未结 5 991
闹比i
闹比i 2020-12-11 04:38

I have followed the instructions of kuester2000\'s answer, but my timeout settings don\'t seem to work.

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


        
相关标签:
5条回答
  • 2020-12-11 04:59

    The other option to set on the client itself:

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

    This should result in those particular params being set...

    HTH

    0 讨论(0)
  • 2020-12-11 05:00

    After reading around, here's how I did it using the params straight from the default client:

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

    Original credit goes to http://www.jayway.com/2009/03/17/configuring-timeout-with-apache-httpclient-40/

    0 讨论(0)
  • 2020-12-11 05:02

    You do not use the httpParams params, they must be provided to the HTTPClient. So it won't work like this. In the anwer you linked, the order is correct! Try the following order: Create the Params first and supply them to the 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);
    
    0 讨论(0)
  • I did miss to attach the params to my http request, but the proper way to do this in my example is

    httpGet.setParams(httpParams);
    

    before calling httpClient.execute(httpGet).

    Just added that line and it worked fine.

    0 讨论(0)
  • 2020-12-11 05:10
                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();
                }
    
    0 讨论(0)
提交回复
热议问题