-2

我okhttp3,想可以上传文件,上传json数据,但是发现只能同时上传HashMap,请问,怎么上传JSON?发现RequestBody类方法create,上传json和上传文件方法都重载了

4

1 回答 1

0

复杂请求-----Multipart/form-data

找到一些示例代码,但我还是不明白:

MultipartBody.Builder builder = new MultipartBody.Builder();
        MultipartBody body = builder
                .setType(MultipartBody.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(body)
                .build();

        Response response = OkHttpUtils.getInstance().getOkHttpClient().newCall(request).execute(); 
于 2016-03-19T06:46:11.770 回答