4

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);
4
  • Does the URL timeout when you request it from your web browser? Commented Feb 1, 2012 at 10:31
  • never got timeout from browser Commented Feb 1, 2012 at 10:34
  • confused with your code, please update full code. in you updated code I can not see HttpUrlConnection line. Commented Feb 1, 2012 at 11:00
  • shoet.. one moment.. wrong paste Commented Feb 1, 2012 at 11:03

2 Answers 2

4

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

11 Comments

already tried this one... but same result. some times the error pops out. now i have 1 router between server and telephone. so the weak connection should not be the reason..
can you tell me the value of your url variable ?
this is one of the calls" 192.168.15.103/…
so you are using WiFi in your device or what ?
yes, telephone is on wifi and server also. Its on VirtualBox on my pc
|
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.