如何使用retrofit2上传图像文件?

huangapple go评论146阅读模式
英文:

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(&quot;Content-type:application/json&quot;)
@POST(&quot;upload/profile&quot;)
Call&lt;JsonObject&gt; 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 &amp;&amp; requestCode == 100) {
        if (data != null) {
            Uri uri = data.getData();
            uploadImageToServer(uri);
        }
    }
}

private void uploadImageToServer(Uri uri) {
    MultipartBody.Part partMap = prepareFilePart(&quot;file&quot;, uri);
    Call&lt;JsonObject&gt; changeImage = baseServiceAPI.changePhotoProfile(partMap);
    changeImage.enqueue(new Callback&lt;JsonObject&gt;() {
        @Override
        public void onResponse(Call&lt;JsonObject&gt; call, Response&lt;JsonObject&gt; response) {
            
        }

        @Override
        public void onFailure(Call&lt;JsonObject&gt; 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);
}

如何使用retrofit2上传图像文件?

答案1

得分: 1

你可以尝试不使用 @Headers("Content-type:application/json"),就像下面这样:

@Multipart
@POST("upload/profile")
Call<JsonObject> changePhotoProfile(@Part MultipartBody.Part partFile);

它应该可以工作。

英文:

You can try it without @Headers(&quot;Content-type:application/json&quot;)

like

@Multipart
@POST(&quot;upload/profile&quot;)
Call&lt;JsonObject&gt; changePhotoProfile(@Part MultipartBody.Part partFile);

it should work

huangapple
  • 本文由 发表于 2020年8月13日 12:18:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63388067.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定