Android 如何使用@MultiPart在改装中与@Body一起上载多个图像/文件
我需要在改型中使用@POST提交大数据(嵌套的json对象)以及多个图像/文件 一种简单的方法是将图像转换为base64字符串并放入RequestBody中,不使用@Multipart发送。另一种方法是使用Android 如何使用@MultiPart在改装中与@Body一起上载多个图像/文件,android,retrofit,retrofit2,Android,Retrofit,Retrofit2,我需要在改型中使用@POST提交大数据(嵌套的json对象)以及多个图像/文件 一种简单的方法是将图像转换为base64字符串并放入RequestBody中,不使用@Multipart发送。另一种方法是使用@PartMap()Map。将一些数据与图像一起附加。如果数据较少,则此方法有效。但我有一个巨大的嵌套请求体,如下所示: { "id": "id12", "bar": { "id": "id1", "name": "asd" } } ..... 但是如何使用@Mu
@PartMap()Map
。将一些数据与图像一起附加。如果数据较少,则此方法有效。但我有一个巨大的嵌套请求体,如下所示:
{
"id": "id12",
"bar": {
"id": "id1",
"name": "asd"
}
} .....
但是如何使用@Multipart发送身体和图像
@Multipart
@POST("api/upload")
Observable<SubmitResponse> submitData(@Body Foo foo, @Part MultipartBody.Part[] files);
class Foo {
@SerializedName("book_id")
@Expose
private String book_id;
@SerializedName("bar")
@Expose
private Bar anotherBodyinsideFoo;
}
@Multipart
@发布(“api/上传”)
可观察的submitData(@Body Foo Foo,@Part MultipartBody.Part[]文件);
福班{
@SerializedName(“图书id”)
@暴露
私有字符串图书标识;
@序列化名称(“bar”)
@暴露
私人酒吧,另一个在Efo内的机构;
}
上述方法引发错误:@Body参数不能与表单或多部分编码一起使用
就如何在改型中随请求正文(@body Foo)上载多个文件给出建议。请检查以下代码,以便使用改型上载多个图像。
@POST(UPLOAD_EVENT)
Call<EventResponse> uploadEvent(@Header("Authorization") String auth, @Body RequestBody body);
MultipartBody.Builder builder = new MultipartBody.Builder();
builder.setType(MultipartBody.FORM);
builder.addFormDataPart("event_name", eventName);
builder.addFormDataPart("location", loacation);
builder.addFormDataPart("longitude", longitude);
builder.addFormDataPart("latitude", latitude);
builder.addFormDataPart("is_private", isPublic);
builder.addFormDataPart("caption", caption);
RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), coverImage);
builder.addFormDataPart("cover_image", coverImage.getName(), requestBody);
RequestBody requestBody1 = null;
for (int i = 0, size = eventFiles.size(); i < size; i++) {
requestBody1 = RequestBody.create(MediaType.parse("multipart/form-data"), eventFiles.get(i));
builder.addFormDataPart("album_images" + "[" + i + "]", eventFiles.get(i).getName(), requestBody1);
}
RequestBody finalRequestBody = builder.build();
final Retrofit retrofit = new Retrofit.Builder()
.baseUrl(RestClient.ROOT)
.addConverterFactory(GsonConverterFactory.create())
.build();
RestClient.NetworkCall networkCall = retrofit.create(RestClient.NetworkCall.class);
Call<EventResponse> response = networkCall.uploadEvent(Prefs.getAuth(App.getContext()), finalRequestBody);
response.enqueue(new Callback<EventResponse>() {
@Override
public void onResponse(Call<EventResponse> call, Response<EventResponse> response) {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(MY_NOTIFICATION_ID);
if (response.code() == Constant.SUCCESS_STATUS) {
if (response.body() != null) {
sendBroadcast(new Intent(Constant.REFRESH_FEEDS));
Log.e("Success", "Success");
}
} else {
Converter<ResponseBody, EventResponse> converter =
retrofit.responseBodyConverter(EventResponse.class, new Annotation[0]);
try {
if (response.errorBody() != null) {
EventResponse errorResponse = converter.convert(response.errorBody());
Log.e("Failure", "Failure");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onFailure(Call<EventResponse> call, Throwable t) {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(MY_NOTIFICATION_ID);
Log.e("Failure", "Failure");
}
});
@POST(上传事件)
调用uploadEvent(@Header(“Authorization”)字符串auth,@Body-RequestBody-Body);
MultipartBody.Builder=新的MultipartBody.Builder();
setType(MultipartBody.FORM);
builder.addFormDataPart(“事件名称”,事件名称);
builder.addFormDataPart(“位置”,位置);
builder.addFormDataPart(“经度”,经度);
builder.addFormDataPart(“纬度”,纬度);
builder.addFormDataPart(“is_private”,isPublic);
builder.addFormDataPart(“标题”,标题);
RequestBody RequestBody=RequestBody.create(MediaType.parse(“多部分/表单数据”),coverImage);
addFormDataPart(“cover_image”,coverImage.getName(),requestBody);
RequestBody requestBody1=null;
对于(int i=0,size=eventFiles.size();i
其中一个选项是将图像上传为Base64。是的,现在我只做这个。但我想通过@MultiPart来实现