"改造"在一个多部分请求中附加的多个图像

Lou*_*Loo 23 android multipart retrofit

有没有办法在一个多部分请求中附加多个图像?根据用户选择的图像数量,图像是动态的.

下面的代码仅适用于单个图像:

接口:

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

执行:

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();
        }
    });
Run Code Online (Sandbox Code Playgroud)

Lou*_*Loo 32

看完改造后提供的文档.我能够通过我自己的解决方案完成它,也许不是那么好,但仍然设法让它工作..

这是参考 MultipartTypedOutput

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

接口

@POST("/post")
void createPostWithAttachments( @Body MultipartTypedOutput attachments,Callback<String> response);
Run Code Online (Sandbox Code Playgroud)

履行

    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();
        }
    });
Run Code Online (Sandbox Code Playgroud)

也许这个解决方案不是那么好,但希望它可以帮助其他人.

如果有更好的解决方案请建议,谢谢:D

更新

Retrofit 2.0.0-beta1中不再存在MultipartTypedOutput

对于那些想要上传多个图像的人,现在可以使用@PartMap,引用链接javadoc