“改造”一个多部分请求中附加的多个图像

有没有办法在多个请求中附加多个图像? 根据用户select的图像数量,图像是dynamic的。

以下代码仅适用于单幅图片:

接口:

@Multipart @POST("/post") void createPostWithAttachments( @Part("f[]") TypedFile image,@PartMap Map<String, String> params,Callback<String> response); 

执行:

 TypedFile file = new TypedFile("image/jpg", new File(gallery.sdcardPath)); Map<String,String> params = new HashMap<String,String>(); params.put("key","value"); ServicesAdapter.getAuthorizeService().createPostWithAttachments(file,params, new Callback<String>() { @Override public void success(String s, Response response) { DBLogin.updateCookie(response); new_post_text.setText(""); try { JSONObject json_response = new JSONObject(s); Toast.makeText(getApplicationContext(), json_response.getString("message"), Toast.LENGTH_LONG).show(); if (json_response.getString("status").equals("success")) { JSONObject dataObj = json_response.getJSONObject("data"); Intent intent = new Intent(getApplicationContext(), MainActivity.class); startActivity(intent); finish(); } else { Log.d(TAG, "Request failed"); } } catch (Exception e) { Log.d(TAG, e.getMessage()); } } @Override public void failure(RetrofitError retrofitError) { Toast.makeText(getApplicationContext(), retrofitError.getMessage(), Toast.LENGTH_LONG).show(); } }); 

经过翻新提供的文档后,我可以通过我自己的解决scheme来完成,也许是不是很好,但仍然设法得到它的工作..

这里是引用 MultipartTypedOutput

其实和上面的代码非常相似,只是做了一点改动

接口

 @POST("/post") void createPostWithAttachments( @Body MultipartTypedOutput attachments,Callback<String> response); 

履行

  MultipartTypedOutput multipartTypedOutput = new MultipartTypedOutput(); multipartTypedOutput.addPart("c", new TypedString(text)); multipartTypedOutput.addPart("_t", new TypedString("user")); multipartTypedOutput.addPart("_r", new TypedString(userData.user.id)); //loop through object to get the path of the images that has picked by user for(int i=0;i<attachments.size();i++){ CustomGallery gallery = attachments.get(i); multipartTypedOutput.addPart("f[]",new TypedFile("image/jpg",new File(gallery.sdcardPath))); } ServicesAdapter.getAuthorizeService().createPostWithAttachments(multipartTypedOutput, new Callback<String>() { @Override public void success(String s, Response response) { DBLogin.updateCookie(response); new_post_text.setText(""); try { JSONObject json_response = new JSONObject(s); Toast.makeText(getApplicationContext(), json_response.getString("message"), Toast.LENGTH_LONG).show(); if (json_response.getString("status").equals("success")) { JSONObject dataObj = json_response.getJSONObject("data"); Intent intent = new Intent(getApplicationContext(), MainActivity.class); startActivity(intent); finish(); } else { Log.d(TAG, "Request failed"); } } catch (Exception e) { Log.d(TAG, e.getMessage()); } } @Override public void failure(RetrofitError retrofitError) { Toast.makeText(getApplicationContext(), retrofitError.getMessage(), Toast.LENGTH_LONG).show(); } }); 

也许这个解决scheme不是很好,但希望能帮助别人。

如果有什么更好的解决scheme请提出,谢谢:D

更新

在Retrofit 2.0.0-beta1中, MultipartTypedOutput不再存在

对于想要上传多个图片的用户,现在可以使用@PartMap ,引用链接javadoc

 //We need to create the Typed file array as follow and add the images path in the arrays list. private ArrayList<TypedFile> images; private void postService(final Context context) { Utils.progressDialog(context); MultipartTypedOutput multipartTypedOutput = new MultipartTypedOutput(); multipartTypedOutput.addPart("user_id",new TypedString(strUserId)); multipartTypedOutput.addPart("title", new TypedString(strJobtitle)); multipartTypedOutput.addPart("description", new TypedString( strJobdescription)); multipartTypedOutput.addPart("experience", new TypedString( strUserExperience)); multipartTypedOutput.addPart("category_id", new TypedString( strPostCategory)); multipartTypedOutput.addPart("city_id", new TypedString(strCityCode)); multipartTypedOutput.addPart("country_id", new TypedString( strCountryCode)); multipartTypedOutput.addPart("profile_doc",new TypedFile("multipart/form-data", postCurriculamFile)); for (int i = 0; i < images.size(); i++) { multipartTypedOutput.addPart("image[]", images.get(i)); } PostServiceClient.getInstance().postServiceData(multipartTypedOutput, new Callback<RegistrationResponsModel>() { @Override public void failure(RetrofitError retrofitError) { Logger.logMessage("fail" + retrofitError); Utils.progressDialogdismiss(context); } @Override public void success( RegistrationResponsModel regProfileResponse, Response arg1) { Utils.progressDialogdismiss(context); UserResponse(regProfileResponse); } }); } @POST("/service/update") // annotation used to post the data void postEditServiceData(@Body MultipartTypedOutput attachments, Callback<RegistrationResponsModel> callback); 

//这是我们可以发布文件的方式multipartTypedOutput.addPart(“profile_doc”,new TypedFile(“multipart / form-data”,postCurriculamFile));

//这是我们可以发布多个图像的方式

  for (int i = 0; i < images.size(); i++) { multipartTypedOutput.addPart("image[]", images.get(i)); }