我需要在嵌套的多部分数据中发出请求。我需要以多部分表格数据发送图像,其他细节也应该是多部分。 我的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"
}
}
答案 0 :(得分:0)
您必须像json结构一样创建一个POJO类并设置所有值。
然后
@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());
}
});