Java 如何在OKhttp中通过多部分文件上传请求发送body参数

Java 如何在OKhttp中通过多部分文件上传请求发送body参数,java,android,okhttp3,Java,Android,Okhttp3,我正在尝试使用okhttp中的多部分请求上载文件。 使用下面的代码 public static Boolean uploadFile(String serverURL, File file) { try { RequestBody requestBody = new MultipartBuilder() .type(MultipartBuilder.FORM) .addFormDataPart("file"

我正在尝试使用okhttp中的多部分请求上载文件。 使用下面的代码

public static Boolean uploadFile(String serverURL, File file) {
    try {

        RequestBody requestBody = new MultipartBuilder()
                .type(MultipartBuilder.FORM)
                .addFormDataPart("file", file.getName(),
                    RequestBody.create(MediaType.parse("text/csv"), file))
                .addFormDataPart("some-field", "some-value")
                .build();

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

        client.newCall(request).enqueue(new Callback() {

            @Override
            public void onFailure(Request request, IOException e) {
                // Handle the error
            }

            @Override
            public void onResponse(Response response) throws IOException {
                if (!response.isSuccessful()) {
                    // Handle the error
                }
                // Upload successful
            }
        });

        return true;
    } catch (Exception ex) {
        // Handle the error
    }
    return false;
}
这是工作文件

我的问题是-我还想在body参数中发送一些键值对,但这在请求中造成了问题。

我使用下面的代码在多部分请求中附加键值体参数

FormBody.Builder formBodyBuilder = new FormBody.Builder();
formBodyBuilder.add("name", "Mac");
requestBody.addPart(formBodyBuilder.build());
但它正在制造问题。 任何帮助都将不胜感激。谢谢你试试这个

RequestBody formBody = new FormBody.Builder()
        .add("grant_type", "")
        .add("username", "")
        .add("password", "")
        .build();
RequestBody requestBodyy = new MultipartBody.Builder()
        .setType(MultipartBody.FORM)
        .addPart(
                Headers.of("content-type", "form-data; name=\"image\""),
                RequestBody.create(null, "Square Logo"))
        .addPart(
                Headers.of("content-type", "application/json; charset=utf-8"),
                formBody)
        .build();
你试试这个

RequestBody formBody = new FormBody.Builder()
        .add("grant_type", "")
        .add("username", "")
        .add("password", "")
        .build();
RequestBody requestBodyy = new MultipartBody.Builder()
        .setType(MultipartBody.FORM)
        .addPart(
                Headers.of("content-type", "form-data; name=\"image\""),
                RequestBody.create(null, "Square Logo"))
        .addPart(
                Headers.of("content-type", "application/json; charset=utf-8"),
                formBody)
        .build();

我正在使用Okhttp3执行此操作,它工作正常:

MediaType MEDIA_TYPE = MediaType.parse(MIME_TYPE);
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("type", stringsToPost[0])
                ...
                .addFormDataPart("file", file.getName(), RequestBody.create(MEDIA_TYPE, file));

我正在使用Okhttp3执行此操作,它工作正常:

MediaType MEDIA_TYPE = MediaType.parse(MIME_TYPE);
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("type", stringsToPost[0])
                ...
                .addFormDataPart("file", file.getName(), RequestBody.create(MEDIA_TYPE, file));

我们如何再添加一个同名文件我们如何再添加一个同名文件