简体   繁体   English

如何使用Retrofit 2在Android上同时更新用户个人资料文本字段和个人资料照片

[英]How to update user Profile textfields and profile photo same time on Android Using Retrofit 2

I'am using Retrofit 2.I'am trying to implement "PATCH" method to updating profile photo on main thread.I also tried on Postman.I can update the profile photo. 我正在使用Retrofit2。我正在尝试实现“ PATCH”方法来更新主线程上的个人资料照片。我也在Postman上尝试过。我可以更新个人资料照片。

PATCH request success PATCH请求成功

This is my interface when I use calls.Also UpdateUser is the model w 这是我使用呼叫时的界面。UpdateUser也是模型w

 @PATCH("user/{username}/")
Call<User> updateUserProfile(@Path("username") String username, @Body UpdateUser User);

This is my User Model 这是我的用户模型

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;
}

This is my ProfileUpdatePresenter I handle request here 这是我的ProfileUpdatePresenter,我在这里处理要求

   public class ProfileUpdatePresenter
  {
  private ProfileUpdateView profileView;
  private ApiInterface apiInterface =           
  MyAPIClient.getClient().create(ApiInterface.class);

  public ProfileUpdatePresenter(ProfileUpdateView profileView) {
    this.profileView = profileView;
  }

I get user on this method 我得到了这种方法的用户

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);
        }
    });
}

I update User here 我在这里更新用户

 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);

        }
    });

On my UpdateProfile Activity.I get the user by implementing My presenter's methods.Then also I update here.But I'am confused on how to update my profile photo here. 在我的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)
 }

I also use interface to implement methods that I use on main thread 我还使用接口来实现在主线程上使用的方法

public interface ProfileUpdateView {
void onProfileGet(@NotNull User user);
void onUpdateSucceed(@NotNull User user);

} }


To be honest The main Problem for me is how to Use My Old @PATCH for updating the profile photo.Basically How to combine these on one Call? 说实话我的主要问题是如何使用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);

If you can modify server's API to @POST + Multipart then you can do following: 如果可以将服务器的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM