How to update user profile text fields and profile photo at the same time on Android using Retrofit 2

I am using Retrofit 2. I am trying to implement the "PATCH" method to update the profile photo on the main topic. I also tried Postman.I can update my profile photo.

PATCH request success

This is my interface when I use calls. Also UpdateUser is a model 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 am processing the request here.

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

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

I get a user from 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'm updating 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);

        }
    });

In my UpdateProfile Activity.I get the user using the methods My presenter. Then I also update here. But I don’t understand how to update my profile photo here.

 @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 the interface to implement the methods that I use in the main thread

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);
+4
1

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))
+1

Source: https://habr.com/ru/post/1693349/


All Articles