簡體   English   中英

在 Android 中使用 Retrofit 上傳圖片

[英]Image Upload using Retrofit in Android

我正在嘗試使用改造將圖像上傳到網絡服務器。

API 是使用 Dot Net 開發的。 API 在郵遞員上運行良好,但在 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);

請幫助我成功將圖像上傳到服務器。 請更正我的代碼並告訴我需要進行哪些更改。 謝謝

// 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