下载文件时出现问题

时间:2010-04-19 10:33:05

标签: android

这是我面临的问题:我尝试使用以下snipet代码从tomcat服务器下载文件

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
}

当我在模拟器上运行它时,上面的工作正常,但是当我尝试在我的设备上测试时它没有做任何事情,并且~50秒后它会在行<抛出“套接字超时”异常< / p>

 BufferedInputStream getit = new BufferedInputStream(new URL("http://192.168.2.180:8080/android.apk").openStream());

这是怎么回事?我该如何解决?

这是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: WARN/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)

2 个答案:

答案 0 :(得分:1)

问题来自端口8080,尝试使用默认网址http://www.google.com。 (默认端口= 80)它可以工作。

答案 1 :(得分:0)

正如Samuh所说,检查您是否可以访问私人IP。 此外,您应该正确捕获超时异常,显示用户或Toast的对话框。

请记住,如果您愿意在手机中使用3g申请,我建议您使用以下内容增加超时值:

setConnectTimeout(CONNECT_TIMEOUT);
setReadTimeout(READ_TIMEOUT);