Android Retrofit2上传图片

1、单张图片上传

    private void setHttpPortrait(final String name, final String strPath) {//name:参数名称;strPath:图片路径
        File file = new File(strPath);
        RequestBody imageBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part imageBodyPart = MultipartBody.Part.createFormData(name, file.getName(), imageBody);
        Call<PictureBean> repos = HttpUtils.setRetrofit().setHttpPortrait(imageBodyPart);
        repos.enqueue(new Callback<PictureBean>() {
            @Override
            public void onResponse(Call<PictureBean> call, Response<PictureBean> response) {
                try {
                    if (response.body().isOk()) {//请求成功
                        //返回数据处理
                    } else {
                        //图片上传失败
                    }
                } catch (Exception e) {
                    //返回数据异常
                }
            }

            @Override
            public void onFailure(Call<PictureBean> call, Throwable t) {
                //请求异常
            }
        });
    }


    @Multipart
    @POST("home/upload-image")
    Call<PictureBean> setHttpPortrait(@Part MultipartBody.Part part);

2、带参数图片上传

    private void setHttpPortrait(final String name, final String strPath) {
        File file = new File(strPath);
        RequestBody imageBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part imageBodyPart = MultipartBody.Part.createFormData(name, file.getName(), imageBody);
        MultipartBody.Part txtBodyPart = MultipartBody.Part.createFormData("id", "参数id");

        Call<PictureBean> repos = HttpUtils.setRetrofit().setHttpPortrait(txtBodyPart, imageBodyPart);
        repos.enqueue(new Callback<PictureBean>() {
            @Override
            public void onResponse(Call<PictureBean> call, Response<PictureBean> response) {
                try {
                    if (response.body().isOk()) {//请求成功
                        //返回数据处理
                    } else {
                        //图片上传失败
                    }
                } catch (Exception e) {
                    //返回数据异常
                }
            }

            @Override
            public void onFailure(Call<PictureBean> call, Throwable t) {
                //请求异常
            }
        });
    }


    @Multipart
    @POST("home/upload-image")
    Call<PictureBean> setHttpPortrait(@Part MultipartBody.Part id, @Part MultipartBody.Part part);

3、多张图片上传

    private void setHttpPortrait(final String name, final List<String> paths) {
        showLoading();
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        for (String path : paths) {
            File file = new File(path);
            RequestBody requestBody = RequestBody.create(MediaType.parse("text/x-markdown; charset=utf-8"), file);
            builder.addFormDataPart("files", file.getName(), requestBody);
        }
        List parts = builder.build().parts();

        Call<PictureBean> repos = HttpUtils.setRetrofit().setHttpDataPortrait(parts);
        repos.enqueue(new Callback<PictureBean>() {
            @Override
            public void onResponse(Call<PictureBean> call, Response<PictureBean> response) {
                try {
                    if (response.body().isOk()) {
                        //返回数据处理
                    } else {
                        //图片上传失败
                    }
                } catch (Exception e) {
                    //返回数据异常
                }
            }

            @Override
            public void onFailure(Call<PictureBean> call, Throwable t) {
                //请求异常
            }
        });
    }


    @Multipart
    @POST("home/upload-image")//多张上传图片
    Call<PictureBean> setHttpDataPortrait(@Part List file);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

c小旭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值