码迷,mamicode.com
首页 > Web开发 > 详细

retrofit2 上传图片

时间:2016-11-19 02:35:02      阅读:335      评论:0      收藏:0      [点我收藏+]

标签:context   void   cee   cal   request   gets   tip   数据   ica   

直接上代码

接口请求代码

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

标签:context   void   cee   cal   request   gets   tip   数据   ica   

原文地址:http://www.cnblogs.com/freexiaoyu/p/6079547.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!