Retrofit 上传图片(多张)

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

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

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

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

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

    @Multipart
    @POST("Comm/uploadImg")
    Call<UploadImgResponse> uploadImg(@PartMap Map<String, RequestBody> params);
}
使用:
<pre name="code" class="java">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<String> imgStrs) {
        Map<String, RequestBody> 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<UploadImgResponse> call = service.uploadImg(params);
        call.enqueue(new Callback<UploadImgResponse>() {
            @Override
            public void onResponse(Call<UploadImgResponse> call, Response<UploadImgResponse> 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<UploadImgResponse> call, Throwable e) {
                ToastUtil.toast(context, context.getString(R.string.connect_server_error) + e.getMessage());

            }
        });

    }
}
 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值