Exemplo n.º 1
0
  @Deprecated
  private Request buildMultipartFormRequest(
      String url,
      String[] fileKeys,
      File[] files,
      WeakHashMap<String, String> params,
      Object... tag) {
    MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);

    if (params != null) {
      Iterator<Map.Entry<String, String>> entries = params.entrySet().iterator();
      while (entries.hasNext()) {
        Map.Entry<String, String> entry = entries.next();
        builder.addFormDataPart(entry.getKey(), entry.getValue());
      }
    }

    if (files != null && files.length > 0) {
      for (int i = 0; i < files.length; i++) {
        File file = files[i];
        String fileName = file.getName();
        MediaType mediaType = MediaType.parse(guessMediaType(fileName));
        builder.addFormDataPart(fileKeys[i], fileName, RequestBody.create(mediaType, file));
      }
    }
    RequestBody requestBody = builder.build();
    Request.Builder requestBuilder = new Request.Builder();
    requestBuilder.url(url);
    requestBuilder.post(requestBody);
    if (tag.length > 0) {
      requestBuilder.tag(tag[0]);
    }
    return requestBuilder.build();
  }
    @Override
    protected RequestBody postBody(File file) {
      // 设置请求体
      MultipartBody.Builder builder = new MultipartBody.Builder();
      builder.setType(MultipartBody.FORM);
      // 封装文件请求体
      if (file != null && file.exists()) {
        String filename = file.getName();
        MediaType mediaType = MediaType.parse(guessMimeType(filename));
        RequestBody fileBody = RequestBody.create(mediaType, file);
        builder.addFormDataPart("file", filename, fileBody);
      }
      // 封装请求参数
      HashMap<String, String> params = new HashMap<>();
      addParams(params);
      if (params != null && !params.isEmpty()) {
        for (String key : params.keySet()) {
          builder.addPart(
              Headers.of("Content-Disposition", "form-data; name=\"" + key + "\""),
              RequestBody.create(null, params.get(key)));
        }
      }

      return builder.build();
    }
Exemplo n.º 3
0
 private void addParams(MultipartBody.Builder builder) {
   if (params != null && !params.isEmpty()) {
     for (String key : params.keySet()) {
       builder.addPart(
           Headers.of("Content-Disposition", "form-data; name=\"" + key + "\""),
           RequestBody.create(null, params.get(key)));
     }
   }
 }
Exemplo n.º 4
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.º 5
0
  @Deprecated
  private Request buildMultipartFormRequest2(
      String url,
      String[] fileKeys,
      File[] files,
      WeakHashMap<String, String> params,
      Object... tag) {

    MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);

    if (params != null) {
      Iterator<Map.Entry<String, String>> entries = params.entrySet().iterator();
      while (entries.hasNext()) {
        Map.Entry<String, String> entry = entries.next();
        builder.addPart(
            Headers.of("Content-Disposition", "form-data; name=\"" + entry.getKey() + "\""),
            RequestBody.create(null, entry.getValue()));
      }
    }

    if (files != null) {
      RequestBody fileBody = null;
      for (int i = 0; i < files.length; i++) {
        File file = files[i];
        String fileName = file.getName();
        fileBody = RequestBody.create(MediaType.parse(guessMediaType(fileName)), file);
        builder.addPart(
            Headers.of(
                "Content-Disposition",
                "form-data; name=\"" + fileKeys[i] + "\"; filename=\"" + fileName + "\""),
            fileBody);
      }
    }

    RequestBody requestBody = builder.build();
    Request.Builder requestBuilder = new Request.Builder();
    requestBuilder.url(url);
    requestBuilder.post(requestBody);
    if (tag.length > 0) {
      requestBuilder.tag(tag[0]);
    }
    return requestBuilder.build();
  }
Exemplo n.º 6
0
  /**
   * 文件上传
   *
   * @param tag
   * @param url
   * @param filePath
   * @param headers
   * @param params
   * @param uploadFileCallBack
   */
  public static void uploadFile(
      Object tag,
      String url,
      String filePath,
      Map<String, String> headers,
      Map<String, String> params,
      final UploadFileCallBack uploadFileCallBack) {
    Logger.i(TAG, "uploadFile url :" + url);
    setCancel(false);
    File file = new File(filePath);
    MultipartBody.Builder multipartBuilder =
        new MultipartBody.Builder().setType(MultipartBody.FORM);
    addParams(multipartBuilder, params);
    addFiles(multipartBuilder, new Pair<String, File>("file", file));
    progerssRequestBody = new ProgressRequestBody(multipartBuilder.build(), uploadFileCallBack);
    Request.Builder builder = new Request.Builder();
    appendHeaders(builder, headers);
    Request request = builder.tag(tag).url(url).post(progerssRequestBody).build();

    mCall = mClient.newCall(request);
    mCalls.add(mCall);
    mCall.enqueue(
        new Callback() {
          @Override
          public void onFailure(Call call, IOException e) {
            Logger.e(TAG, "uploadFile onFailure :" + e.toString());
            uploadFileCallBack.onFailure(call, e);
            EventBus.getDefault().post(new NetWorkBadEvent());
          }

          @Override
          public void onResponse(Call call, Response response) throws IOException {
            if (response.isSuccessful()) {
              Logger.i(TAG, "uploadFile onResponse");
              uploadFileCallBack.onResponse(call, response);
            } else {
              Logger.i(TAG, "uploadFile onError: " + response.code());
              uploadFileCallBack.onError(response.code());
            }
          }
        });
  }
Exemplo n.º 7
0
 public PostRequest multiBuilder(MultipartBody.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.º 8
0
 private static void addFiles(MultipartBody.Builder builder, Pair<String, File>... files) {
   if (files != null) {
     RequestBody fileBody;
     for (int i = 0; i < files.length; i++) {
       Pair<String, File> filePair = files[i];
       String fileKeyName = filePair.first;
       File file = filePair.second;
       String fileName = file.getName();
       fileBody = RequestBody.create(MediaType.parse(guessMimeType(fileName)), file);
       builder.addPart(
           Headers.of(
               "Content-Disposition",
               "form-data; name=\"" + fileKeyName + "\"; filename=\"" + fileName + "\""),
           fileBody);
     }
   } else {
     throw new IllegalArgumentException("File can not be null");
   }
 }