Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting java.net.SocketTimeoutException: Connection timed out in android

I'm relatively new to android development. I'm developing an android application where I'm sending requests to the web server and parsing JSON objects. Frequently I'm getting java.net.SocketTimeoutException: Connection timed out exception while communicating with the server. Some times it will work perfectly without any problem. I know this same question has been asked in SO many times. But still, I didn't get any satisfying solution to this problem. I'm posting my logcat and app-server communication code below.

public JSONObject RequestWithHttpUrlConn(String _url, String param){      HttpURLConnection con = null;     URL url;     String response = "";     Scanner inStream = null;     PrintWriter out = null;     try {         url = new URL(_url);         con = (HttpURLConnection) url.openConnection();         con.setDoOutput(true);         con.setRequestMethod("POST");         if(param != null){             con.setFixedLengthStreamingMode(param.getBytes().length);         }         con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");         out = new PrintWriter(con.getOutputStream());         if(param != null){             out.print(param);         }         out.flush();         out.close();         inStream = new Scanner(con.getInputStream());          while(inStream.hasNextLine()){             response+=(inStream.nextLine());         }     } catch (MalformedURLException e) {         // TODO Auto-generated catch block         e.printStackTrace();     } catch (IOException e) {         // TODO Auto-generated catch block         e.printStackTrace();     } finally{         if(con != null){             con.disconnect();         }if(inStream != null){             inStream.close();         }if(out != null){             out.flush();             out.close();         }     } } 

Logcat:

03-25 10:55:32.613: W/System.err(18868): java.net.SocketTimeoutException: Connection  timed out 03-25 10:55:32.617: W/System.err(18868):at org.apache.harmony.luni.platform.OSNetworkSystem.connect(Native Method) 03-25 10:55:32.617: W/System.err(18868):at dalvik.system.BlockGuard $WrappedNetworkSystem.connect(BlockGuard.java:357) 03-25 10:55:32.617: W/System.err(18868):at  org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:204) 03-25 10:55:32.617: W/System.err(18868):at  org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:437) 03-25 10:55:32.617: W/System.err(18868):at        java.net.Socket.connect(Socket.java:1002) 03-25 10:55:32.621: W/System.err(18868):at  org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init> (HttpConnection.java:75) 03-25 10:55:32.621: W/System.err(18868): at  org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init> (HttpConnection.java:48)03-25 10:55:32.624: W/System.err(18868):at  org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect (HttpConnection.java:322)03-25 10:55:32.624: W/System.err(18868):at  org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get (HttpConnectionPool.java:89)03-25 10:55:32.628: W/System.err(18868):at  org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpCon nection(HttpURLConnectionImpl.java:285) 03-25 10:55:32.628: W/System.err(18868):at     org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConn ection(HttpURLConnectionImpl.java:267) 03-25 10:55:32.636: W/System.err(18868):at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect (HttpURLConnectionImpl.java:205) 03-25 10:55:32.636: W/System.err(18868):at  org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getOutputS tream(HttpURLConnectionImpl.java:614) 03-25 10:55:32.636: W/System.err(18868):at  com.myapp.core.JSONRequest.RequestWithHttpUrlConn(JSONRequest.java:63) 03-25 10:55:32.636: W/System.err(18868):    at com.myapp.core.DetailPage $AsyncRecBooks.doInBackground(AKBookDetailView.java:265) 03-25 10:55:32.640: W/System.err(18868):    at com.myapp.core.DetailPage $AsyncRecBooks.doInBackground(AKBookDetailView.java:1) 03-25 10:55:32.640: W/System.err(18868):    at android.os.AsyncTask$2.call (AsyncTask.java:185) 03-25 10:55:32.640: W/System.err(18868):    at java.util.concurrent.FutureTask $Sync.innerRun(FutureTask.java:306) 03-25 10:55:32.640: W/System.err(18868):    at java.util.concurrent.FutureTask.run (FutureTask.java:138) 03-25 10:55:32.640: W/System.err(18868):    at  java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) 03-25 10:55:32.648: W/System.err(18868):    at  java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) 03-25 10:55:32.648: W/System.err(18868):    at java.lang.Thread.run(Thread.java:1019) 03-25 10:55:32.652: E/JSON Parser(18868): Error parsing data org.json.JSONException:   End of input at character 0 of  

Can anyone help me out to find out a solution for this? Thanks in Advance....

like image 206
akh Avatar asked Mar 25 '13 05:03

akh


People also ask

How do I resolve Java net SocketTimeoutException connect timed out?

Using try/catch/finally If you are a developer, so you can surround the socket connection part of your code in a try/catch/finally and handle the error in the catch. You might try connecting a second time, or try connecting to another possible socket, or simply exit the program cleanly.

Why do we get socket timeout exception?

Socket timeouts can occur when attempting to connect to a remote server, or during communication, especially long-lived ones. They can be caused by any connectivity problem on the network, such as: A network partition preventing the two machines from communicating. The remote machine crashing.

What is Java net SocketException?

The java. net. SocketException: Connection reset error usually comes when one of the parties in TCP connection like client or server is trying to read/write data, but other parties abruptly close the connection like it was crashed, stopped or terminated.


1 Answers

I've searched all over the web and after reading lot of docs regarding connection timeout exception, the thing I understood is that, preventing SocketTimeoutException is beyond our limit. One way to effectively handle it is to define a connection timeout and later handle it by using a try-catch block. Hope this will help anyone in future who are facing the same issue.

HttpUrlConnection conn = (HttpURLConnection) url.openConnection();  //set the timeout in milliseconds conn.setConnectTimeout(7000); 
like image 61
akh Avatar answered Sep 30 '22 20:09

akh