0

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!"); 
        }

3 Answers 3

3

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);
Sign up to request clarification or add additional context in comments.

Comments

1

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;
    }
}

1 Comment

great yep... but if possible i would better include a timeout within my HttpResponse connexion order instead of making a connexion twice, because i am pretty sure my boss will notice it and won't like it :(
0

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);

1 Comment

that's was also what i was thinking about as I found those exemples, but it seems to be that way...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.