【问题标题】:Image Upload using Retrofit in Android在 Android 中使用 Retrofit 上传图片
【发布时间】:2020-05-31 20:18:04
【问题描述】:

我正在尝试使用改造将图像上传到网络服务器。

API 是使用 Dot Net 开发的。 API 在 postman 上运行良好,但在 android 中显示错误。

URL: https://m13tp89pn9.execute-api.us-west-2.amazonaws.com/Prod/api/user/UploadImag/userid=67

Authorization = Bearer token

这是我的界面

@Headers("Content-Type: multipart/form-data")
@Multipart
@POST("user/UploadImage")
Call<GeneralPOJO> uploadProfileImage(@Query ("userid") int uid,
                                     @Part MultipartBody.Part image, @Header("Authorization")String header);

这是我的代码

    final String path = getPathFromURI(selectedImageUri);
                    if (path != null) {
                        File file = new File(path);

   RequestBody mFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
                        MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("file", file.getAbsolutePath(), mFile);
                        RequestBody filename = RequestBody.create(MediaType.parse("text/plain"), file.getName());


new RestCaller(ProfileActivity.this, MainApplication.getRestClient().getApiServices().
                                    uploadProfileImage(AppSession.getInt(Constants.LOGIN_UDID), fileToUpload, AppSession.get(Constants.LOGIN_TOKEN)), Constants.REQUEST_CODE_PROFILE_PICTURE);

请帮助我将图像成功上传到服务器。请更正我的代码并告诉我需要进行哪些更改。谢谢

【问题讨论】:

标签: android rest android-studio retrofit2 retrofit2.6


【解决方案1】:
// Interface

@Multipart
@POST("user/UploadImage/{userid}")
Call<GeneralPOJO> uploadProfileImage(@Path ("userid") int uid,
                                     @Part MultipartBody.Part image, @Header("Authorization")String header);

【讨论】:

  • 如果您能解释为什么这是我们想要教育的首选解决方案,而不仅仅是提供现成的代码,那就太好了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-14
  • 1970-01-01
  • 1970-01-01
  • 2015-02-22
  • 2016-04-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多