I have the following json that I need to upload:
[{"key":"value","key1":"value1"},
[{"innerkey":"innervalue","filename":"name"},{"innerkey":"innervalue","filename":"name"}]]
the two innerkeys which are inside the JsonArray have two files which need to be uploaded.
I am using okhttp multipart to upload them.
A normal field is added as
multipart.addFormDataPart(key, value);
and a file is added as
multipart.addFormDataPart("filename", "image.jpeg", RequestBody.create(MediaType.parse("image/jpeg"), new File(path)));
I have uploaded a non nested json before by converting the json into a hashmap and adding the parts using a for loop
MultipartBody.Builder multipart = new MultipartBody.Builder();
for (Map.Entry<String, String> entry : data.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
if(!key.equals("filePath")){
multipart.addFormDataPart(key, value);
}else{
String filename = "";
String type = MyUtility.getMimeType(key);
filename = key.substring(key.lastIndexOf("/") + 1);
multipart.addFormDataPart("fileName", filename, RequestBody.create(MediaType.parse(type), new File(key)));
}
}
the new structure seems to be quite difficult to process how do I do it?