我正在使用改装版2.0.0-beta2向服务器发送多个数据。数据包含文件ArrayList<File>
的数组。我正在使用此代码发送分开的文件:
public Call<User> requestUpdateProfile3(String token, File image) {
RequestBody requestBodyImage = RequestBody.create(MediaType.parse("multipart/form-data"), image);
return apiService.updateProfile3(token, requestBodyProfile, requestBodyImage);
这个在ApiService 上
@Multipart
@POST("/shanyraq/profile/update")
Call<User> updateProfile3(
@Header(value = "X-AUTH-TOKEN") String toke,
@Part("1"; filename="1.jpg") RequestBody image);
问题是:如何使用改进版2.0.0-beta2发布文件阵列??
我上传了多张图片,如下所示。上传带有他们名字的图片。
服务接口:
@Multipart
@POST(ConstantsWebService.UPLOAD_SERVICES_IMAGE)
Call<List<String>> uploadImage(
@Header("Authorization") String token,
//@Part("file"; filename="902367000083-1.jpg") RequestBody mapFileAndName); //for sending only one image
@PartMap() Map<String,RequestBody> mapFileAndName); //for sending multiple images
请求:
HashMap<String,RequestBody> map=new HashMap<>(listOfNames.size());
RequestBody file=null;
File f=null;
for(int i=0,size=listOfNames.size(); i<size;i++){
try {
f = new File(context.getCacheDir(), listOfNames.get(i)+".jpg");
FileOutputStream fos = new FileOutputStream(f);
Bitmap bitmap = getImageFromDatabase(listOfNames.get(i));
if(bitmap!=null){
bitmap.compress(Bitmap.CompressFormat.JPEG, 0 /*ignored for PNG*/, fos);
fos.flush();
fos.close();
}else{
view.showErrorView("imageNotFound"); //todo
return;
}
} catch (Exception e) {
e.printStackTrace();
view.showErrorView("imageNotFound || file not created"); //todo
return;
}
file=RequestBody.create(MediaType.parse("multipart/form-data"), f);
map.put("file"; filename=""+listOfNames.get(i)+".jpg",file);
file=null;
f = null;
}
serviceOperation.uploadImage(token,map).enqueue(){..}
所以您可以更改返回类型、标题名称、图像名称。
如果这也能帮助到你,我将非常高兴。
服务器API协定:输入类型:来自数据的多部分
根据名为"图像"的密钥在正文中发送数据
@POST("feeds")
Call<> createFeeds(@Body RequestBody file);
MultipartBody.Builder builder = new MultipartBody.Builder();
builder.setType(MultipartBody.FORM);
builder.addFormDataPart("content", textContent);
for(String filePath : imagePathList){
File file = new File(filePath);
builder.addFormDataPart("images", file.getName(),
RequestBody.create(MediaType.parse("image/*"), file));
}
MultipartBody requestBody = builder.build();
Call<SocialCreateFeedResponse> call = mSocialClient.createFeeds( requestBody);
Interface structure:
@Multipart
@POST("user_edit_profile_save")
Call<EditProfileDto> saveEditProfile(@Part("first_name") RequestBody first_name,
@Part MultipartBody.Part profile_picture);
Calling:
RequestBody requestBodyUserId = RequestBody.create(
MediaType.parse("multipart/form-data"), "5");
MultipartBody.Part body = null;
if (path != null && path.length() > 0) {
file = new File(path);
isImageSet = true;
requestBodyPicture = RequestBody.create(
MediaType.parse("multipart/form-data"), file);
body = MultipartBody.Part.createFormData("profile_picture", file.getName(), requestBodyPicture);
} else {
requestBodyPicture = RequestBody.create(
MediaType.parse("multipart/form-data"), "");
}