2017-10-04 51 views
0

试图通过使用Retrofit 2和rxjava的api调用上传配置文件图片。响应总是给我错误,即422不可处理的实体。给我的代码,并需要建议,以克服这一点。翻新2多部分文件上传错误

@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

这可以帮助ühttps://github.com/square/retrofit/issues/696 – Anil

+0

@阿尼谢谢。它并没有帮助 –

回答

0

你应该通过@Part MultipartBody.Part profile_picture而不是RequestBody来更新接口。

+0

多部分 POST( “API /更新轮廓图象”) 可观察 updateProfilePicture( 报头( “授权”)字符串的accessToken, 部分MultipartBody.Part profile_picture ); 两种尝试。同样的结果。 –

+0

如何使用确切的MIME类型(不带星号)并检查您的文件名是否正确?您可以使用请求检查器(如Stetho)来查看请求是否正确?类似的代码对我来说工作得很好。 – 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在你的请求中有多部分数据,所以也许服务器不能识别请求类型,所以..请添加和尝试它。 –

+0

不是你错了,因为我已经添加了类型,服务器知道数据。 –

相关问题