[英]How to update user Profile textfields and profile photo same time on Android Using Retrofit 2
我正在使用Retrofit2。我正在尝试实现“ PATCH”方法来更新主线程上的个人资料照片。我也在Postman上尝试过。我可以更新个人资料照片。
这是我使用呼叫时的界面。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);
}
说实话我的主要问题是如何使用My Old @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);
如果可以将服务器的API修改为@POST + Multipart,则可以执行以下操作:
@Multipart
@POST("user/{username}/")
Call<User> updateUserProfile(
@Path("username") String username,
@Part MultipartBody.Part photo,
@Part("json_user") String jsonUser
);
// Some Utils for request
public MultipartBody.Part fileToPart(File file) {
return MultipartBody.Part.createFormData(
"photo", // param name
file.getName(),
RequestBody.create(MediaType.parse("image/*"), file)
);
}
// usage
updateUserProfile("Dude", fileToPart(myFile), new Gson().toJson(myUpdateUser))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.