如何使用 retrofit 2 库上传图像和数据?

How to upload image and data with retrofit 2 library?

我想 post 一张像这样的图像 postman image:

里面 body:

里面 header:

我写了一些上传图片的代码,比如 postman 但我得到了这个错误:

okhttp3.internal.http2.ConnectionShutdownException

这是我的代码如下:

    File globalFileName;

    RequestBody requestBody1 = RequestBody.create(MediaType.parse("*/*"), globalFileName);

    MultipartBody.Part fileToUpload1 = MultipartBody.Part.createFormData("uploadFile",
            globalFileName.getName(), requestBody1);

    ApiConfig getResponse = AppConfig.getRetrofit().create(ApiConfig.class);
    RequestBody type = RequestBody.create(MultipartBody.FORM, "documents");
    RequestBody token = RequestBody.create(MultipartBody.FORM, "7220A3B7F8D2FD2C236092E0918B4EA3");

    OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
    httpClient.protocols( Collections.singletonList(Protocol.HTTP_1_1) );
    Call<ServerResponse> call = getResponse.uploadFile(userToken, fileToUpload1, type, token);
    call.enqueue(new Callback<ServerResponse>() {
        @Override
        public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
            ServerResponse serverResponse = response.body();
            Log.v(TAG, "Mahdi: Retrofit 2 onResponse: 0 " + serverResponse.toString());
        }

        @Override
        public void onFailure(Call<ServerResponse> call, Throwable t) {
            Log.e(TAG, "Mahdi: Retrofit 2 onFailure: ", t);
        }
    });

ApiConfig 接口代码:

public interface ApiConfig {
    @Multipart
    @POST("/upload")
    Call<ServerResponse> uploadFile(
            @Header("token") String userToken,
            @Part MultipartBody.Part file,
            @Part("category") RequestBody documents,
            @Part("token") RequestBody token
    );
}

服务器响应class代码:

public class ServerResponse {
    // variable name should be same as in the json response from php  
    @SerializedName("status")
    boolean status;
    @SerializedName("message")
    String message;
    public String getMessage() {
        return message;  
    }  
    public boolean getSuccess() {
        return status;
    }  
}

AppConfig class 代码:

public class AppConfig {
    private static String BASE_URL = "https://taxiappapi.webfumeprojects.online";
    public static Retrofit getRetrofit() {
        return new Retrofit.Builder().baseUrl(AppConfig.BASE_URL).addConverterFactory(GsonConverterFactory.create()).build();
    }
}

我使用了这个包:

implementation 'com.squareup.retrofit2:retrofit:2.6.1'
implementation 'com.squareup.retrofit2:converter-gson:2.6.1'

请帮助我谢谢。

上传图片改造的正确结构如下所示,但在你的情况下,我传递令牌会导致问题

    RequestBody requestBody1 = RequestBody.create(MediaType.parse("multipart/form- 
    data"), globalFileName);

    MultipartBody.Part fileToUpload1 = MultipartBody.Part.createFormData("uploadFile",
            globalFileName.getName(), requestBody1);

    
    RequestBody type = RequestBody.create(MediaType.parse("multipart/form-data"), 
    "documents");

    RequestBody token = RequestBody.create(MediaType.parse("multipart/form-data"), 
    "7220A3B7F8D2FD2C236092E0918B4EA3");

    ApiConfig getResponse = AppConfig.getRetrofit().create(ApiConfig.class);