Android - 使用Multipart Retrofit上传图像失败或崩溃

时间:2017-02-24 03:42:04

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

我尝试使用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的响应,这让我很难找到错误。请帮我解决这个问题。感谢。

1 个答案:

答案 0 :(得分:0)

if (rank == master) {
  MPI_SendRand(some_data, sime_size, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
} else {
  // the rest waits
  MPI_Recv(some_buff, some_size, MPI_SOURCE_ANY, MPI_TAG_ANY, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  // do work...
}