サーバーからzipファイルをダウンロードしようとしています。そのために私は次のコードを使用しています:
String path = Environment.getExternalStorageDirectory().toString() + "/KaLePro/";
StartscreenModel sm = StartscreenModel.getInstance();
//TODO FIX: unkown error! Bring usb-cable to find out!
try {
URL url = new URL(urlString);
System.out.println("Connecting to " + url);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.connect();
int fileLength = connection.getContentLength();
File PATH = new File(path + "import/");
if (!PATH.exists()) {
PATH.mkdirs();
}
File outputFile = new File(PATH, "tmp.zip");
InputStream input = connection.getInputStream();
OutputStream output = new FileOutputStream(outputFile);
byte data[] = new byte[1024];
long total = 0;
int count;
sm.setSubMax(100);
while ((count = input.read(data)) != -1) {
total += count;
sm.setSubStep((int) (total * 100 / fileLength));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
Toast.makeText(Booklist.this, "Download fertig.", Toast.LENGTH_SHORT).show();
} catch (MalformedURLException e) {
Toast.makeText(Booklist.this, "Keine gültige URL", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(Booklist.this, "Keine Verbindung zur URL", Toast.LENGTH_SHORT).show();
}
しかし、ファイルをダウンロードしようとすると、次のLogcatエラーが発生します。
10-23 10:22:37.867: E/AndroidRuntime(21490): FATAL EXCEPTION: main
10-23 10:22:37.867: E/AndroidRuntime(21490): android.os.NetworkOnMainThreadException
10-23 10:22:37.867: E/AndroidRuntime(21490): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
10-23 10:22:37.867: E/AndroidRuntime(21490): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
10-23 10:22:37.867: E/AndroidRuntime(21490): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
10-23 10:22:37.867: E/AndroidRuntime(21490): at libcore.io.IoBridge.connect(IoBridge.java:112)
10-23 10:22:37.867: E/AndroidRuntime(21490): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
10-23 10:22:37.867: E/AndroidRuntime(21490): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
10-23 10:22:37.867: E/AndroidRuntime(21490): at java.net.Socket.connect(Socket.java:842)
10-23 10:22:37.867: E/AndroidRuntime(21490): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:76)
10-23 10:22:37.867: E/AndroidRuntime(21490): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
10-23 10:22:37.867: E/AndroidRuntime(21490): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
10-23 10:22:37.867: E/AndroidRuntime(21490): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
10-23 10:22:37.867: E/AndroidRuntime(21490): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
10-23 10:22:37.867: E/AndroidRuntime(21490): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
10-23 10:22:37.867: E/AndroidRuntime(21490): at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
10-23 10:22:37.867: E/AndroidRuntime(21490): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
10-23 10:22:37.867: E/AndroidRuntime(21490): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
10-23 10:22:37.867: E/AndroidRuntime(21490): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
10-23 10:22:37.867: E/AndroidRuntime(21490): at de.isogon.kalepro.activities.Booklist$2.onDownload(Booklist.java:194)
10-23 10:22:37.867: E/AndroidRuntime(21490): at de.isogon.kalepro.views.dialogs.DialogDownload$2.onClick(DialogDownload.java:76)
10-23 10:22:37.867: E/AndroidRuntime(21490): at android.view.View.performClick(View.java:4211)
10-23 10:22:37.867: E/AndroidRuntime(21490): at android.view.View$PerformClick.run(View.java:17267)
10-23 10:22:37.867: E/AndroidRuntime(21490): at android.os.Handler.handleCallback(Handler.java:615)
10-23 10:22:37.867: E/AndroidRuntime(21490): at android.os.Handler.dispatchMessage(Handler.java:92)
10-23 10:22:37.867: E/AndroidRuntime(21490): at android.os.Looper.loop(Looper.java:137)
10-23 10:22:37.867: E/AndroidRuntime(21490): at android.app.ActivityThread.main(ActivityThread.java:4898)
10-23 10:22:37.867: E/AndroidRuntime(21490): at java.lang.reflect.Method.invokeNative(Native Method)
10-23 10:22:37.867: E/AndroidRuntime(21490): at java.lang.reflect.Method.invoke(Method.java:511)
10-23 10:22:37.867: E/AndroidRuntime(21490): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
10-23 10:22:37.867: E/AndroidRuntime(21490): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
10-23 10:22:37.867: E/AndroidRuntime(21490): at dalvik.system.NativeStart.main(Native Method)
de.isogon.kalepro.activities.Booklist $ 2.onDownload(Booklist.java:194)がconnection.connect();にリンクしている場所。上記のコードの。
そして、私のマニフェスト権限は次のとおりです。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.GET_TASKS"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
私はすでに多くのダウンロードコード例をテストしました-すべてがほぼ同じエラーを生成するようです。