【问题标题】:Android - Uploading Image with Multipart Retrofit Failed or CrashAndroid - 使用 Multipart Retrofit 上传图像失败或崩溃
【发布时间】:2017-07-14 19:06:43
【问题描述】:

我尝试使用 Retrofit Mulitpart Android 制作上传图片。但我仍然收到我的应用程序错误。

这是获取图片的方法

 private final int SELECT_PHOTO = 1;

      private void startGallery() {
        Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
        photoPickerIntent.setType("image/png");
        startActivityForResult(photoPickerIntent, SELECT_PHOTO);
    }

这是 onActivityResult 和我请求上传图片的方式

 @Override
public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
    switch (requestCode) {
        case SELECT_PHOTO:
            if (resultCode == RESULT_OK) {
                selectedImage = imageReturnedIntent.getData();
                File file = new File(selectedImage.getPath());

                RequestBody reqFile = RequestBody.create(okhttp3.MediaType.parse("image/png"), file);

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

                getApi().uploadByCustomer(PrefHelper.getString(PrefKey.TOKEN), "DocTyp01", body)
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribeOn(Schedulers.io())
                        .subscribe(new Observer<GenericResponse>() {
                            @Override
                            public void onCompleted() {

                            }

                            @Override
                            public void onError(Throwable e) {
                               dismissProgressDialog();
                                Timber.e(e.getMessage());
                            }

                            @Override
                            public void onNext(GenericResponse response) {
                            dismissProgressDialog();
                                if (response.getCode() == 0) {
                                 showSuccessDialog("Saving success", false);
                                }

                            }
                        });
            }


            }
    }

这是 API 服务

    @Multipart
    @POST(PORTAL_URL + "customerDocument/uploadByCustomer")
    Observable<GenericResponse> uploadByCustomer(@Part("token") String token,
                                                 @Part("type") String type,
                                                 @Part MultipartBody.Part requestBodyFile);

而且有时日志没有显示错误,而且我没有从 API 得到响应,这让我很难找到错误。请帮我解决这个问题。谢谢。

【问题讨论】:

  • 你遇到了什么错误?
  • 来自 api 的响应是“找不到文件”,我在调试器上看到 contentType 为空。如果我在 Rest Client @yosriz 上测试它应该填写“DocTyp01”
  • 请附上错误日志

标签: android upload rx-java multipartform-data retrofit2


【解决方案1】:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
    switch (requestCode) {
        case SELECT_PHOTO:
            if (resultCode == RESULT_OK) {
                selectedImage = imageReturnedIntent.getData();
                File file = new File(selectedImage.getPath());
                RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
                MultipartBody.Part multipartBody = MultipartBody.Part.createFormData("file", file.getName(), requestFile);

                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl(URL)
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();

                RetroInterface retroInterface = retrofit.create(RetroInterface.class);
                Call<ResponseBody> call = retroInterface.uploadPhoto(token, multipartBody);
                call.enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                    }

                    @Override
                    public void onFailure(Call<ResponseBody> call, Throwable t) {
                        t.printStackTrace();
                    }
                });
            }
    }
}

【讨论】:

  • 我使用 RxJava Observable。所以它不适用于我的代码
猜你喜欢
  • 2017-09-28
  • 2016-04-25
  • 2017-02-22
  • 1970-01-01
  • 1970-01-01
  • 2021-12-02
  • 2020-12-05
  • 2015-11-17
  • 2019-10-24
相关资源
最近更新 更多