public void execute(Callback callback) {
    generateCall(callback);

    if (callback != null) {
      callback.onBefore(request);
    }

    OkHttpUtils.getInstance().execute(this, callback);
  }
예제 #2
0
  public File saveFile(Response response) throws IOException {
    InputStream is = null;
    byte[] buf = new byte[2048];
    int len = 0;
    FileOutputStream fos = null;
    try {
      is = response.body().byteStream();
      final long total = response.body().contentLength();
      long sum = 0;

      L.e(total + "");

      File dir = new File(destFileDir);
      if (!dir.exists()) {
        dir.mkdirs();
      }
      File file = new File(dir, destFileName);
      fos = new FileOutputStream(file);
      while ((len = is.read(buf)) != -1) {
        sum += len;
        fos.write(buf, 0, len);
        final long finalSum = sum;
        OkHttpUtils.getInstance()
            .getDelivery()
            .post(
                new Runnable() {
                  @Override
                  public void run() {

                    inProgress(finalSum * 1.0f / total);
                  }
                });
      }
      fos.flush();

      return file;

    } finally {
      try {
        if (is != null) is.close();
      } catch (IOException e) {
      }
      try {
        if (fos != null) fos.close();
      } catch (IOException e) {
      }
    }
  }
  public Call generateCall(Callback callback) {
    request = generateRequest(callback);

    if (readTimeOut > 0 || writeTimeOut > 0 || connTimeOut > 0) {
      cloneClient();

      readTimeOut = readTimeOut > 0 ? readTimeOut : OkHttpUtils.DEFAULT_MILLISECONDS;
      writeTimeOut = writeTimeOut > 0 ? writeTimeOut : OkHttpUtils.DEFAULT_MILLISECONDS;
      connTimeOut = connTimeOut > 0 ? connTimeOut : OkHttpUtils.DEFAULT_MILLISECONDS;

      clone.setReadTimeout(readTimeOut, TimeUnit.MILLISECONDS);
      clone.setWriteTimeout(writeTimeOut, TimeUnit.MILLISECONDS);
      clone.setConnectTimeout(connTimeOut, TimeUnit.MILLISECONDS);

      call = clone.newCall(request);
    } else {
      call = OkHttpUtils.getInstance().getOkHttpClient().newCall(request);
    }
    return call;
  }
 private void cloneClient() {
   clone = OkHttpUtils.getInstance().getOkHttpClient().clone();
 }