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) {
      }
    }
  }
Пример #3
0
  @Override
  public void login(String account, String password, final OnLoginFinishedListener listener) {
    if (TextUtils.isEmpty(account)) {
      listener.onAccountError();
    } else if (TextUtils.isEmpty(password)) {
      listener.onPasswordError();
    } else {
      PersonWithDeviceTokenBean personBean = new PersonWithDeviceTokenBean();
      personBean.setAccount(account);
      personBean.setPassword(Util.getMD5(password));
      personBean.setDevicetoken(MyApplication.mDevicetoken);
      L.i("loginmodelimpl", personBean.toString());
      OkHttpUtils.postString()
          .url(Urls.LOGIN_URL)
          .content(new Gson().toJson(personBean))
          .build()
          .execute(
              new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                  L.i("login_response", e.toString());
                  L.i("login_response", id + "");
                  listener.onNetWorkError();
                }

                @Override
                public void onResponse(String response, int id) {
                  L.i("response", response);
                  ResultBean resultBean = new Gson().fromJson(response, ResultBean.class);
                  if (resultBean.getStatus().equals("true")) {
                    PersonBean personBean =
                        new Gson().fromJson(resultBean.getResponse(), PersonBean.class);
                    listener.onSuccess(personBean);
                  } else {
                    if (resultBean.getResponse().equals("Login Error!")) {
                      listener.onUserError();
                    } else {
                      listener.onNetWorkError();
                    }
                  }
                }
              });
    }
  }
  /** 向服务器发出上传新版本 */
  private void requestServer(String title, int type, String keyword, String summary, int id) {
    ProgressDialogUtil.show(mContext);
    OkHttpUtils.post()
        .url(Constant.URL + "recontribute")
        .addParams("articleId", String.valueOf(id))
        .addParams("title", title)
        .addParams("type", String.valueOf(type))
        .addParams("keyword", keyword)
        .addParams("summary", summary)
        .addFile("articleFile", mFileName, mFile)
        .addParams("source", "android")
        .build()
        .execute(
            new StringCallback() {
              @Override
              public void onError(Request request, Exception e) {
                ProgressDialogUtil.dismiss();
                Log.e(getTagName(), "onError:" + e.getMessage());
                T.show(mContext, "网络访问错误");
              }

              @Override
              public void onResponse(String response) {
                ProgressDialogUtil.dismiss();
                Log.e(getTagName(), "onResponse:" + response);
                try {
                  JSONObject serverInfo = new JSONObject(response);
                  boolean isSuccess = serverInfo.getBoolean("isSuccess");
                  if (isSuccess) {
                    T.show(mContext, "上传新版本成功!!!");
                    // 返回上传新版本信息
                    Intent returnIntent = new Intent();
                    returnIntent.putExtra("isContributeNewManuscript", isSuccess);
                    ContributeManuscriptActivity.this.setResult(Activity.RESULT_OK, returnIntent);
                    finish();
                  }
                } catch (JSONException e) {
                  e.printStackTrace();
                }
              }
            });
  }
  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;
  }
Пример #6
0
  private void getJson(final int type) {
    OkHttpUtils.get()
        .url(url)
        .build()
        .execute(
            new StringCallback() {
              @Override
              public void onError(Call call, Exception e) {

                Toast.makeText(SearchNews.this, R.string.intent_error, Toast.LENGTH_SHORT).show();
              }

              @Override
              public void onResponse(String response) {
                if (type == 1) {
                  arrayList.clear();
                }
                setArray(response);
                adapter.notifyDataSetChanged();
              }
            });
  }
Пример #7
0
  private void getArtistInfo(String tingUid) {
    OkHttpUtils.get()
        .url(Constants.BASE_URL)
        .addParams(Constants.PARAM_METHOD, Constants.METHOD_ARTIST_INFO)
        .addParams(Constants.PARAM_TING_UID, tingUid)
        .build()
        .execute(
            new JsonCallback<JArtistInfo>(JArtistInfo.class) {
              @Override
              public void onResponse(JArtistInfo response) {
                ViewUtils.changeViewState(
                    svArtistInfo, llLoading, llLoadFail, LoadStateEnum.LOAD_SUCCESS);
                onSuccess(response);
              }

              @Override
              public void onError(Call call, Exception e) {
                ViewUtils.changeViewState(
                    svArtistInfo, llLoading, llLoadFail, LoadStateEnum.LOAD_FAIL);
              }
            });
  }
 private void cloneClient() {
   clone = OkHttpUtils.getInstance().getOkHttpClient().clone();
 }