Java 如何使用改型2在Android上同时更新用户配置文件文本字段和配置文件照片
我正在使用改型2。我正在尝试实现“补丁”方法来更新主线程上的配置文件照片。我还尝试了邮递员。我可以更新配置文件照片 这是我使用调用时的接口。UpdateUser也是模型wJava 如何使用改型2在Android上同时更新用户配置文件文本字段和配置文件照片,java,android,retrofit,rx-java,retrofit2,Java,Android,Retrofit,Rx Java,Retrofit2,我正在使用改型2。我正在尝试实现“补丁”方法来更新主线程上的配置文件照片。我还尝试了邮递员。我可以更新配置文件照片 这是我使用调用时的接口。UpdateUser也是模型w @PATCH("user/{username}/") Call<User> updateUserProfile(@Path("username") String username, @Body UpdateUser User); 这是我的ProfileUpdatePresenter我在这里处理请求 pu
@PATCH("user/{username}/")
Call<User> updateUserProfile(@Path("username") String username, @Body UpdateUser User);
这是我的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 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);
@PATCH(“user/{username}/”)
调用updateUserProfile(@Path(“username”)字符串username,@Body UpdateUser User);
+
@多部分
@补丁(“/reformation\u tutorial/reformation\u client.php”)
调用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))
@Multipart
@POST(“用户/{username}/”)
调用updateUserProfile(
@路径(“用户名”)字符串用户名,
@部分多部件主体。部分照片,
@部分(“json_用户”)字符串jsonUser
);
//请求的一些UTIL
公共MultipartBody.partfiletopart(文件){
返回MultipartBody.Part.createFormData(
“photo”,//参数名称
file.getName(),
create(MediaType.parse(“image/*”)文件)
);
}
//用法
updateUserProfile(“Dude”,fileToPart(myFile),new Gson().toJson(myUpdateUser))
你能像在POST方法中一样尝试使用@Multipart的补丁方法吗。@ChetakBhimani我编辑了代码。问题是如何将默认的补丁方法与Multipart结合起来。@ChetakBhimani也能用updateUserProfile更新个人资料照片吗?@ChetakBhimani我的目标是发送一个请求。谢谢你……你能解释一下吗请提供更多信息。api必须有哪些更改。如果没有更改,我就不能这样做。因为示例服务器可能会将@GET(“user/{username}/”)和@POST(“user/{username}/”)处理为两个不同的请求以及@PATCH。我的意思是你应该和你的后端开发者联系,讨论一下请求格式。
public interface ProfileUpdateView {
void onProfileGet(@NotNull User user);
void onUpdateSucceed(@NotNull User user);
@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);
@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))