Here's the problem I'm facing : I try to download a file from a tomcat server using the following snipet of code
try
{
BufferedInputStream getit = new BufferedInputStream(new URL("http://192.168.2.180:8080/android.apk").openStream());
FileOutputStream saveit = new FileOutputStream(path);
BufferedOutputStream bout = new BufferedOutputStream(saveit,1024);
byte data[] = new byte[1024];
int readed = getit.read(data,0,1024);
while(readed != -1)
{
bout.write(data,0,readed);
readed = getit.read(data,0,1024);
}
bout.close();
getit.close();
saveit.close();
}
catch(Exception e)
{
e.printStackTrace
}
The above works just fine when I run it on the emulator , but when I try to test it on my device it doesn't do anything , and ~50 sec later it throws a "socket timeout"exception at the line
BufferedInputStream getit = new BufferedInputStream(new URL("http://192.168.2.180:8080/android.apk").openStream());
What is this happening and how can I fix it ?
here's the stacktrace
04-19 12:28:19.865: WARN/System.err(2962): java.net.SocketException: The operation timed out
04-19 12:28:19.865: WARN/System.err(2962): at org.apache.harmony.luni.platform.OSNetworkSystem.connectSocketImpl(Native Method)
04-19 12:28:19.865: WARN/System.err(2962): at org.apache.harmony.luni.platform.OSNetworkSystem.connect(OSNetworkSystem.java:125)
04-19 12:28:19.865: W开发者_运维问答ARN/System.err(2962): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:227)
04-19 12:28:19.865: WARN/System.err(2962): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:521)
04-19 12:28:19.865: WARN/System.err(2962): at java.net.Socket.connect(Socket.java:1019)
04-19 12:28:19.865: WARN/System.err(2962): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:67)
04-19 12:28:19.865: WARN/System.err(2962): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionManager$ConnectionPool.getHttpConnection(HttpConnectionManager.java:151)
04-19 12:28:19.865: WARN/System.err(2962): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionManager.getConnection(HttpConnectionManager.java:73)
04-19 12:28:19.865: WARN/System.err(2962): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getHTTPConnection(HttpURLConnection.java:826)
04-19 12:28:19.865: WARN/System.err(2962): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:812)
04-19 12:28:19.865: WARN/System.err(2962): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1054)
04-19 12:28:19.865: WARN/System.err(2962): at java.net.URL.openStream(URL.java:674)
The problem comes from the port 8080, try with a default url like http://www.google.com. (default port = 80) It will work.
As Samuh said, check that you have access to the private ip. Also you should catch that timeout exception correctly, showing a dialog to the user or a Toast.
Remember that if you are willing to use 3g in your phone for you application, I would recommend increasing the timeout values using something like:
setConnectTimeout(CONNECT_TIMEOUT);
setReadTimeout(READ_TIMEOUT);
精彩评论