retrofit:2.0.0-beta2 post 文件数组
retrofit:2.0.0-beta2 post array of files
我正在使用改造 retrofit: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);
问题是:如何使用 retrofit:2.0.0-beta2 Post 文件数组??
我上传了多张图片,如下所示。上传带有名字的图片。
服务接口:
@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(){..}
所以你改变return类型,Header名称,图像名称。
如果这也能对您有所帮助,我会很高兴。
服务器API合同:
输入类型:多部分来自数据
要在正文中针对名为“images”的键发送的数据
@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"), "");
}
我正在使用改造 retrofit: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);
问题是:如何使用 retrofit:2.0.0-beta2 Post 文件数组??
我上传了多张图片,如下所示。上传带有名字的图片。
服务接口:
@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(){..}
所以你改变return类型,Header名称,图像名称。
如果这也能对您有所帮助,我会很高兴。
服务器API合同: 输入类型:多部分来自数据
要在正文中针对名为“images”的键发送的数据
@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"), "");
}