最近使用Retrofit上传头像的时候报错
java.lang.IllegalArgumentException: @Part parameters can only be used with multipart encoding. (parameter #1)
for method UserService.updateAvatar
at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:755)
at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:746)
at retrofit2.ServiceMethod$Builder.parameterError(ServiceMethod.java:764)
at retrofit2.ServiceMethod$Builder.parseParameterAnnotation(ServiceMethod.java:595)
at retrofit2.ServiceMethod$Builder.parseParameter(ServiceMethod.java:339)
at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:207)
at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:170)
at retrofit2.Retrofit$1.invoke(Retrofit.java:147)
at java.lang.reflect.Proxy.invoke(Proxy.java:913)
at $Proxy3.updateAvatar(Unknown Source)
at com.chinaso.beautifulchina.net.UserRetrofitManager.updateAvatar(UserRetrofitManager.java:118)
at com.chinaso.beautifulchina.ui.my.mvp.EditUserPresenterImpl.updateAvatar(EditUserPresenterImpl.java:70)
at com.chinaso.beautifulchina.ui.my.UserEditActivity.onViewClicked(UserEditActivity.java:175)
at com.chinaso.beautifulchina.ui.my.UserEditActivity_ViewBinding$3.doClick(UserEditActivity_ViewBinding.java:72)
at butterknife.internal.DebouncingOnClickListener.onClick(DebouncingOnClickListener.java:22)
at android.view.View.performClick(View.java:6304)
at android.view.View$PerformClick.run(View.java:24803)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6604)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:518)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:818)
解决方案
//设置头像 @Multipart //添加这一行即可 @POST(ConstantUrl.MLZG_BASE_URL + "/usermanager/updateAvatar") Observable<EditUserResponse> updateAvatar(@Part MultipartBody.Part file);