使用Okhttp下载zip文件并将其保存在外部存储中

时间:2016-01-09 18:26:22

标签: java android okhttp

我想从服务器下载一个zip文件并将其保存在SD卡上,以便我在SF上搜索并找到solutionlink

我可以成功获取数据长度并打印标题。

@Override
        public void onResponse(Response response) throws IOException {
            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
            Headers responseHeaders = response.headers();
            for (int i = 0; i < responseHeaders.size(); i++) {
                System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
            }

            System.out.println(response.body().string());

        }

所以在这个步骤中如何在磁盘上保存zip文件,另一个问题是在一秒钟后我得到这个日志

  01-09 18:12:19.528 17196-17941/com.myapp I/System.out: Content-Type:    application/zip
  01-09 18:12:19.528 17196-17941/com.myapp I/System.out: Content-Length:   13467179
  01-09 18:12:19.528 17196-17941/com.myapp I/System.out: Vary: User-Agent
  01-09 18:12:19.528 17196-17941/com.myapp I/System.out: OkHttp-Sent-Millis: 1452363138725
  01-09 18:12:19.528 17196-17941/com.myapp I/System.out: OkHttp-Received-Millis: 1452363139533
  01-09 18:12:26.608 17196-17515/com.myapp I/okhttp3.OkHttpClient: Callback   failure for call to http://myapp.com....
                                                                                       java.net.SocketTimeoutException
                                                                                       at java.net.PlainSocketImpl.read(PlainSocketImpl.java:491)
                                                                                       at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
                                                                                       at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
                                                                                       at okio.Okio$2.read(Okio.java:139)
                                                                                       at okio.AsyncTimeout$2.read(AsyncTimeout.java:211)
                                                                                       at okio.RealBufferedSource.read(RealBufferedSource.java:50)
                                                                                       at okhttp3.internal.http.Http1xStream$FixedLengthSource.read(Http1xStream.java:382)
                                                                                       at okio.Buffer.writeAll(Buffer.java:956)
                                                                                       at okio.RealBufferedSource.readByteArray(RealBufferedSource.java:92)
                                                                                       at okhttp3.ResponseBody.bytes(ResponseBody.java:83)
                                                                                       at okhttp3.ResponseBody.string(ResponseBody.java:109)
                                                                                       at com.myapp.GetDataActivity$1.onResponse(GetDataActivity.java:99)
                                                                                       at okhttp3.RealCall$AsyncCall.execute(RealCall.java:133)
                                                                                       at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:33)
                                                                                       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
                                                                                       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
                                                                                       at java.lang.Thread.run(Thread.java:856)
    01-09 18:12:49.380 17196-17941/com.myapp I/okhttp3.OkHttpClient: Callback failure for call to http://www.myapp.com/...
                                                                                   java.net.SocketTimeoutException
                                                                                       at java.net.PlainSocketImpl.read(PlainSocketImpl.java:491)
                                                                                       at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
                                                                                       at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
                                                                                       at okio.Okio$2.read(Okio.java:139)
                                                                                       at okio.AsyncTimeout$2.read(AsyncTimeout.java:211)
                                                                                       at okio.RealBufferedSource.read(RealBufferedSource.java:50)
                                                                                       at okhttp3.internal.http.Http1xStream$FixedLengthSource.read(Http1xStream.java:382)
                                                                                       at okio.Buffer.writeAll(Buffer.java:956)
                                                                                       at okio.RealBufferedSource.readByteArray(RealBufferedSource.java:92)
                                                                                       at okhttp3.ResponseBody.bytes(ResponseBody.java:83)
                                                                                       at okhttp3.ResponseBody.string(ResponseBody.java:109)
                                                                                       at com.myapp.GetDataActivity$1.onResponse(GetDataActivity.java:99)
                                                                                       at okhttp3.RealCall$AsyncCall.execute(RealCall.java:133)
                                                                                       at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:33)
                                                                                       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
                                                                                       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
                                                                                       at java.lang.Thread.run(Thread.java:856)

0 个答案:

没有答案