gpt4 book ai didi

android - 如何在 Retrofit 2.2.0 的一个请求中同时发布 Body 和 Multipart 数据

转载 作者:行者123 更新时间:2023-11-29 19:10:47 27 4
gpt4 key购买 nike

我正在尝试在同一 API 请求中同时发布 @Body@Part 数据

喜欢下面

@Multipart
@POST("AccountService/MultipartProfileImageUpload")
Call<String> updateProfilePic(@Body UserIdCredentials userIdCredentials,@Part MultipartBody.Part file);

但它不工作我收到错误

@Body parameters cannot be used with form or multi-part encoding. (parameter #1)

完整的错误日志

07-31 05:01:01.644 6315-6315/com.orbiosolutions.yabeee E/AndroidRuntime: FATAL EXCEPTION: main Process: com.orbiosolutions.yabeee, PID: 6315 java.lang.RuntimeException: Failure delivering result ResultInfo{who=android:fragment:1, request=3, result=-1, data=Intent { (has extras) }} to activity {com.orbiosolutions.yabeee/com.orbiosolutions.yabeee.Activities.HomeActivity}: java.lang.IllegalArgumentException: @Body parameters cannot be used with form or multi-part encoding. (parameter #1) for method ApiInterface.updateProfilePic at android.app.ActivityThread.deliverResults(ActivityThread.java:3659) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3702) at android.app.ActivityThread.access$1300(ActivityThread.java:155) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1366) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5343) 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:905) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) Caused by: java.lang.IllegalArgumentException: @Body parameters cannot be used with form or multi-part encoding. (parameter #1) for method ApiInterface.updateProfilePic at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:752) at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:743) at retrofit2.ServiceMethod$Builder.parameterError(ServiceMethod.java:761) at retrofit2.ServiceMethod$Builder.parseParameterAnnotation(ServiceMethod.java:701) at retrofit2.ServiceMethod$Builder.parseParameter(ServiceMethod.java:336) at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:204) at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:170) at retrofit2.Retrofit$1.invoke(Retrofit.java:147) at java.lang.reflect.Proxy.invoke(Proxy.java:397) at $Proxy0.updateProfilePic(Unknown Source) at com.orbiosolutions.yabeee.Fragments.ProfileFragment.uploadProfilImg(ProfileFragment.java:191) at com.orbiosolutions.yabeee.Fragments.ProfileFragment.onCropImg(ProfileFragment.java:527) at com.orbiosolutions.yabeee.Fragments.ProfileFragment.onActivityResult(ProfileFragment.java:506) at android.app.Activity.dispatchActivityResult(Activity.java:6222) at android.app.ActivityThread.deliverResults(ActivityThread.java:3655) ... 10 more

请有人帮我解决这个问题

最佳答案

引用This answer 我做了这样的更改并且工作正常。

请求

RequestBody requestBody = RequestBody.create(MediaType.parse("*/*"), file);
MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("image", file.getName(), requestBody);
MultipartBody.Part id = MultipartBody.Part.createFormData("userId", userId);
//Creating a rest adapter
ApiInterface apiService =
ApiClient.getClient().create(ApiInterface.class);

Call<ProfilePicUpdateResponse> call = apiService.updateProfilePic(id,fileToUpload);
call.enqueue(new Callback<ProfilePicUpdateResponse>() {
@Override
public void onResponse(Call<ProfilePicUpdateResponse> call, Response<ProfilePicUpdateResponse> response) {

loading.dismiss();
if (response.isSuccessful()) {
Log.e("Success", new Gson().toJson(response.body()));
}
else
{
loading.dismiss();
csActivity.showMsg(getResources().getString(R.string.something_error));
}
}

@Override
public void onFailure(Call<ProfilePicUpdateResponse> call, Throwable t) {

loading.dismiss();
Log.d("=========>>", t.getMessage().toString());
Log.d("============>>>", "Failure");
}
});

接口(interface)类

 @Multipart
@POST("AccountService/MultipartProfileImageUpload")
Call<ProfilePicUpdateResponse> updateProfilePic(@Part MultipartBody.Part userId, @Part MultipartBody.Part file);

关于android - 如何在 Retrofit 2.2.0 的一个请求中同时发布 Body 和 Multipart 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45415422/

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