我使用这些代码从服务器下载 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();
有什么帮助吗?感谢!