下載文件如果我嘗試從網站下載圖像(使用http連接),我沒有問題,如果下載在我的android服務中執行,而如果我嘗試從同一個方法下載文件活動出現此錯誤:錯誤從活動
11-08 13:27:36.800: W/System.err(8010): android.os.NetworkOnMainThreadException
11-08 13:27:36.800: W/System.err(8010): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1077)
11-08 13:27:36.800: W/System.err(8010): at java.net.InetAddress.lookupHostByName(InetAddress.java:477)
11-08 13:27:36.800: W/System.err(8010): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:277)
11-08 13:27:36.800: W/System.err(8010): at java.net.InetAddress.getAllByName(InetAddress.java:249)
11-08 13:27:36.800: W/System.err(8010): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:69)
11-08 13:27:36.800: W/System.err(8010): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
11-08 13:27:36.800: W/System.err(8010): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:304)
11-08 13:27:36.800: W/System.err(8010): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
11-08 13:27:36.800: W/System.err(8010): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:292)
11-08 13:27:36.800: W/System.err(8010): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:274)
11-08 13:27:36.800: W/System.err(8010): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1038)
11-08 13:27:36.800: W/System.err(8010): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHeaderField(HttpURLConnectionImpl.java:481)
11-08 13:27:36.800: W/System.err(8010): at java.net.URLConnection.getHeaderFieldInt(URLConnection.java:572)
11-08 13:27:36.800: W/System.err(8010): at java.net.URLConnection.getContentLength(URLConnection.java:335)
我解決調用問題,從活動,這樣我的下載方法:
new Thread(new Runnable() {
public void run() {
downloadFile(imagePath);
}
}).start();
謝謝。我使用線程 – GVillani82
解決了問題現在我只有一個問題:我希望我的主線程等待下載,然後繼續執行。我能做什麼? – GVillani82