获取java.net.SocketTimeoutException:连接在android中超时

我对android开发比较陌生。 我正在开发一个Android应用程序,我发送请求到Web服务器和parsingJSON对象。 经常我得到java.net.SocketTimeoutException: Connection timed out与服务器通信java.net.SocketTimeoutException: Connection timed outexception。 有些时候它可以完美的工作,没有任何问题。 我知道这个问题在很多时候都被问过了。 但是,我仍然没有得到任何令人满意的解决这个问题。 我在下面发布我的logcat和app-server通信代码。

 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 

任何人都可以帮我找出解决办法吗? 提前致谢….

我search了整个networking和阅读了很多关于连接超时exception的文档后,我明白的是,防止SocketTimeoutException超出了我们的限制…有效地处理它的一种方法是定义连接超时,然后处理它通过使用try catch块…希望这将有助于任何人在未来面临同样的问题。

 HttpUrlConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(7000); //set the timeout in milliseconds 

我知道这个问题有点老了。 但是因为我在研究的时候偶然发现了这个东西,所以我觉得有一点补充可能会有所帮助。

如上所述,错误不能由客户端解决,因为这是一个networking相关的问题。 但是,你可以做的是重试连接几次。 这可能是一个解决方法,直到真正的问题得到解决。

 for (int retries = 0; retries < 3; retries++) { try { final HttpClient client = createHttpClientWithDefaultSocketFactory(null, null); final HttpResponse response = client.execute(get); final int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != 200) { throw new IllegalStateException("GET Request on '" + get.getURI().toString() + "' resulted in " + statusCode); } else { return response.getEntity(); } } catch (final java.net.SocketTimeoutException e) { // connection timed out...let's try again } } 

也许这有助于某人。

如果您在本地主机上testing服务器,则您的Android设备必须连接到同一本地networking。 然后,您的APP使用的服务器URL必须包含您的计算机IP地址,而不是“本地主机”掩码。

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