Raturofit 2 Multipart文件上传错误



尝试使用raterofit 2和rxjava通过API调用来上传配置文件图片。响应总是给我错误的错误,即422个无法处理的实体。给出我的代码和需要的建议来克服这一点。

@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

您应该通过 @Part MultipartBody.Part profile_picture而不是请求body进行改造接口。

您需要添加标题:

接受= */*

例如:

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);
    }
}

相关内容

  • 没有找到相关文章

最新更新