首页 > 代码库 > retrofit2 上传图片

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 上传图片