zoukankan      html  css  js  c++  java
  • 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);
        }
    

      

  • 相关阅读:
    hihocoder #1467 : 2-SAT·hihoCoder音乐节 2-SAT
    hihoCoder#1185 : 连通性·三 tarjan求强联通分量 缩点 dfs/拓扑排序求路径和最大值
    hihoCoder1175 拓扑排序·二 拓扑排序
    012 列表的一些常用操作符
    011,列表2
    010 列表1
    009,分支和循环3
    008,分支和循环2
    006 Python的操作符
    005 Python的数值类型
  • 原文地址:https://www.cnblogs.com/freexiaoyu/p/6079547.html
Copyright © 2011-2022 走看看