简体   繁体   English

在 Android 中使用 Retrofit 上传图片

[英]Image Upload using Retrofit in Android

I am trying to upload an image to the Webserver using retrofit.我正在尝试使用改造将图像上传到网络服务器。

The API has been developed using Dot Net. API 是使用 Dot Net 开发的。 The API is working fine on postman but shows error in android. API 在邮递员上运行良好,但在 android 中显示错误。

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

Authorization = Bearer token

This is my interface这是我的界面

@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);

This is my Code这是我的代码

    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);

Please help me in uploading the image to the server successfully.请帮助我成功将图像上传到服务器。 Please correct my code and tell me what change is required.请更正我的代码并告诉我需要进行哪些更改。 Thanks谢谢

// Interface

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM