【问题标题】:How to update user Profile textfields and profile photo same time on Android Using Retrofit 2如何使用 Retrofit 2 在 Android 上同时更新用户个人资料文本字段和个人资料照片
【发布时间】: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


【解决方案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))

【讨论】:

  • 你能解释一下吗。api必须有什么变化。我不能没有变化就完成
  • 例如服务器可能会处理 @GET("user/{username}/")@POST("user/{username}/") 作为两个不同的请求以及 @PATCH。我的意思是您应该联系您的后端开发人员并讨论请求格式。
猜你喜欢
  • 1970-01-01
  • 2014-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-13
相关资源
最近更新 更多