试图通过改装将表单数据发送到服务器,但无法向服务器请求。我想用他们的数据发布一个图像数组。
val builder: MultipartBody.Builder = MultipartBody.Builder().setType(MultipartBody.FORM);
builder.addFormDataPart("device_id",device_UDID)
builder.addFormDataPart("device_token",device_token)
builder.addFormDataPart("device_type","android")
builder.addFormDataPart("country_code",Constant.COUNTRY_CODE)
builder.addFormDataPart("email",signUpBean.email)
builder.addFormDataPart("mobile",signUpBean.phoneNumber)
builder.addFormDataPart("first_name",signUpBean.firstName)
builder.addFormDataPart("last_name",signUpBean.lastName)
builder.addFormDataPart("gender",signUpBean.gender)
builder.addFormDataPart("dob",signUpBean.dob)
builder.addFormDataPart("city",signUpBean.city)
builder.addFormDataPart("bike_type_id","1")
builder.addFormDataPart("bike_model",signUpBean.mfg)
builder.addFormDataPart("bike_manufacturer",signUpBean.mfg)
builder.addFormDataPart("reg_year",signUpBean.mfgYear)
builder.addFormDataPart("liecense_plate",signUpBean.licencePlateNo)
builder.addFormDataPart("bank_ac_name",signUpBean.bnkHolderName)
builder.addFormDataPart("bank_ac_number",signUpBean.bnkAccountNumber)
builder.addFormDataPart("bank_name",signUpBean.bnkName)
builder.addFormDataPart("bank_ifsc_code",signUpBean.ifscCode)
builder.addFormDataPart(
"profile_pic",
"profile" + ".jpg",
RequestBody.create(MediaType.parse("image/*"), file_profile!!)
)
builder.addFormDataPart(
"provider_documents[0][document]",
"1" + ".jpg",
RequestBody.create(MediaType.parse("image/*"), file_profile!!)
)
builder.addFormDataPart("provider_documents[0][document_id]","1")
builder.addFormDataPart("provider_documents[0][unique_id]","1")
builder.addFormDataPart("provider_documents[0][exprice_at]","2010-12-12")
val requestBody = builder.build()
observable = apiInterface.signUp2(requestBody)
我尝试了许多解决方案,但无法发布包含其数据的图像阵列。当我从addFormDataPart中删除provider\u文档时,效果很好。
// @Multipart
@POST(URLHelper.register)
fun signUp2(@Body builder: RequestBody ): Observable<Registration>
如何发送Providers\u文档
array,它在Postman
上运行良好。
val partMap = HashMap<String, RequestBody>()
partMap.put("device_id", createPartFromString(device_UDID));
partMap.put("device_token",createPartFromString(device_token))
partMap.put("device_type",createPartFromString("android"))
partMap.put("country_code",createPartFromString(Constant.COUNTRY_CODE))
partMap.put("email",createPartFromString(signUpBean.email))
partMap.put("mobile",createPartFromString(signUpBean.phoneNumber))
partMap.put("first_name",createPartFromString(signUpBean.firstName))
partMap.put("last_name",createPartFromString(signUpBean.lastName))
partMap.put("gender",createPartFromString(signUpBean.gender))
partMap.put("dob",createPartFromString(signUpBean.dob))
partMap.put("city",createPartFromString(signUpBean.city))
partMap.put("bike_type_id",createPartFromString("1"))
partMap.put("bike_model",createPartFromString(signUpBean.mfg))
partMap.put("bike_manufacturer",createPartFromString(signUpBean.mfg))
partMap.put("reg_year",createPartFromString(signUpBean.mfgYear))
partMap.put("liecense_plate",createPartFromString(signUpBean.licencePlateNo))
partMap.put("bank_ac_name",createPartFromString(signUpBean.bnkHolderName))
partMap.put("bank_ac_number",createPartFromString(signUpBean.bnkAccountNumber))
partMap.put("bank_name",createPartFromString(signUpBean.bnkName))
partMap.put("bank_ifsc_code",createPartFromString(signUpBean.ifscCode))
partMap.put(
"provider_documents["+0+"][document_id]",
createPartFromString(signUpBean.ifscCode)
)
partMap.put(
"provider_documents["+0+"][unique_id]",
createPartFromString(signUpBean.ifscCode)
)
partMap.put(
"provider_documents["+0+"][exprice_at]",createPartFromString(signUpBean.dob)
)
val ImageMap = HashMap<String, MultipartBody.Part>()
ImageMap.put("profile_pic", prepareFilePart("12", file_profile!!));
ImageMap.put("provider_documents["+0+"][document]", prepareFilePart("1", file_profile!!));
请求Api
@Multipart
@POST(URLHelper.register)
fun signUp3(
@PartMap photo: HashMap<String,
RequestBody>,
@PartMap ImageMap:HashMap<MultipartBody.Part>,
): Observable<Registration>
像这样使用它:-
// @Multipart
@POST(URLHelper.register)
fun signUp2(@Part builder: MultipartBody ): Observable<Registration>
更新:-
private void uploadToServer(String filePath) {
showProgressDialog();
Retrofit retrofit = RetrofitClient.getRetrofitClient(this);
ApiInterface uploadAPIs = retrofit.create(ApiInterface.class);
File file = new File(filePath);
//compressor.setDestinationDirectoryPath()
RequestBody fileReqBody = RequestBody.create(MediaType.parse("image/*"), file);
MultipartBody.Part part = MultipartBody.Part.createFormData("fileUpload", file.getName(), fileReqBody);
//RequestBody description = RequestBody.create(MediaType.parse("text/plain"), "image-type");
RequestBody imgNameReqBody = RequestBody.create(MediaType.parse("multipart/form-data"), "B2B_" + System.nanoTime());
uploadAPIs.uploadImage(imgNameReqBody, part).enqueue(new Callback<UploadImageRespose>() {
@Override
public void onResponse(@NonNull Call<UploadImageRespose> call, @NonNull retrofit2.Response<UploadImageRespose> response) {
if (response.isSuccessful() && response.body() != null) {
if (response.body().getCODE().equalsIgnoreCase("SUCCESS")) {
Toast.makeText(Activity.this, "Profile Image Upload Succesfully", Toast.LENGTH_SHORT).show();
} else {
hideProgressDialog();
Toast.makeText(Activity.this, "Some Error occurred, try again", Toast.LENGTH_SHORT).show();
}
} else {
hideProgressDialog();
}
}
@Override
public void onFailure(@NonNull Call<UploadImageRespose> call, @NonNull Throwable t) {
Timber.d(TAG, t.getMessage());
hideProgressDialog();
Toast.makeText(Activity.this, "Some Error occurred, try again", Toast.LENGTH_SHORT).show();
}
});
}
在界面中添加以下方法:-
@Multipart
@POST("Your Path Here")
Call<UploadImageRespose> uploadImage(@Part("img_name") RequestBody img_name,
@Part MultipartBody.Part file);
虚拟api接口。
public interface ApiInterface {
@Multipart
@POST(URLHelper.register)
Call<ModelProp> signUp2(@Part List<MultipartBody.Part> photos,
@PartMap Map<String, RequestBody> map;
}
现在创建像这样发布的数据。
Map<String, RequestBody> partMap = new HashMap<>();
List<MultipartBody.Part> images = new ArrayList<>();
partMap.put("device_id", createPartFromString(deviceId)); // add data which are common for all images like device_id, device_token, device_type etc.
..
..
for (int i=0; i < upFileList.size(); i++){
images.add(prepareFilePart("provider_documents["+i+"][document]", imageFile));
partMap.add("provider_documents["+i+"][expires_at]", createPartFromString(expiry)); // add image specific data.
...
..
}
...
..
observable = apiInterface.signUp2(images, partMap).
createPartFromString方法
public RequestBody createPartFromString(String string) {
return RequestBody.create(MultipartBody.FORM, string);
}
prepareFilePart方法
private MultipartBody.Part prepareFilePart(String partName, File file){
RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), file);
return MultipartBody.Part.createFormData(partName, file.getName(),requestBody);
}
问题内容: 我们想将图像文件作为multipart / form发送到后端,我们尝试使用html表单获取文件并将文件作为formData发送,这是代码 后端中的错误是 “嵌套异常为org.springframework.web.multipart.MultipartException:无法解析多部分servlet请求;嵌套异常为java.io.IOException:org.apache.tomc
我一直在做这个项目,其中一个部分包括从Raspberry向我的服务器发送一些数据。但它不能正常工作,我不知道为什么。我试图通过删除“urllib2”并使用“request”来修复错误。但徒劳的是,一切都没有改变。如果有人能帮助我,我会非常感激的。谢谢! PHP代码: python代码:
问题内容: 我在通过Notification从服务向活动发送数据时遇到问题,我单击了一个活动被调用的通知,但是当我尝试通过捆绑包添加一些参数时,我无法获得所谓的intent中的参数,我经历了链接 如何将通知单击中的参数发送到活动? 但是仍然没有运气。 其他人也发生过同样的问题吗? 提前致谢。 问题答案: 您还必须修改清单文件。 这是有效的示例: 这些变量和方法是Service类的成员: 这是Mai
我们想将图像文件作为multipart/form发送到后端,我们尝试使用html表单获取文件并将文件作为formData发送,下面是代码 后端中的错误是“嵌套异常是org.springframework.web.multipart.MultipartException:无法分析多部分servlet请求;嵌套异常是java.io.IOException:org.apache.tomcat.util.
我正在尝试将FormData从React JS发送到后端(express node server),如下所示。 我看到。我也尝试了这篇文章中的建议,但没有成功。感谢您的帮助。 如何从React js axios post请求发送FormData到节点服务器? 反应测试。js模块 快速Test.js 添加@con fused和@Kidas推荐的后,我可以在Express router中读取FormD
我正在尝试使用POST提交表单,但我有一些来自 标记的额外数据,这些数据已存储到JS对象中。当我从JavaScript中点击时,我想把它发送到服务器。 我尝试做的是用事件发送