【发布时间】:2018-02-08 09:04:44
【问题描述】:
我正在使用 Retrofit 2。我正在尝试实现“PATCH”方法来更新主线程上的个人资料照片。我也尝试过 Postman。我可以更新个人资料照片。
PATCH request success
这是我使用调用时的界面。UpdateUser 也是 w 的模型
@PATCH("user/{username}/")
Call<User> updateUserProfile(@Path("username") String username, @Body UpdateUser User);
这是我的用户模型
public class User {
@SerializedName("username")
@Expose
private String username;
@SerializedName("first_name")
@Expose
private String firstName;
@SerializedName("last_name")
@Expose
private String lastName;
@SerializedName("profile_photo")
@Expose
private String profilePhoto;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getProfilePhoto() {
return profilePhoto;
}
public void setProfilePhoto(String profilePhoto) {
this.profilePhoto = profilePhoto;
}
这是我的 ProfileUpdatePresenter 我在这里处理请求
public class ProfileUpdatePresenter
{
private ProfileUpdateView profileView;
private ApiInterface apiInterface =
MyAPIClient.getClient().create(ApiInterface.class);
public ProfileUpdatePresenter(ProfileUpdateView profileView) {
this.profileView = profileView;
}
我让用户使用这个方法
public void getUserProfile(Activity activity, String username) {
apiInterface.getUserProfile(username).enqueue(new CustomCallBack<User>(activity) {
@Override
public void onResponse(@NonNull Call<User> call, @NonNull Response<User> response) {
super.onResponse(call,response);
if(response.isSuccessful()) {
profileView.onProfileGet(response.body());
}
}
@Override
public void onFailure(@NonNull Call<User> call, @NonNull Throwable t) {
super.onFailure(call,t);
}
});
}
我在这里更新用户
public void updateUserProfile(Activity activity, String Username, UpdateUser User){
apiInterface.updateUserProfile(Username,User).enqueue(new CustomCallBack<User>(activity) {
@Override
public void onResponse(@NonNull Call<User> call, @NonNull Response<User> response) {
super.onResponse(call,response);
if(response.isSuccessful()) {
profileView.onUpdateSucceed(response.body());
}
}
@Override
public void onFailure(@NonNull Call<User> call, @NonNull Throwable t) {
super.onFailure(call,t);
}
});
在我的 UpdateProfile 活动中。我通过实现我的演示者的方法来获取用户。然后我也在这里更新。但是我对如何在这里更新我的个人资料照片感到困惑。
@Override
public void onProfileGet(@NotNull User user) {
UserProfile = user;
if(UserProfile.getFirstName() != null)
et_name.setText(UserProfile.getFirstName());
if(UserProfile.getLastName() != null)
et_surname.setText(UserProfile.getLastName());
if(UserProfile.getPhone() != null)
}
我也使用接口来实现我在主线程上使用的方法
public interface ProfileUpdateView {
void onProfileGet(@NotNull User user);
void onUpdateSucceed(@NotNull User user);
}
说实话,我的主要问题是如何使用我的旧@PATCH 来更新个人资料照片。基本上如何在一次通话中结合这些?
@PATCH("user/{username}/")
Call<User> updateUserProfile(@Path("username") String username, @Body UpdateUser User);
+
@Multipart
@PATCH("/retrofit_tutorial/retrofit_client.php")
Call<ServerResponse> uploadFile(@Part MultipartBody.Part file, @Part("file") RequestBody name);
【问题讨论】:
-
你能像在 POST 方法中一样尝试使用 @Multipart 的 PATCH 方法吗?
-
@ChetakBhimani 我编辑了代码。问题是如何处理将默认方法与多部分相结合的补丁方法。
-
@ChetakBhimani 我也可以用 updateUserProfile 更新个人资料照片吗?
-
@ChetakBhimani 我的目标是发送一个请求。谢谢...
标签: java android retrofit rx-java retrofit2