有没有办法在一个多部分请求中附加多个图像?图片是动态的,基于用户选择的图片数量。
以下代码仅适用于单个图像:
界面:
@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();
}
});
最佳答案
查看改造提供的文档后.. 我能够通过我自己的解决方案完成它,也许不是那么好,但仍然设法让它工作..
这是引用 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();
}
});
也许这个解决方案不是很好,但希望对其他人有所帮助。
如果有更好的解决方法请指教,谢谢:D
更新
MultipartTypedOutput 在 Retrofit 2.0.0-beta1 中不再存在
想要上传多张图片的 friend 现在可以使用@PartMap,引用链接javadoc
关于android - "Retrofit"多个图像附加在一个多部分请求中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25249042/