Android下载zip文件错误(错误)

时间:2016-05-19 03:24:26

标签: java android inputstream foursquare okhttp

您好我正在从网上下载一个zip文件并保存在SD卡上。 我一直在java.net.SocketTimeoutException

错误:

io error: java.net.SocketTimeoutException
     at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
     at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:37)
     at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:237)
     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:381)
     at okio.RealBufferedSource$1.read(RealBufferedSource.java:371)
     at java.io.DataInputStream.read(DataInputStream.java:63)
     at java.io.InputStream.read(InputStream.java:162)
     at java.io.DataInputStream.read(DataInputStream.java:59)
     at com.my.app.SyncFragment$25$1.onResponse(SyncFragment.java:3473)
     at okhttp3.RealCall$AsyncCall.execute(RealCall.java:133)
     at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
     at java.lang.Thread.run(Thread.java:818)

我的代码:

...
//New Request
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
final OkHttpClient client = new OkHttpClient.Builder()
        .addInterceptor(logging)
        .build();


if (Utils.isConnectedToInternet(getActivity()))
    AsyncTask.execute(new Runnable() {
        @Override
        public void run() {

            client
                    .newCall(getRequest(url))
                    .enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            e.printStackTrace();
                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            if (!response.isSuccessful())
                                throw new IOException("Unexpected code " + response);

                            try {

                                InputStream ins = response.body().byteStream();
                                DataInputStream dis = new DataInputStream(ins);

                                byte[] buffer = new byte[1024];
                                int length;


                                FileOutputStream fos = new FileOutputStream(new File(folderPath + "/" + filename + ".zip"));
                                while ((length = dis.read(buffer)) > 0) { //ERROR FROM THIS LINE*
                                    fos.write(buffer, 0, length);
                                }

                            } catch (MalformedURLException mue) {
                                Log.e("SYNC downloadZipFile", "malformed url error: ", mue);
                            } catch (IOException ioe) {
                                Log.e("SYNC downloadZipFile", "io error: ", ioe);
                            } catch (SecurityException se) {
                                Log.e("SYNC downloadZipFile", "security error: ", se);
                            } catch (Exception e) {
                                Log.e("SYNC downloadZipFile", "Exception: ", e);
                            }
                        }
                    });
        }
    });
    ...

1 个答案:

答案 0 :(得分:0)

对于初学者,你实际上并没有提出问题,但我想你想知道这个错误意味着什么。

超时异常通常意味着您根本没有收到任何响应。 https://docs.oracle.com/javase/7/docs/api/java/net/SocketTimeoutException.html

检查目标文件是否确实存在,检查输入没有错误(http://或www丢失?),检查端口是否打开 - 如果端口在服务器端被阻止,则会发生这种情况。尝试使用其他文件,如果可能,请使用更多本地计算机上的文件。