簡體   English   中英

使用Retrofit 2上傳文件時出錯

[英]Error uploading a file using Retrofit 2

我正在嘗試使用Retrofit 2將文件(圖片)上傳到服務器。 我正在關注那個起初看起來很簡單的教程 ,但在我的情況下不起作用......

當我調用API函數時,我總是收到此錯誤:

W/System.err: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
W/System.err:     at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:190)
W/System.err:     at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:166)
W/System.err:     at retrofit2.Retrofit$1.invoke(Retrofit.java:145)
W/System.err:     at java.lang.reflect.Proxy.invoke(Proxy.java:393)
W/System.err:     at com.plante.android.cobalt.fragment.FragmentIncidentPlan.uploadFile(FragmentIncidentPlan.java:575)

這是我的API調用:

@Multipart
@POST(Constants.URL_UPLOAD)
Call<ResponseBody> upload(@Part RequestBody description,
                          @Part MultipartBody.Part file);

這是我用來上傳文件的方法:

private void uploadFile(String path) {
    // create upload service client

    // use the FileUtils to get the actual file by uri
    File file = new File(path);
    Log.e(TAG, file.getAbsolutePath());

    // create RequestBody instance from file
    RequestBody requestFile =
            RequestBody.create(MediaType.parse("multipart/form-data"), file);

    // MultipartBody.Part is used to send also the actual file name
    MultipartBody.Part body =
            MultipartBody.Part.createFormData("picture", file.getName(), requestFile);

    // add another part within the multipart request
    String descriptionString = "hello, this is description speaking";
    RequestBody description =
            RequestBody.create(
                    MediaType.parse("multipart/form-data"), descriptionString);

    // finally, execute the request
    Call<ResponseBody> call = cobaltServices.upload(description, body);
    call.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call,
                               Response<ResponseBody> response) {
            Log.v("Upload", "success");
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            Log.e("Upload error:", t.getMessage());
        }
    });
}

Exceptions表示第一個@Part在注釋中不需要名稱。

@Multipart
@POST(Constants.URL_UPLOAD)
Call<ResponseBody> upload(@Part RequestBody description,
                          @Part MultipartBody.Part file);

我使用此鏈接解決了我的問題: https//github.com/square/retrofit/issues/1063#issuecomment-145920568

這是問題的解決方案:

@Multipart
@POST ("/api/Events/editevent")
Call<Event> editEvent (@PartMap Map<String, RequestBody> params);

我通過以下方式稱呼它。

Map<String, RequestBody> map = new HashMap<>();
map.put("Id", AZUtils.toRequestBody(eventId));
map.put("Name", AZUtils.toRequestBody(titleView.getValue()));

if (imageUri != null) {
      File file = new File(imageUri.getPath());
      RequestBody fileBody = RequestBody.create(MediaType.parse("image/png"), file);
      map.put("file\"; filename=\"pp.png\"", fileBody);
}

Call<Event> call = api.editEvent(map);
call.enqueue(new Callback<Event>() { }

toRequestBody方法只是將String轉換為RequestBody

public static RequestBody toRequestBody (String value) {
    RequestBody body = RequestBody.create(MediaType.parse("text/plain"), value);
    return body ;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM