我必须上传图片,并使用String
在post方法中发送Retrofit 2
数据。我正在使用流动的方法来做到这一点。
@Multipart
@FormUrlEncoded
@POST("signup/step3")
Call<BasicModel> completeRegistration(
/*All Basic Parameter*/
@Field("mobile_no") String mobileNo,
/*For User Image*/
@Part MultipartBody.Part photoFile
);
但是我收到了以下错误。
java.lang.IllegalArgumentException: Only one encoding annotation is allowed.
for method AllNetworkCalls.completeRegistration
at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:695)
at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:686)
at retrofit2.ServiceMethod$Builder.parseMethodAnnotation(ServiceMethod.java:266)
at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:169)
at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:166)
at retrofit2.Retrofit$1.invoke(Retrofit.java:145)
at java.lang.reflect.Proxy.invoke(Proxy.java:397)
at $Proxy1.completeRegistration(Unknown Source)
at com.sslwireless.app.Activities.SignUp.RegistrationActivity_5.completeRegistration(RegistrationActivity_5.java:244)
at com.sslwireless.app.Activities.SignUp.RegistrationActivity_5.access$1100(RegistrationActivity_5.java:51)
at com.sslwireless.app.Activities.SignUp.RegistrationActivity_5$5.onClick(RegistrationActivity_5.java:132)
at android.view.View.performClick(View.java:4856)
at android.view.View$PerformClick.run(View.java:19956)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5389)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
我还能采取其他方式吗?
答案 0 :(得分:2)
使用Retrofit 2,您需要使用OkHttp的RequestBody或MultipartBody.Part类,并将您的文件封装到请求正文中。我们来看看文件上传的接口定义。
public interface FileUploadService {
@Multipart
@POST("upload")
Call<ResponseBody> upload(@Part("description") RequestBody description,
@Part MultipartBody.Part file);
}
Java文件中的
private void uploadFile(Uri fileUri) {
// create upload service client
FileUploadService service =
ServiceGenerator.createService(FileUploadService.class);
// https://github.com/iPaulPro/aFileChooser/blob/master/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java
// use the FileUtils to get the actual file by uri
File file = FileUtils.getFile(this, fileUri);
// create RequestBody instance from file
RequestBody requestFile =
RequestBody.create(MediaType.parse("multipart/form-data"), file);
// MultipartBody.Part is used to send also the actual file name
MultipartBody.Part body =
MultipartBody.Part.createFormData("picture", file.getName(), requestFile);
// add another part within the multipart request
String descriptionString = "hello, this is description speaking";
RequestBody description =
RequestBody.create(
MediaType.parse("multipart/form-data"), descriptionString);
// finally, execute the request
Call<ResponseBody> call = service.upload(description, body);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call,
Response<ResponseBody> response) {
Log.v("Upload", "success");
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.e("Upload error:", t.getMessage());
}
});
}