2017-02-24 4 views
0

私はRetrofit Mulitpart Androidで画像をアップロードしようとしています。しかし、私はまだ私のアプリのエラーを取得します。Android - Multipart Retrofitで画像をアップロードできない、またはクラッシュする

これは

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からの応答もなくなりました。 dエラー。この問題を解決するのを手伝ってください。ありがとう。

+0

あなたはどのようなエラーが出るのですか? – yosriz

+0

apiからの応答が「ファイルが見つかりません」で、デバッガのcontentTypeがnullになっています。私はそれをテストする場合は "DocTyp01"を記入する必要があります。残りのクライアント@yosriz –

+0

エラーのログを添付してください。 – yosriz

答えて

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

私はRxJava Observableを使用しています。だから私のコードでは動かないだろう –

関連する問題