Retrofit 上传图片(多张)

修改interface——ApiUploadImg,使用@PartMap注解:

public interface ApiUploadImg {
   /**图片上传API*/
  /*  @POST("Comm")
    Observable> uploadImg(@Body RequestApi RequestApi);*/

//   @Multipart
//   @POST("Comm/uploadImg")
//   Call uploadImage(/*@Part("fileName") String description,*/
//                            @Part("file\"; filename=\"image.png") RequestBody imgs);

//    @Multipart
//    @POST("Comm/uploadImg")
//    Observable> uploadImg( @Part("description") RequestBody description, @Part MultipartBody.Part file);

//    @Multipart
//    @POST("Comm/uploadImg")
//    Call uploadImg( @Part("description") RequestBody description, @Part MultipartBody.Part file);

    @Multipart
    @POST("Comm/uploadImg")
    Call uploadImg(@PartMap Map params);
}
使用:
package cn.suanzi.tcwl_manage.utils;

        import android.content.Context;
        import android.util.Log;

        import java.io.File;
        import java.util.HashMap;
        import java.util.List;
        import java.util.Map;

        import cn.suanzi.tc_base.api.RetrofitUtil;
        import cn.suanzi.tcwl_manage.R;
        import cn.suanzi.tcwl_manage.models.enties.response.UploadImgResponse;
        import cn.suanzi.tcwl_manage.models.services.ApiUploadImg;
        import okhttp3.MediaType;
        import okhttp3.RequestBody;
        import retrofit2.Call;
        import retrofit2.Callback;
        import retrofit2.Response;

/**
 * Created by MAOYH on 2016/4/20.
 * 上传图片
 */
public class UploadImgUtil  {
    private static String imgUrl;

    public static String getImgUrl() {
        return imgUrl;
    }

    public static void setImgUrl(String imgUrl) {
        UploadImgUtil.imgUrl = imgUrl;
    }

    public static void upLoadImg(final Context context ,List imgStrs) {
        Map params = new HashMap<>();
        for(String imgStr :imgStrs) {
            File file = new File(imgStr);
            // create RequestBody instance from file
            RequestBody  requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
            //一定要加("AttachmentKey\"; filename=\"" +,不然失败
            params.put("AttachmentKey\"; filename=\"" +file.getName(),requestFile);
        }

        // create upload service client
        ApiUploadImg service = RetrofitUtil.getInstance().getService(ApiUploadImg.class);

        // finally, execute the request
        Call call = service.uploadImg(params);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                if (null != response) {
                    int code = response.body().getCode();
                    imgUrl = response.body().getImgUrl();

                    switch (code) {
                        case 50000:
                            ToastUtil.toast(context, "上传图片成功");
                            break;
                        case 80020:
                            ToastUtil.toast(context, "上传图片失败,图片格式不正确");
                            break;
                        case 80021:
                            ToastUtil.toast(context, "上传图片失败,图片大小不正确");
                            break;
                        case 80022:
                            ToastUtil.toast(context, "上传图片失败,没有上传的图片");
                            break;
                    }
                    Log.e("图片路径", "图片路径:" + imgUrl);


                } else {
                    ToastUtil.toast(context, context.getResources().getString(R.string.server_null));

                }

            }

            @Override
            public void onFailure(Call call, Throwable e) {
                ToastUtil.toast(context, context.getString(R.string.connect_server_error) + e.getMessage());

            }
        });

    }
}
 
  


你可能感兴趣的:(使用步骤)