0

It's normal when I use wifi to download the apk. Here is my code:

  private void singleDownloadThread() {
    try {
        URL url = new URL(entity.getUrl());
        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setConnectTimeout(CONNECT_TIME);
        connection.setReadTimeout(READ_TIME);
        FileOutputStream fos = new FileOutputStream(entity.getPath());
        InputStream is = connection.getInputStream();
        byte[] buffer = new byte[2048];
        int len = -1;
        long contentlength = connection.getContentLength();
        entity.setSize((int) contentlength);
        while ((len = is.read(buffer)) != -1) {
            entity.setProgress(entity.getProgress() + len);
            L.e("len:"+len+",projress:"+entity.getProgress()+",length:"+contentlength);
            Message msg = mHandler.obtainMessage();
            msg.what = ConstantValues.Download.UPDATA_DOWNLOAD_PROGRESS;
            msg.obj = entity;
            mHandler.sendMessage(msg);
            fos.write(buffer, 0, len);
        }
        entity.setStatus(DownloadStatus.done);
        Message msg = mHandler.obtainMessage();
        msg.what = ConstantValues.Download.DOWNLOAD_DONE;
        msg.obj = entity;
        mHandler.sendMessage(msg);

        is.close();
        fos.flush();
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
        entity.setStatus(DownloadStatus.netError);
        Message msg = mHandler.obtainMessage();
        msg.what = ConstantValues.Download.DOWNLOAD_ERROR;
        msg.obj = entity;
        mHandler.sendMessage(msg);
    }
}

It is the java.net.SocketTimeoutException int the end when downloding in mobile network, but I can download a little bit in the beginning. Then, the console prints the exception:

 W/System.err: java.net.SocketTimeoutException: timeout
W/System.err:     at com.android.okhttp.okio.Okio$3.newTimeoutException(Okio.java:212)
W/System.err:     at com.android.okhttp.okio.AsyncTimeout.exit(AsyncTimeout.java:250)
W/System.err:     at com.android.okhttp.okio.AsyncTimeout$2.read(AsyncTimeout.java:217)
W/System.err:     at com.android.okhttp.okio.RealBufferedSource.read(RealBufferedSource.java:50)
W/System.err:     at com.android.okhttp.internal.http.HttpConnection$FixedLengthSource.read(HttpConnection.java:418)
W/System.err:     at com.android.okhttp.okio.RealBufferedSource$1.read(RealBufferedSource.java:371)
W/System.err:     at java.io.InputStream.read(InputStream.java:101)
gaomode
  • 156
  • 1
  • 11
  • Mobile networks do take longer to respond than wifi. To simulate the same error on the emulator through wifi, you can slow down the speed and increase the response time of the network. In any case, if there is a timeout, then what do you think you should change in your code? – Stephan Branczyk Aug 03 '17 at 09:53
  • Thanks. Actually, the mobile networks is 4G which I can use to surfing the internet normally. I also get the same exception when I raise readtimeout and connecttimeout parameters to 15 seconds. And when I follow your advice "simulate the same error on the emulator through wifi" and set the emulator surfing in the mode of mobile networks, the code runs nomarlly. This is strange.@StephanBranczyk – gaomode Aug 03 '17 at 10:27

0 Answers0