1

我使用这些代码从服务器下载 mp3 文件:

URL u = new URL(link);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
FileOutputStream f = new FileOutputStream(new File(root,"Video.mp4"));


InputStream in = c.getInputStream();

byte[] buffer = new byte[1024];
int len1 = 0;
while ( (len1 = in.read(buffer)) > 0 ) {
     f.write(buffer,0,len1);
}
f.close();

它通过移动数据(3G)按预期工作,但是当我尝试通过 wifi 下载时,LogCat 显示:

06-08 22:36:49.892: W/System.err(347): java.net.UnknownHostException: Host is unresolved: dl2.mp3.zdn.vn:80
06-08 22:36:49.902: W/System.err(347):     at java.net.Socket.connect(Socket.java:1038)
06-08 22:36:49.902: W/System.err(347):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:62)
06-08 22:36:49.902: W/System.err(347):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:88)
06-08 22:36:49.902: W/System.err(347):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHTTPConnection(HttpURLConnectionImpl.java:927)
06-08 22:36:49.902: W/System.err(347):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:909)
06-08 22:36:49.902: W/System.err(347):     at binh.book.Book$1.run(Book.java:60)
06-08 22:36:49.902: W/System.err(347):     at java.lang.Thread.run(Thread.java:1096)  
06-07 21:34:59.306: W/System.err(9686):     at binh.pronunciation.DownloadFiles$1$1.run(DownloadFiles.java:62)

在这段代码中:

c.connect();

有什么帮助吗?感谢!

4

1 回答 1

0
java.net.UnknownHostException: Host is unresolved:

当您连接到有效的 wifi 但路由器没有接收到互联网时,也会引发此异常。很容易重现这一点:1)连接到有效的wifi 2)现在在路由器插入时从路由器上拔下电缆

你会看到这个错误!!

于 2012-09-19T21:27:42.367 回答