2

我想重新编码我的项目并使用 okHttp 而不是 Android 中实现的默认 HttpClient。

我已经下载了 okhttp-main 版本的最新源代码。

现在我找到了一些如何创建和构建 POST 请求的示例。

现在我的问题。我想创建一个 RequestBody 来保留几个数据(字符串、文件等),但我不能直接分配它们。

意味着 RequestBuilder 必须通过不同的循环来添加数据。

OkHTTPs RequestBody 似乎立即需要示例中列出的数据 https://github.com/square/okhttp/wiki/Recipes

当我想尝试类似的东西时

RequestBody requestBody = new MultipartBuilder().type(MultipartBuilder.FORM);

for (Object aMData : dataClass.getData().entrySet()) {
            Map.Entry mapEntry = (Map.Entry) aMData;
            String keyValue = (String) mapEntry.getKey();
            String value = (String) mapEntry.getValue();
            requestBody.addPart(keyValue, value);
}


for (DataPackage dataPackage : dataClass.getDataPackages()) {
            requestBody.addPart("upfile[]", dataPackage.getFile());
}

requestBody.build();

它失败了,因为 build() 本身创建了 RequestBody。在它只是一个 MultipartBuilder() 之前。如果我尝试强制类型为 RequestBody 它不会编译/运行。

那么,在创建 MultiPartBuilder 并添加 DATA 和 Strings 之后添加这些数据的正确方法是什么?

4

3 回答 3

2

使用 OkHttp 分段上传文件

private static final String IMGUR_CLIENT_ID = "...";
private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");

private final OkHttpClient client = new OkHttpClient();

public void run() throws Exception {
  // Use the imgur image upload API as documented at https://api.imgur.com/endpoints/image
  RequestBody requestBody = new MultipartBuilder()
      .type(MultipartBuilder.FORM)
      .addPart(
          Headers.of("Content-Disposition", "form-data; name=\"title\""),
          RequestBody.create(null, "Square Logo"))
      .addPart(
          Headers.of("Content-Disposition", "form-data; name=\"image\""),
          RequestBody.create(MEDIA_TYPE_PNG, new File("website/static/logo-square.png")))
      .build();

  Request request = new Request.Builder()
      .header("Authorization", "Client-ID " + IMGUR_CLIENT_ID)
      .url("https://api.imgur.com/3/image")
      .post(requestBody)
      .build();

  Response response = client.newCall(request).execute();
  if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

  System.out.println(response.body().string());
}
于 2015-04-07T16:38:31.753 回答
2

这对我使用 okHttp3 有用:

            OkHttpClient client = new OkHttpClient();
            File file = new File(payload);

            RequestBody formBody = new MultipartBody.Builder()
                    .setType(MultipartBody.FORM)
                    .addFormDataPart("file", "image.jpg",
                            RequestBody.create(MediaType.parse("image/jpg"), file))

                    .build();

            Request request = new Request.Builder().url(url).post(formBody).build();

            Response response = client.newCall(request).execute();
            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
于 2016-06-21T10:49:48.783 回答
1

我修改了 Enemy 博士的回答

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

for (Object aMData : dataClass.getData().entrySet()) {
        Map.Entry mapEntry = (Map.Entry) aMData;
        String keyValue = (String) mapEntry.getKey();
        String value = (String) mapEntry.getValue();
        builder.addPart(keyValue, value);
}

for (DataPackage dataPackage : dataClass.getDataPackages()) {
        builder.addPart("upfile[]", dataPackage.getFile());
}

开始将 formDataPart 添加到构建器,最后创建 RequestBody

RequestBody requestBody = builder.build();

您可以执行上述操作

 compile 'com.squareup.okhttp3:okhttp:3.4.1'
于 2017-01-03T13:21:40.343 回答