Exemplo n.º 1
0
  public SixRequest() {
    builder = new FormBody.Builder();
    handler = new Handler(Looper.getMainLooper());

    // add common params
    builder.add("platform", "android");
    builder.add("deviceId", "deviceID");
    builder.add("accessToken", "token");
    builder.add("timestamp", "" + System.currentTimeMillis());
    builder.add("version", "v1.0");
  }
Exemplo n.º 2
0
  @Override
  protected Request searchMangaRequest(MangasPage page, String query) {
    if (page.page == 1) {
      page.url = getInitialSearchUrl(query);
    }

    FormBody.Builder form = new FormBody.Builder();
    form.add("authorArtist", "");
    form.add("mangaName", query);
    form.add("status", "");
    form.add("genres", "");

    return ReqKt.post(page.url, getRequestHeaders(), form.build());
  }
Exemplo n.º 3
0
  public void execute(String url, final IRespListener listener) {
    RequestBody reqBody = builder.build();
    Request req = new Request.Builder().url(url).post(reqBody).build();
    OkHttpClient http = new OkHttpClient();
    http.newCall(req)
        .enqueue(
            new Callback() {
              @Override
              public void onFailure(final Call call, final IOException e) {
                Log.d("szw", "http error : " + e);
                handler.post(
                    new Runnable() {
                      @Override
                      public void run() {
                        listener.onHttpFail(call, e);
                      }
                    });
              }

              @Override
              public void onResponse(final Call call, final Response response) throws IOException {
                handler.post(
                    new Runnable() {
                      @Override
                      public void run() {
                        try {
                          listener.onHttpResp(call, response.body().string());
                        } catch (IOException e) {
                          e.printStackTrace();
                        }
                      }
                    });
              }
            });
  }
Exemplo n.º 4
0
 private void addParams(FormBody.Builder builder) {
   if (params != null) {
     for (String key : params.keySet()) {
       builder.add(key, params.get(key));
     }
   }
 }
Exemplo n.º 5
0
  /**
   * post请求
   *
   * @param tag
   * @param url
   * @param headers
   * @param params
   * @param postCallBack
   */
  public static void post(
      Object tag,
      String url,
      Map<String, String> headers,
      Map<String, String> params,
      final PostCallBack postCallBack) {
    Logger.i(TAG, "post url :" + url);
    setCancel(false);
    RequestBody requestBody = null;
    FormBody.Builder formBodyBuilder = new FormBody.Builder();
    if (params == null || params.size() == 0) {
      requestBody = formBodyBuilder.build();
    } else {
      for (Map.Entry<String, String> me : params.entrySet()) {
        formBodyBuilder.add(me.getKey(), me.getValue());
      }
      requestBody = formBodyBuilder.build();
    }
    Request.Builder requestBuilder = new Request.Builder();
    requestBuilder.tag(tag);
    requestBuilder.url(url);
    appendHeaders(requestBuilder, headers);
    requestBuilder.post(requestBody);
    Request request = requestBuilder.build();
    mCall = mClient.newCall(request);
    mCalls.add(mCall);
    mCall.enqueue(
        new Callback() {
          @Override
          public void onFailure(Call call, IOException e) {
            Logger.e(TAG, "post onFailure :" + e.toString());
            postCallBack.onFailure(call, e);
            EventBus.getDefault().post(new NetWorkBadEvent());
          }

          @Override
          public void onResponse(Call call, Response response) throws IOException {
            if (response.isSuccessful()) {
              Logger.i(TAG, "post onResponse");
              postCallBack.onResponse(call, response);
            } else {
              Logger.i(TAG, "post onError: " + response.code());
              postCallBack.onError(response.code());
            }
          }
        });
  }
Exemplo n.º 6
0
 @Deprecated
 private Request buildPostFormRequest(
     String url, WeakHashMap<String, String> params, Object... tag) {
   FormBody.Builder builder = new FormBody.Builder();
   Iterator<Map.Entry<String, String>> entries = params.entrySet().iterator();
   while (entries.hasNext()) {
     Map.Entry<String, String> entry = entries.next();
     builder.add(entry.getKey(), entry.getValue());
   }
   RequestBody formBody = builder.build();
   Request.Builder requestBuilder = new Request.Builder();
   requestBuilder.url(url);
   requestBuilder.post(formBody);
   if (tag.length > 0) {
     requestBuilder.tag(tag[0]);
   }
   return requestBuilder.build();
 }
Exemplo n.º 7
0
  @Override
  protected RequestBody buildRequestBody() {
    if (files == null || files.isEmpty()) {
      FormBody.Builder builder = new FormBody.Builder();
      addParams(builder);
      return builder.build();
    } else {
      MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
      addParams(builder);

      for (int i = 0; i < files.size(); i++) {
        PostFormBuilder.FileInput fileInput = files.get(i);
        RequestBody fileBody =
            RequestBody.create(MediaType.parse(guessMimeType(fileInput.filename)), fileInput.file);
        builder.addFormDataPart(fileInput.key, fileInput.filename, fileBody);
      }
      return builder.build();
    }
  }
Exemplo n.º 8
0
  /**
   * Request Factory
   *
   * @param url 请求url
   * @param params 参数
   * @param post true:post 方式; false :get方式
   * @return
   */
  @SuppressWarnings("unchecked")
  private static Request requestFactory(String url, JSONObject params, boolean post) {
    Request request = null;
    RequestBody formBody = null;
    if (post) {
      FormBody.Builder buider = new FormBody.Builder();
      if (params != null && params.keys() != null) {
        for (Iterator<String> iterator = params.keys(); iterator.hasNext(); ) {
          String key = iterator.next();
          buider.addEncoded(key, params.optString(key));
        }
        formBody = buider.build();
      }
    } else {
      if (params != null && params.length() > 0) {
        if (url.endsWith("?")) {
          url = url + json2String(params);
        } else {
          url = url + "?" + json2String(params);
        }
      }
    }

    if (formBody == null) {
      if (mHeaders != null) {
        request = new Request.Builder().headers(mHeaders).url(url).build();
      } else {
        request = new Request.Builder().url(url).build();
      }
    } else {
      if (post) {
        if (mHeaders != null) {
          request = new Request.Builder().headers(mHeaders).url(url).post(formBody).build();
        } else {
          request = new Request.Builder().url(url).post(formBody).build();
        }
      }
    }
    return request;
  }
Exemplo n.º 9
0
 public PostRequest formBuilder(FormBody.Builder builder) {
   requestBody = builder.build();
   requestBody =
       new ProgressRequestBody(
           requestBody,
           new ProgressListener() {
             @Override
             public void onProgress(long bytesRead, long contentLength, boolean done) {
               if (callback != null) {
                 callback.upload("", bytesRead, contentLength, done);
               }
             }
           });
   return this;
 }
Exemplo n.º 10
0
 public void addParam(String key, String value) {
   builder.add(key, value);
 }