Android,okhttp同时上传文件和json
Android,okhttp meanwhile upload file and json
我okhttp3,我想可以上传文件,上传JSON数据,但是发现同时只能上传HashMap,请问,怎么上传JSON?我发现RequestBodyclass 方法创建、上传 json 和上传文件方法已重载
复杂请求-----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();
我okhttp3,我想可以上传文件,上传JSON数据,但是发现同时只能上传HashMap,请问,怎么上传JSON?我发现RequestBodyclass 方法创建、上传 json 和上传文件方法已重载
复杂请求-----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();