在 android 中通过改造 2.3.0 发送多张图像时出现错误请求
bad request while sending multiple images through retrofit 2.3.0 in android
我曾经通过改造 2.3.0 将多张图片发送到服务器,
但我收到了错误的请求(400 err0r)
我的代码:
ArrayList<MultiPartImagesInputData> multiImagesList = new ArrayList<>();
String[] multiplesImages = {"originalImgBlob", "img430Blog", "img200Blog", "img100Blog", "blurResponseBlob"};
for (int i = 0; i < mFiles.size(); i++) {
MultiPartImagesInputData multiPartImagesInputData = new MultiPartImagesInputData(multiplesImages[i], mFiles.get(i));
multiImagesList.add(multiPartImagesInputData);
}
并像这样创建多部分类型数组:
MultipartBody.Part[] multipartTypedOutputle = new MultipartBody.Part[mFiles.size()];
for (int index = 0; index < mFiles.size(); index++) {
Log.d("Upload request", "requestUploadSurvey: survey image " + index + " " + mFiles.get(index));
// File file2 = new File(mResultBtmpProccess.get(index).path);
RequestBody surveyBody = RequestBody.create(MediaType.parse("image/png"), mFiles.get(index));
multipartTypedOutputle[index] = MultipartBody.Part.createFormData("imageFiles["+index+"]", multiImagesList.get(index).getFileName(), surveyBody);
}
这里是服务器调用
RequestBody memberId1 = RequestBody.create(MediaType.parse("text/plain"), Singleton.getInstance().getUserRegDetailsRespModel().getMId());
RequestBody actionType1 = RequestBody.create(MediaType.parse("text/plain"), ACTION_TYPE_UPLOAD_NEW_PIC);
Call<JsonObject> stringCall = mServerUtilities.getStringClassService(getApplicationContext(), "").postImages(memberId1, actionType1, multipartTypedOutputle);
stringCall.enqueue(new retrofit2.Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, @NonNull retrofit2.Response<JsonObject> response) {
Log.d("fb_regist_response", "--->" + "" + response);
mUtilities.cancelProgressDialog();
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
mUtilities.cancelProgressDialog();
mUtilities.showAlert(t.getMessage(), getResources().getString(R.string.app_name));
Log.d("onFail_fb_regist_res", t.getMessage());
}
});
接口:
@PartMap Map<String, RequestBody> multipartTypedOutput);
@Multipart
@POST("/api/mbrphotos/prfImgIU/{memberId}/{actionType}")
Call<JsonObject> postImages(@Part("memberId") RequestBody memberId,
@Part("actionType") RequestBody actionType,
@Part MultipartBody.Part[] multipartTypedOutput);
我做错了什么?
你写的一切都正确,但错误必须在 API 后端。请重新检查。对我来说,我检查了邮递员它正在上传,即使 API 不允许移动设备触发它。他们必须允许表单数据。 :) 希望您的问题得到解决。
我曾经通过改造 2.3.0 将多张图片发送到服务器, 但我收到了错误的请求(400 err0r) 我的代码:
ArrayList<MultiPartImagesInputData> multiImagesList = new ArrayList<>();
String[] multiplesImages = {"originalImgBlob", "img430Blog", "img200Blog", "img100Blog", "blurResponseBlob"};
for (int i = 0; i < mFiles.size(); i++) {
MultiPartImagesInputData multiPartImagesInputData = new MultiPartImagesInputData(multiplesImages[i], mFiles.get(i));
multiImagesList.add(multiPartImagesInputData);
}
并像这样创建多部分类型数组:
MultipartBody.Part[] multipartTypedOutputle = new MultipartBody.Part[mFiles.size()];
for (int index = 0; index < mFiles.size(); index++) {
Log.d("Upload request", "requestUploadSurvey: survey image " + index + " " + mFiles.get(index));
// File file2 = new File(mResultBtmpProccess.get(index).path);
RequestBody surveyBody = RequestBody.create(MediaType.parse("image/png"), mFiles.get(index));
multipartTypedOutputle[index] = MultipartBody.Part.createFormData("imageFiles["+index+"]", multiImagesList.get(index).getFileName(), surveyBody);
}
这里是服务器调用
RequestBody memberId1 = RequestBody.create(MediaType.parse("text/plain"), Singleton.getInstance().getUserRegDetailsRespModel().getMId());
RequestBody actionType1 = RequestBody.create(MediaType.parse("text/plain"), ACTION_TYPE_UPLOAD_NEW_PIC);
Call<JsonObject> stringCall = mServerUtilities.getStringClassService(getApplicationContext(), "").postImages(memberId1, actionType1, multipartTypedOutputle);
stringCall.enqueue(new retrofit2.Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, @NonNull retrofit2.Response<JsonObject> response) {
Log.d("fb_regist_response", "--->" + "" + response);
mUtilities.cancelProgressDialog();
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
mUtilities.cancelProgressDialog();
mUtilities.showAlert(t.getMessage(), getResources().getString(R.string.app_name));
Log.d("onFail_fb_regist_res", t.getMessage());
}
});
接口:
@PartMap Map<String, RequestBody> multipartTypedOutput);
@Multipart
@POST("/api/mbrphotos/prfImgIU/{memberId}/{actionType}")
Call<JsonObject> postImages(@Part("memberId") RequestBody memberId,
@Part("actionType") RequestBody actionType,
@Part MultipartBody.Part[] multipartTypedOutput);
我做错了什么?
你写的一切都正确,但错误必须在 API 后端。请重新检查。对我来说,我检查了邮递员它正在上传,即使 API 不允许移动设备触发它。他们必须允许表单数据。 :) 希望您的问题得到解决。