改造 - @Part MultipartBody.Part 未触发 API

Retrofit - @Part MultipartBody.Part is not triggering API

Retrofit 调用在我的 API 上运行良好,例如:

@POST("addeventphoto")
Call<NewPhotoObject> listRepos(@Query("key1") String testKey);
RetrofitInterfaces.IPostNewMessage service = RetrofitClientInstance.getRetrofitInstance().create(RetrofitInterfaces.IPostNewMessage.class);
        Call<NewPhotoObject> call = service.listRepos("hello");
        call.enqueue(new Callback<NewPhotoObject>() {
            @Override
            public void onResponse(Call<NewPhotoObject> call, Response<NewPhotoObject> response) {
                Log.d(TAG, "onResponse: " + response.body());
                if(response.isSuccessful() && response.body() != null){
                    Log.d(TAG, "onResponse: " + response.body().getEvent_photo());
                }

            }

            @Override
            public void onFailure(Call<NewPhotoObject> call, Throwable t) {
                Log.d(TAG, "onFailure: " + t);
            }
        });

我的 API 被触发,我得到了回复。但是,一旦我将 @Multipart 添加到我的 Retrofit 界面中......我的 API 就不再被触发。

@Multipart
@POST("addeventphoto")
Call<NewPhotoObject> listRepos(@Query("key1") String testKey, @Part MultipartBody.Part image);

我是否错误地使用了改装?当前版本:

implementation 'com.squareup.okhttp3:okhttp:3.10.0'
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'

编辑:与 MultipartBody.Part

一起使用
File file = new File(imagePath);
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("image", file.getName(), requestFile);

RetrofitInterfaces.IPostNewMessage service = RetrofitClientInstance.getRetrofitInstance().create(RetrofitInterfaces.IPostNewMessage.class);
        Call<NewPhotoObject> call = service.listRepos("hello", body);
        call.enqueue(new Callback<NewPhotoObject>() {
            @Override
            public void onResponse(Call<NewPhotoObject> call, Response<NewPhotoObject> response) {
                Log.d(TAG, "onResponse: " + response.body());
                if(response.isSuccessful() && response.body() != null){
                    Log.d(TAG, "onResponse: " + response.body().getEvent_photo());
                }

            }

            @Override
            public void onFailure(Call<NewPhotoObject> call, Throwable t) {
                Log.d(TAG, "onFailure: " + t);
            }
        });

您需要 标量转换器工厂 以便在 multipart

中传递值

添加这个依赖项

implementation "com.squareup.retrofit2:converter-scalars:$retrofit_version"

并在您的改造客户端中class添加转换器工厂

.addConverterFactory(ScalarsConverterFactory.create())

示例代码如下

retrofit = new Retrofit.Builder()
        .baseUrl(BASE_URL)
        .addConverterFactory(ScalarsConverterFactory.create())
        .addConverterFactory(GsonConverterFactory.create())
        .client(httpClient.build())
        .build();

我正在使用 API 网关 + Lambda,您需要手动启用二进制支持才能接受 MultipartBody.Part 类型。这就是为什么它已成功发送,但我的 API 没有被解雇。

https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-payload-encodings-configure-with-console.html