如何使用 Multipart 在 retrofit2 中发送整数列表和图像
How to send List of integers along with image in retrofit2 using Multipart
我正在尝试使用 retrofit 2.0 上传图像。我几乎没有其他参数可以与包含整数列表的表单数据一起发送。我的请求声明是
@Multipart
@POST("/api/Add")
Call<AddResponse> addApi(@Part("Id") RequestBody id,
@Part("Name") RequestBody name,
@Part("Description") RequestBody description,
@Part("Phone") RequestBody phone,
@Part MultipartBody.Part logo,
@Part("WorkingDays") RequestBody workingDays,
@Part("CitiesIDs") RequestBody cities);
public class RetrofitService {
private static Retrofit retrofit;
private static final Object LOCK = new Object();
private static final String BASE_URL = "https://baseurl.app";
public static Retrofit getRetrofitInstance() {
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(chain -> {
Request original = chain.request();
Request.Builder requestBuilder = original.newBuilder()
.addHeader("Content-Type", "application/json");
Request request = requestBuilder.build();
return chain.proceed(request);
});
OkHttpClient client = httpClient.build();
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
httpClient.addInterceptor(logging);
httpClient.readTimeout(60, TimeUnit.SECONDS);
httpClient.connectTimeout(60, TimeUnit.SECONDS);
if (retrofit == null) {
synchronized (LOCK) {
retrofit = new retrofit2.Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
.build();
}
}
return retrofit;
}
这种方式无法按预期工作,整数列表如下所示单独发送。
Content-Disposition: form-data; name="CitiesIDs"
Content-Transfer-Encoding: binary
Content-Type: multipart/form-data; boundary=290d2964-526a-48b9-b742-c6a3b547c7d6
Content-Length: 286
--290d2964-526a-48b9-b742-c6a3b547c7d6
Content-Disposition: form-data; name="CitiesIDs"
Content-Length: 1
7
--290d2964-526a-48b9-b742-c6a3b547c7d6
Content-Disposition: form-data; name="CitiesIDs"
Content-Length: 1
8
--290d2964-526a-48b9-b742-c6a3b547c7d6--
此外,为了将整数列表转换为 RequestBody,我使用以下方式
okhttp3.MultipartBody.Builder citiesRequestBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM);
for(int i=0; i<citiesIdList.size(); ++i){
citiesRequestBodyBuilder.addFormDataPart("CitiesIDs", String.valueOf(citiesIdList.get(i)));
}
由于添加表单数据部分方法 addFormDataPart
只接受字符串,所以我不得不将整数转换为字符串,这使得服务器响应以下消息
{"Success":false,
"ErrorMessage":"Input string '--290d2964-526a-48b9-b742-c6a3b547c7d6' is not a valid number. Path '', line 1, position 38.",
"Id":0}
邮递员,工作正常
那么我如何使用 Multipart 发送图像和整数列表。
问题是 RequestBody
只接受 File, byte[], String, ByteString 如何发送带有 @part
.
的整数列表
感谢您的帮助。
您必须仅对文件使用 MultipartBody.Part,其他字段应与 RequestBody 一起使用。
删除下面的代码
for(int i=0; i<citiesIdList.size(); ++i){
citiesRequestBodyBuilder.addFormDataPart("CitiesIDs", String.valueOf(citiesIdList.get(i)));
}
使用
// Add other field here
RequestBody description =
RequestBody.create(
okhttp3.MultipartBody.FORM, Gson().toJson(citiesIdList));
我正在尝试使用 retrofit 2.0 上传图像。我几乎没有其他参数可以与包含整数列表的表单数据一起发送。我的请求声明是
@Multipart
@POST("/api/Add")
Call<AddResponse> addApi(@Part("Id") RequestBody id,
@Part("Name") RequestBody name,
@Part("Description") RequestBody description,
@Part("Phone") RequestBody phone,
@Part MultipartBody.Part logo,
@Part("WorkingDays") RequestBody workingDays,
@Part("CitiesIDs") RequestBody cities);
public class RetrofitService {
private static Retrofit retrofit;
private static final Object LOCK = new Object();
private static final String BASE_URL = "https://baseurl.app";
public static Retrofit getRetrofitInstance() {
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(chain -> {
Request original = chain.request();
Request.Builder requestBuilder = original.newBuilder()
.addHeader("Content-Type", "application/json");
Request request = requestBuilder.build();
return chain.proceed(request);
});
OkHttpClient client = httpClient.build();
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
httpClient.addInterceptor(logging);
httpClient.readTimeout(60, TimeUnit.SECONDS);
httpClient.connectTimeout(60, TimeUnit.SECONDS);
if (retrofit == null) {
synchronized (LOCK) {
retrofit = new retrofit2.Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
.build();
}
}
return retrofit;
}
这种方式无法按预期工作,整数列表如下所示单独发送。
Content-Disposition: form-data; name="CitiesIDs"
Content-Transfer-Encoding: binary
Content-Type: multipart/form-data; boundary=290d2964-526a-48b9-b742-c6a3b547c7d6
Content-Length: 286
--290d2964-526a-48b9-b742-c6a3b547c7d6
Content-Disposition: form-data; name="CitiesIDs"
Content-Length: 1
7
--290d2964-526a-48b9-b742-c6a3b547c7d6
Content-Disposition: form-data; name="CitiesIDs"
Content-Length: 1
8
--290d2964-526a-48b9-b742-c6a3b547c7d6--
此外,为了将整数列表转换为 RequestBody,我使用以下方式
okhttp3.MultipartBody.Builder citiesRequestBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM);
for(int i=0; i<citiesIdList.size(); ++i){
citiesRequestBodyBuilder.addFormDataPart("CitiesIDs", String.valueOf(citiesIdList.get(i)));
}
由于添加表单数据部分方法 addFormDataPart
只接受字符串,所以我不得不将整数转换为字符串,这使得服务器响应以下消息
{"Success":false,
"ErrorMessage":"Input string '--290d2964-526a-48b9-b742-c6a3b547c7d6' is not a valid number. Path '', line 1, position 38.",
"Id":0}
邮递员,工作正常
问题是 RequestBody
只接受 File, byte[], String, ByteString 如何发送带有 @part
.
感谢您的帮助。
您必须仅对文件使用 MultipartBody.Part,其他字段应与 RequestBody 一起使用。
删除下面的代码
for(int i=0; i<citiesIdList.size(); ++i){
citiesRequestBodyBuilder.addFormDataPart("CitiesIDs", String.valueOf(citiesIdList.get(i)));
}
使用
// Add other field here
RequestBody description =
RequestBody.create(
okhttp3.MultipartBody.FORM, Gson().toJson(citiesIdList));