使用改造的嵌套多部分请求
nested multipart request using retrofit
我需要发出嵌套多部分数据中的请求。我需要在多部分表单数据中发送图像,其他细节也应该在多部分中。
我的 Json 请求如下:
{
"emailId": "vision.jav@avenger.com",
"phoneNumber": "7417385811",
"profileImage": "image",
"password": "12345678",
"customerDetails": {
"firstName": "vison",
"lastName": "vision"
},
"addressDetails": {
"city": "chicago",
"province": "NY",
"postalCode": "654987",
"latitude": "28.52",
"longitude": "77.54"
},
"userRole": {
"role": "CUSTOMER"
}
}
您必须像您的 json 结构一样创建一个 POJO class 并设置所有值。
然后
@Multipart
@POST("uploadData.php")
Call<ServerResponse> uploadFile(@PartMap Map<String, RequestBody> map,@Part MultipartBody.Part file);
那么你必须使用映射选项将数据发送到服务器。
Map<String, RequestBody> map = new HashMap<>();
map.put("PRODUCTID", RequestBody.create(MediaType.parse("text/plain"), ProductId));
MultipartBody.Part imageFile = null;
try {
File file = new File(Environment.getExternalStorageDirectory() + "/Download/Salty.png");
if file != null) {
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"),
file);
imageFile = MultipartBody.Part.createFormData("ImageFile", file.getName(), requestFile);
}
}
catch (Exception ex)
{
Log.d("UploadStatus", "Multipart Image as exception occurs");
}
ApiService uploadImage = ApiClient.getClient().create(ApiService.class);
Call<ServerResponse> fileUpload = uploadImage.uploadFile(map,imageFile);
fileUpload.enqueue(new Callback<ServerResponse>() {
@Override
public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
Toast.makeText(MainActivity.this, "Success " + response.message(), Toast.LENGTH_LONG).show();
Toast.makeText(MainActivity.this, "Success " + response.body().toString(), Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(Call<ServerResponse> call, Throwable t) {
Log.d("TAG", "Error " + t.getMessage());
}
});
作为参考,我发布了答案。我不知道 json 对象是如何在后端映射的。经过多次尝试,我终于找到了解决方案。它为我工作。
对于上面发布的请求,只需使用一个点 (.) 和内部 json 对象来制作如下键。
- 为 firstName 插入数据,使用 "customerDetails.firstName" 作为键。
- 类似地,姓氏使用 "customerDetails.lastName" 作为关键字。
我需要发出嵌套多部分数据中的请求。我需要在多部分表单数据中发送图像,其他细节也应该在多部分中。 我的 Json 请求如下:
{
"emailId": "vision.jav@avenger.com",
"phoneNumber": "7417385811",
"profileImage": "image",
"password": "12345678",
"customerDetails": {
"firstName": "vison",
"lastName": "vision"
},
"addressDetails": {
"city": "chicago",
"province": "NY",
"postalCode": "654987",
"latitude": "28.52",
"longitude": "77.54"
},
"userRole": {
"role": "CUSTOMER"
}
}
您必须像您的 json 结构一样创建一个 POJO class 并设置所有值。
然后
@Multipart
@POST("uploadData.php")
Call<ServerResponse> uploadFile(@PartMap Map<String, RequestBody> map,@Part MultipartBody.Part file);
那么你必须使用映射选项将数据发送到服务器。
Map<String, RequestBody> map = new HashMap<>();
map.put("PRODUCTID", RequestBody.create(MediaType.parse("text/plain"), ProductId));
MultipartBody.Part imageFile = null;
try {
File file = new File(Environment.getExternalStorageDirectory() + "/Download/Salty.png");
if file != null) {
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"),
file);
imageFile = MultipartBody.Part.createFormData("ImageFile", file.getName(), requestFile);
}
}
catch (Exception ex)
{
Log.d("UploadStatus", "Multipart Image as exception occurs");
}
ApiService uploadImage = ApiClient.getClient().create(ApiService.class);
Call<ServerResponse> fileUpload = uploadImage.uploadFile(map,imageFile);
fileUpload.enqueue(new Callback<ServerResponse>() {
@Override
public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
Toast.makeText(MainActivity.this, "Success " + response.message(), Toast.LENGTH_LONG).show();
Toast.makeText(MainActivity.this, "Success " + response.body().toString(), Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(Call<ServerResponse> call, Throwable t) {
Log.d("TAG", "Error " + t.getMessage());
}
});
作为参考,我发布了答案。我不知道 json 对象是如何在后端映射的。经过多次尝试,我终于找到了解决方案。它为我工作。 对于上面发布的请求,只需使用一个点 (.) 和内部 json 对象来制作如下键。
- 为 firstName 插入数据,使用 "customerDetails.firstName" 作为键。
- 类似地,姓氏使用 "customerDetails.lastName" 作为关键字。