我使用这些代码从服务器下载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();
任何帮助? 谢谢!
答案 0 :(得分:0)
java.net.UnknownHostException: Host is unresolved:
当您连接到有效的wifi但路由器未接收互联网时,也会引发此异常。很容易重现这个: 1)连接到有效的wifi 2)现在在插入路由器时从路由器上拔下电缆
你会发现这个错误!!