直接上代码
接口请求代码
NewResultApi.modifyUserInfo(userModel.getUid(), userModel.getToken(), sex, intro, name, city, birthday,new File(filename), new ICallBack>() { @Override public void onSuccess(String flag, String key, BaseResultBean 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> modifyUserInfo(int uid, String token, String sex, String intro, String username, String city, String birthday, File imagePath, final ICallBack > callBack){ MultipartBody.Part filePart = MultipartBody.Part. createFormData("icon", imagePath.getName(), RequestBody.create(MediaType.parse("image/*"), imagePath)); Call > 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 >() { @Override public void onResponse(Call > call, Response > response) { if (response.isSuccessful()) { BaseResultBean resultBean = response.body(); if (resultBean.getCode() == 200) { //数据正确,把数据返回 callBack.onSuccess("", "", resultBean); } else { //数据错误 callBack.onFailure("", "", resultBean.getMsg()); } } } @Override public void onFailure(Call > call, Throwable t) { callBack.onFailure("", "", "失败"); } }); return call; };
@Multipart @POST("api.ashx") Call> 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); }