gpt4 book ai didi

android - Retrofit 2.0 发送文件和字符串值报错

转载 作者:行者123 更新时间:2023-11-29 19:34:33 25 4
gpt4 key购买 nike

我必须使用 Retrofit 2 在 post 方法中上传图片并发送 String 数据。我正在使用流动方法来做到这一点。

@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)

还有其他方法吗?

最佳答案

使用 Retrofit 2,您需要使用 OkHttp 的 RequestBody 或 MultipartBody.Part 类并将您的文件封装到请求主体中。让我们看一下文件上传的接口(interface)定义。

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

关于android - Retrofit 2.0 发送文件和字符串值报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39364928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com