How to set HttpResponse timeout for Android in Java

后端 未结 10 2095
刺人心
刺人心 2020-11-22 08:02

I have created the following function for checking the connection status:

private void checkConnectionStatus() {
    HttpClient httpClient = new DefaultHttpC         


        
相关标签:
10条回答
  • 2020-11-22 08:34

    An option is to use the OkHttp client, from Square.

    Add the library dependency

    In the build.gradle, include this line:

    compile 'com.squareup.okhttp:okhttp:x.x.x'
    

    Where x.x.x is the desired library version.

    Set the client

    For example, if you want to set a timeout of 60 seconds, do this way:

    final OkHttpClient okHttpClient = new OkHttpClient();
    okHttpClient.setReadTimeout(60, TimeUnit.SECONDS);
    okHttpClient.setConnectTimeout(60, TimeUnit.SECONDS);
    

    ps: If your minSdkVersion is greater than 8, you can use TimeUnit.MINUTES. So, you can simply use:

    okHttpClient.setReadTimeout(1, TimeUnit.MINUTES);
    okHttpClient.setConnectTimeout(1, TimeUnit.MINUTES);
    

    For more details about the units, see TimeUnit.

    0 讨论(0)
  • 2020-11-22 08:36

    If your are using Jakarta's http client library then you can do something like:

            HttpClient client = new HttpClient();
            client.getParams().setParameter(HttpClientParams.CONNECTION_MANAGER_TIMEOUT, new Long(5000));
            client.getParams().setParameter(HttpClientParams.SO_TIMEOUT, new Integer(5000));
            GetMethod method = new GetMethod("http://www.yoururl.com");
            method.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, new Integer(5000));
            method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            int statuscode = client.executeMethod(method);
    
    0 讨论(0)
  • 2020-11-22 08:37

    For those saying that the answer of @kuester2000 does not work, please be aware that HTTP requests, first try to find the host IP with a DNS request and then makes the actual HTTP request to the server, so you may also need to set a timeout for the DNS request.

    If your code worked without the timeout for the DNS request it's because you are able to reach a DNS server or you are hitting the Android DNS cache. By the way you can clear this cache by restarting the device.

    This code extends the original answer to include a manual DNS lookup with a custom timeout:

    //Our objective
    String sURL = "http://www.google.com/";
    int DNSTimeout = 1000;
    int HTTPTimeout = 2000;
    
    //Get the IP of the Host
    URL url= null;
    try {
         url = ResolveHostIP(sURL,DNSTimeout);
    } catch (MalformedURLException e) {
        Log.d("INFO",e.getMessage());
    }
    
    if(url==null){
        //the DNS lookup timed out or failed.
    }
    
    //Build the request parameters
    HttpParams params = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(params, HTTPTimeout);
    HttpConnectionParams.setSoTimeout(params, HTTPTimeout);
    
    DefaultHttpClient client = new DefaultHttpClient(params);
    
    HttpResponse httpResponse;
    String text;
    try {
        //Execute the request (here it blocks the execution until finished or a timeout)
        httpResponse = client.execute(new HttpGet(url.toString()));
    } catch (IOException e) {
        //If you hit this probably the connection timed out
        Log.d("INFO",e.getMessage());
    }
    
    //If you get here everything went OK so check response code, body or whatever
    

    Used method:

    //Run the DNS lookup manually to be able to time it out.
    public static URL ResolveHostIP (String sURL, int timeout) throws MalformedURLException {
        URL url= new URL(sURL);
        //Resolve the host IP on a new thread
        DNSResolver dnsRes = new DNSResolver(url.getHost());
        Thread t = new Thread(dnsRes);
        t.start();
        //Join the thread for some time
        try {
            t.join(timeout);
        } catch (InterruptedException e) {
            Log.d("DEBUG", "DNS lookup interrupted");
            return null;
        }
    
        //get the IP of the host
        InetAddress inetAddr = dnsRes.get();
        if(inetAddr==null) {
            Log.d("DEBUG", "DNS timed out.");
            return null;
        }
    
        //rebuild the URL with the IP and return it
        Log.d("DEBUG", "DNS solved.");
        return new URL(url.getProtocol(),inetAddr.getHostAddress(),url.getPort(),url.getFile());
    }   
    

    This class is from this blog post. Go and check the remarks if you will use it.

    public static class DNSResolver implements Runnable {
        private String domain;
        private InetAddress inetAddr;
    
        public DNSResolver(String domain) {
            this.domain = domain;
        }
    
        public void run() {
            try {
                InetAddress addr = InetAddress.getByName(domain);
                set(addr);
            } catch (UnknownHostException e) {
            }
        }
    
        public synchronized void set(InetAddress inetAddr) {
            this.inetAddr = inetAddr;
        }
        public synchronized InetAddress get() {
            return inetAddr;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 08:38
    HttpParams httpParameters = new BasicHttpParams();
                HttpProtocolParams.setVersion(httpParameters, HttpVersion.HTTP_1_1);
                HttpProtocolParams.setContentCharset(httpParameters,
                        HTTP.DEFAULT_CONTENT_CHARSET);
                HttpProtocolParams.setUseExpectContinue(httpParameters, true);
    
                // Set the timeout in milliseconds until a connection is
                // established.
                // The default value is zero, that means the timeout is not used.
                int timeoutConnection = 35 * 1000;
                HttpConnectionParams.setConnectionTimeout(httpParameters,
                        timeoutConnection);
                // Set the default socket timeout (SO_TIMEOUT)
                // in milliseconds which is the timeout for waiting for data.
                int timeoutSocket = 30 * 1000;
                HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
    
    0 讨论(0)
提交回复
热议问题