英文:
How to upload an image file using retrofit2?
问题
I am trying to upload an image from an Android device to a server using Retrofit2 and end up getting the error '400 Bad Request'. Below is the implementation. Could somebody help to fix the error?
Service:
@Multipart
@Headers("Content-type: application/json")
@POST("upload/profile")
Call<JsonObject> changePhotoProfile(@Part MultipartBody.Part partFile);
Here is my code:
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == 100) {
if (data != null) {
Uri uri = data.getData();
uploadImageToServer(uri);
}
}
}
private void uploadImageToServer(Uri uri) {
MultipartBody.Part partMap = prepareFilePart("file", uri);
Call<JsonObject> changeImage = baseServiceAPI.changePhotoProfile(partMap);
changeImage.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
}
});
}
private MultipartBody.Part prepareFilePart(String partName, Uri uri) {
File file = FileUtils.getFile(ProfileActivity.this, uri);
RequestBody requestBody = RequestBody.create(MediaType.parse(Objects.requireNonNull(getContentResolver().getType(uri))), file);
return MultipartBody.Part.createFormData(partName, file.getName(), requestBody);
}
英文:
I am trying to upload an image from android device to server using Retrofit2 and end up getting error '400 Bad Request'. Below is the implementation. Could somebody help to fix the error?
Service :
@Multipart
@Headers("Content-type:application/json")
@POST("upload/profile")
Call<JsonObject> changePhotoProfile(@Part MultipartBody.Part partFile);
Here is my code :
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == 100) {
if (data != null) {
Uri uri = data.getData();
uploadImageToServer(uri);
}
}
}
private void uploadImageToServer(Uri uri) {
MultipartBody.Part partMap = prepareFilePart("file", uri);
Call<JsonObject> changeImage = baseServiceAPI.changePhotoProfile(partMap);
changeImage.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
}
});
}
private MultipartBody.Part prepareFilePart(String partName, Uri uri) {
File file = FileUtils.getFile(ProfileActivity.this, uri);
RequestBody requestBody = RequestBody.create(MediaType.parse(Objects.requireNonNull(getContentResolver().getType(uri))), file);
return MultipartBody.Part.createFormData(partName, file.getName(), requestBody);
}
答案1
得分: 1
你可以尝试不使用 @Headers("Content-type:application/json")
,就像下面这样:
@Multipart
@POST("upload/profile")
Call<JsonObject> changePhotoProfile(@Part MultipartBody.Part partFile);
它应该可以工作。
英文:
You can try it without @Headers("Content-type:application/json")
like
@Multipart
@POST("upload/profile")
Call<JsonObject> changePhotoProfile(@Part MultipartBody.Part partFile);
it should work
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论