I'm using Retrofit in my Android application to communicate with a REST-API.
When user changes his profile picture in my application, I need to send a request and upload new image. This is my service:
@Multipart
@PATCH("/api/users/{username}/")
Call<User> changeUserPhoto(@Header("Authorization") String token,@Path("username") String userName , @Part("photo") RequestBody photo);
And this is my code to send request:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(GlobalVars.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
UserService userService = retrofit.create(UserService.class);
Callback<User> callback = new Callback<User>() {
@Override
public void onResponse(Response<User> response, Retrofit retrofit) {
if (response.isSuccess()) {
//do sth
} else {
// do sth else
}
}
@Override
public void onFailure(Throwable t) {
t.printStackTrace();
}
};
RequestBody photo = RequestBody.create(MediaType.parse("application/image"), new File(imageUir));
Call<User> call = userService.changeUserPhoto(token, username, photo);
call.enqueue(callback);
But when I send this request to server, REST keeps telling me that photo is not a file and something is wrong with encoding type.
Can anybody help me how to fix this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…