How to set HttpResponse timeout for Android in Java

后端 未结 10 2094
刺人心
刺人心 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:15

    In my example, two timeouts are set. The connection timeout throws java.net.SocketTimeoutException: Socket is not connected and the socket timeout java.net.SocketTimeoutException: The operation timed out.

    HttpGet httpGet = new HttpGet(url);
    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 = 3000;
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
    // Set the default socket timeout (SO_TIMEOUT) 
    // in milliseconds which is the timeout for waiting for data.
    int timeoutSocket = 5000;
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
    
    DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
    HttpResponse response = httpClient.execute(httpGet);
    

    If you want to set the Parameters of any existing HTTPClient (e.g. DefaultHttpClient or AndroidHttpClient) you can use the function setParams().

    httpClient.setParams(httpParameters);
    
    0 讨论(0)
  • 2020-11-22 08:15

    If you're using the default http client, here's how to do it using the default http params:

    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-11-22 08:16

    If you are using the HttpURLConnection, call setConnectTimeout() as described here:

    URL url = new URL(myurl);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setConnectTimeout(CONNECT_TIMEOUT);
    
    0 讨论(0)
  • 2020-11-22 08:21

    To set settings on the client:

    AndroidHttpClient client = AndroidHttpClient.newInstance("Awesome User Agent V/1.0");
    HttpConnectionParams.setConnectionTimeout(client.getParams(), 3000);
    HttpConnectionParams.setSoTimeout(client.getParams(), 5000);
    

    I've used this successfully on JellyBean, but should also work for older platforms ....

    HTH

    0 讨论(0)
  • 2020-11-22 08:24
    public boolean isInternetWorking(){
        try {
            int timeOut = 5000;
            Socket socket = new Socket();
            SocketAddress socketAddress = new InetSocketAddress("8.8.8.8",53);
            socket.connect(socketAddress,timeOut);
            socket.close();
            return true;
        } catch (IOException e) {
            //silent
        }
        return false;
    }
    
    0 讨论(0)
  • 2020-11-22 08:29

    you can creat HttpClient instance by the way with Httpclient-android-4.3.5,it can work well.

     SSLContext sslContext = SSLContexts.createSystemDefault();
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
                    sslContext,
                    SSLConnectionSocketFactory.STRICT_HOSTNAME_VERIFIER);
                    RequestConfig.Builder requestConfigBuilder = RequestConfig.custom().setCircularRedirectsAllowed(false).setConnectionRequestTimeout(30*1000).setConnectTimeout(30 * 1000).setMaxRedirects(10).setSocketTimeout(60 * 1000);
            CloseableHttpClient hc = HttpClients.custom().setSSLSocketFactory(sslsf).setDefaultRequestConfig(requestConfigBuilder.build()).build();
    
    0 讨论(0)
提交回复
热议问题