2017-10-04 18 views
0

Retrofit 2とrxjavaを使用してapi呼び出しでプロフィール画像をアップロードしようとしています。応答は常に私に422 Unprocessable Entityというエラーを与えます。私のコードを与え、これを克服するための提案が必要です。Retrofit 2 Multipart file uploadエラー

@Multipart 
@POST("api/update-profile-picture") 
Observable<ProfilePicture> updateProfilePicture(
     @Header("Authorization") String accessToken, 
     @Part("profile_picture") RequestBody profile_picture 
); 


// calling presenter method to update profile picture 
public void updateProfilePictureImage(File file){ 
    getMvpView().showProgress(); 

    RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"), file); 
    //MultipartBody.Part body = MultipartBody.Part.createFormData("profile_picture", file.getName(), reqFile); 

    oyeBuddyService.updateProfilePicture("Bearer" + " " + mPrefs.getOyeUserAccessToken(), reqFile) 
      .subscribeOn(mNewThread) 
      .observeOn(mMainThread) 
      .subscribe(new Observer<ProfilePicture>() { 
       @Override 
       public void onCompleted() { 
        getMvpView().hideProgress(); 
       } 

       @Override 
       public void onError(Throwable e) { 
        getMvpView().hideProgress(); 
        Log.e("error: ",e.getMessage()); 
       } 

       @Override 
       public void onNext(ProfilePicture profilePicture) { 
        Log.e("response: ", profilePicture.toString()); 
        getMvpView().onProfilePictureUpdated(profilePicture.profile_picture_url); 
       } 
      }); 
} 

レスポンス--->

RetrofitModule: Received response for http://174.138.64.95/api/update-profile-picture in 4540.6ms 
                       Date: Wed, 04 Oct 2017 10:30:53 GMT 
                       Server: Apache/2.4.18 (Ubuntu) 
                       Vary: Authorization 
                       Cache-Control: no-cache, private 
                       X-RateLimit-Limit: 60 
                       X-RateLimit-Remaining: 59 
                       Content-Length: 99 
                       Keep-Alive: timeout=5, max=99 
                       Connection: Keep-Alive 
                       Content-Type: application/json 

E/error:: HTTP 422 Unprocessable Entity 
+0

これは、U https://github.com/square/retrofit/issuesを助けるかもしれません/ 696 – Anil

+0

@Anilありがとうございます。それは –

答えて

0

あなたは、インターフェイスを改造する代わりにRequestBodyの@Part MultipartBody.Part profile_pictureを渡す必要があります。あなたが好きなヘッダを追加する必要が

+0

マルチパート POST( "API /更新プロファイルピクチャ") 観測可能な updateProfilePicture( ヘッダー( "認可")文字列accessToken、 パートMultipartBody.Partのprofile_picture を)助けにはなりませんでした。 両方の方法で試しました。同じ結果。 –

+0

正確なMIMEタイプ(アスタリスクなし)を使用し、ファイル名が正しいことを確認する方法はありますか?リクエストのインスペクタ(Stethoなど)を使用してリクエストがOKであることを確認できますか?同様のコードは私のためにうまく動作します。 – etan

+0

それは私のポイントです。私は直接 "multipart/form-data"を使用しましたが、同じ応答です。私はstethoを介してそれをチェックし、あなたに知らせるでしょう –

0

は、例えば= */*

を受け入れ:

httpClient.addInterceptor(new Interceptor() { 
    @Override 
    public Response intercept(Interceptor.Chain chain) throws IOException { 
     Request original = chain.request(); 

     Request request = original.newBuilder() 
      .header("Accept", "*/*") 
      .method(original.method(), original.body()) 
      .build(); 

     return chain.proceed(request); 
    } 
} 
+0

正確に私は余分なヘッダーを追加する必要がありますか? –

+0

@demo_Ashifリクエストに複数のデータがあるので、サーバーがリクエストタイプを認識しない可能性があります。plsが追加して試してみます。 –

+0

あなたは間違っています。私は既にタイプを追加していますし、サーバはデータについて知っています。 –