Hia
Hia

Reputation: 103

HttpURLConnection error: java.net.SocketTimeoutException: Connection timed out

I am using simple urlconnection like this:

       url = URL+"getClient&uid="+cl_id;
        URL url = new URL(this.url);
        Log.d("Set++","get_t URL: "+ url);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

its working fine, but sometimes i get this error:

       error: java.net.SocketTimeoutException: Connection timed out

what could be the reason? I have only 4 clients... so i dont think that the server is overloaded with connections.

the code:

    try {
        URL = Settings.BASE_URL + "_interface.php?" +
                "key=" + Settings.KEY +"&app_naam="+Settings.APP_NAAM+ "&action=check&setTime="+c;
        URL url = new URL(URL);
        if(D)Log.e("ChekTreadAanvr+url", URL);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        BufferedReader rd = new BufferedReader(
                new InputStreamReader(conn.getInputStream()));
        String response;
        if ((response = rd.readLine()) != null) {
            rd.close();
        }
        if(D) Log.d("WebSaveThread+","DATARESIEVED:  "+response);
        return response;
            } catch (MalformedURLException e) {
        if(D)Log.d("ERR","server chekc failure ++ ");
        return success = false;
    } catch (IOException ioex) {
        if(D)Log.e("ERR", "error: " + ioex.getMessage(), ioex);
        success = false;
    }
    return Boolean.toString(success);

Upvotes: 4

Views: 10496

Answers (2)

Tad
Tad

Reputation: 4784

If you are connecting to a particularly slow server, there are two timeouts that you can set: setConnectTimeout() and setReadTimeout(). The names are fairly self explanatory. For example:

            httpURLConnection.setConnectTimeout(60*1000);
            httpURLConnection.setReadTimeout(60*1000);

Upvotes: 2

Lucifer
Lucifer

Reputation: 29632

It may be possible that your Internet Connection Speed is weak. You can increase your Timeout Interval by using following method.

httpURLConnection.setConnectTimeout( 6 * 10 * 1000 ); // One Minute

Upvotes: 4

Related Questions