retrofit2 上传图片

Posted 今天的付出等于明天的收获

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了retrofit2 上传图片相关的知识,希望对你有一定的参考价值。

直接上代码

接口请求代码

NewResultApi.modifyUserInfo(userModel.getUid(), userModel.getToken(), sex, intro,
                name, city, birthday,new File(filename), new ICallBack<BaseResultBean<UserDetails>>() {
                    @Override
                    public void onSuccess(String flag, String key, BaseResultBean<UserDetails> userDetailsBaseResultBean) {
                        UserDateilsTable.getUserDao(context).saveContactList(
                                userDetailsBaseResultBean.getResult(), "1");
                        ToastUtil.show(context, getString(R.string.toast_save_succeed));
                        UserInfoActivity.this.finish();
                        pd.dismiss();
                    }

                    @Override
                    public void onFailure(String flag, String key, String why) {
                        pd.dismiss();
                        ToastUtil.show(context, why);
                    }
                });

  

public static Call<BaseResultBean<UserDetails>> modifyUserInfo(int uid, String token, String sex, String intro, String username, String city,
                                                                   String birthday, File imagePath, final ICallBack<BaseResultBean<UserDetails>> callBack){



        MultipartBody.Part filePart = MultipartBody.Part.
                createFormData("icon", imagePath.getName(), RequestBody.create(MediaType.parse("image/*"), imagePath));
        Call<BaseResultBean<UserDetails>> call=BuildService.getMeiNvService().modifyUserInfo(stringRequestBody(AppConfig.VER+""),
                stringRequestBody("user_modify_userinfo"),
                stringRequestBody(uid+""),stringRequestBody(token),stringRequestBody(sex),stringRequestBody(intro),stringRequestBody(username),
                stringRequestBody(city),stringRequestBody(birthday), filePart);
        call.enqueue(new Callback<BaseResultBean<UserDetails>>() {
            @Override
            public void onResponse(Call<BaseResultBean<UserDetails>> call, Response<BaseResultBean<UserDetails>> response) {
                if (response.isSuccessful()) {
                    BaseResultBean<UserDetails> resultBean = response.body();
                    if (resultBean.getCode() == 200) {
                        //数据正确,把数据返回
                        callBack.onSuccess("", "", resultBean);
                    } else {
                        //数据错误
                        callBack.onFailure("", "", resultBean.getMsg());
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseResultBean<UserDetails>> call, Throwable t) {
                callBack.onFailure("", "", "失败");
            }
        });
        return  call;
    };

  

 @Multipart
    @POST("api.ashx")
    Call<BaseResultBean<UserDetails>> modifyUserInfo(@Part("ver") RequestBody ver,
                                                     @Part("action") RequestBody action,
                                                     @Part("uid") RequestBody uid,
                                                     @Part("token") RequestBody token,
                                                     @Part("sex") RequestBody sex,
                                                     @Part("intro") RequestBody intro,
                                                     @Part("username") RequestBody username,
                                                     @Part("city") RequestBody city,
                                                     @Part("birthday") RequestBody birthday,
                                                     @Part MultipartBody.Part  file);

  

    private static RequestBody stringRequestBody(String params){
        return  RequestBody.create(MediaType.parse("text/plain"), params);
    }

  

以上是关于retrofit2 上传图片的主要内容,如果未能解决你的问题,请参考以下文章

QT QHttpMultiPart上传图片

android 使用Retrofit2 RxJava 文件上传

使用retrofit2视频/图像损坏的AWS S3文件上传

如何上传图片到七牛云

使用Retrofit2解析XML。多个结果列表不起作用

使用 AFNetworking、ios 上传图片