-2

I'm trying to download a zip-file from a server. For that I'm using the following code:

        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();
        }

But when I try to download the file I get the following Logcat errors:

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)

Where de.isogon.kalepro.activities.Booklist$2.onDownload(Booklist.java:194) links to connection.connect(); of the code above.

And my manifest permissions are the following:

<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" />

I've testet a lot of downloading code examples already - all all seem to produce nearly the same error.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Sadragos
  • 35
  • 8

4 Answers4

2

As you can see from your Logcat: 10-23 10:22:37.867: E/AndroidRuntime(21490): android.os.NetworkOnMainThreadException

the reason for this not working is since you are using the UI thread for this. try adding the downloading to a AsyncTask ir running on a separate Thread.

thepoosh
  • 12,497
  • 15
  • 73
  • 132
1

Please don't write network call in main UI Thread, use another thread for that, it will solve your problem and see below link for more information.

Why Ice Cream Sandwich Crashes Your App

Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
0

I works with this Class

public class ZipUtility {
    public static final void zipDirectory(File directory, File zip)
            throws IOException {
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zip));
        zip(directory, directory, zos);
        zos.close();
    }

    private static final void zip(File directory, File base, ZipOutputStream zos)
            throws IOException {
        File[] files = directory.listFiles();
        byte[] buffer = new byte[8192];
        int read = 0;
        for (int i = 0, n = files.length; i < n; i++) {
            if (files[i].isDirectory()) {
                zip(files[i], base, zos);
            } else {
                FileInputStream in = new FileInputStream(files[i]);
                ZipEntry entry = new ZipEntry(files[i].getPath().substring(
                        base.getPath().length() + 1));
                zos.putNextEntry(entry);
                while (-1 != (read = in.read(buffer))) {
                    zos.write(buffer, 0, read);
                }
                in.close();
            }
        }
    }

    public static final void unzip(File zip, File extractTo) throws IOException {
        ZipFile archive = new ZipFile(zip);
        Enumeration e = archive.entries();
        while (e.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) e.nextElement();
            File file = new File(extractTo, entry.getName());
            if (entry.isDirectory() && !file.exists()) {
                file.mkdirs();
            } else {
                if (!file.getParentFile().exists()) {
                    file.getParentFile().mkdirs();
                }

                InputStream in = archive.getInputStream(entry);
                BufferedOutputStream out = new BufferedOutputStream(
                        new FileOutputStream(file));

                byte[] buffer = new byte[8192];
                int read;

                while (-1 != (read = in.read(buffer))) {
                    out.write(buffer, 0, read);
                }

                in.close();
                out.close();
            }
        }
    }
}
Sieryuu
  • 1,510
  • 2
  • 16
  • 41
0

NetworkOnMainThreadException is because you're trying to do network operations on the main thread. You should make a AsyncTask and download the file like that

Should be fairly easy, most of your code could go in the doInBackground in the asyncTask

Stupidus
  • 461
  • 3
  • 10