简体   繁体   中英

HttpResponse & Timeout for Android

I want my device to give up the http connexion after 5 seconds. But my code does not work... I never get any timeout message when shutting network down. Just like if the device still tries to connect, despite de timeout...

Have an idea? Am I trying to catch the right exception?

Thanks.

        try 
        {
            HttpClient httpclient = new DefaultHttpClient();       
                HttpPost httppost = new HttpPost(URL);
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

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

                HttpResponse response = httpclient.execute(httppost);

                if (response.getStatusLine().getStatusCode() < 400) 
                {
                        ... //data processing
                } 
                else 
                {
                    errorMsgId = R.string.http_site_error;                        
                }
        } 
        catch (ConnectTimeoutException e)
        {
            Toast.makeText(this, "Network timeout reached!", Toast.LENGTH_SHORT).show();
            Log.e("+++++++++++++++++ ","Network timeout reached!"); 
        }

ok, GOT IT, so in case this could help someone else:

                    HttpClient httpclient = new DefaultHttpClient();
                    final HttpParams httpParams = httpclient.getParams();
                    HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
                    HttpConnectionParams.setSoTimeout(httpParams, 5000);
                    HttpPost httppost = new HttpPost(URL);
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
                    HttpResponse response = httpclient.execute(httppost);

You can use something like this:

/**
 * Check availability of web service
 * 
 * @param host Address of host
 * @param seconds Timeout in seconds
 * @return Availability of host
 */
public static boolean checkIfURLExists(String host, int seconds)
{
    HttpURLConnection httpUrlConn;
    try
    {
        httpUrlConn = (HttpURLConnection) new URL(host).openConnection();

        // Set timeouts in milliseconds
        httpUrlConn.setConnectTimeout(seconds * 1000);
        httpUrlConn.setReadTimeout(seconds * 1000);

        // Print HTTP status code/message for your information.
        System.out.println("Response Code: " + httpUrlConn.getResponseCode());
        System.out.println("Response Message: "
                + httpUrlConn.getResponseMessage());

        return (httpUrlConn.getResponseCode() == HttpURLConnection.HTTP_OK);
    }
    catch (Exception e)
    {
        System.out.println("Error: " + e.getMessage());
        return false;
    }
}

Perhaps I'm missing something, but where are you associating the parameters that you set the timeout in with the HttpClient that you have created? Shouldn't you do something like this:

HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
HttpConnectionParams.setSoTimeout(httpParameters, 5000);
...
HttpClient httpclient = new DefaultHttpClient(httpParameters);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM