使用翻新2上传文件时出错 [英] Error uploading a file using Retrofit 2

查看:54
本文介绍了使用翻新2上传文件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Retrofit 2 将文件(图片)上传到服务器.我正在关注教程,它看起来很漂亮刚开始很容易,但对我而言却不起作用...

I'm trying to upload a file (picture) to the server using Retrofit 2. I'm following that tutorial which seems pretty easy at first, but didn't work in my case...

当我调用API函数时,我总是会收到此错误:

When I call the API function, i'm always getting this error:

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

推荐答案

我使用以下链接解决了我的问题: https://github.com/square/retrofit/issues/1063#issuecomment-145920568

I fix my issue using this link : https://github.com/square/retrofit/issues/1063#issuecomment-145920568

这是问题的解决方案:

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

我通过以下方式称呼它.

I call it by following way.

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

The method toRequestBody just converts String into RequestBody

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

这篇关于使用翻新2上传文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆