Finally I resolved the issue.
If anyone want to upload image to server with extra additional parameters to server. Following are the way.
First create method in your retrofit API request interface.
@Multipart
@POST("test/test.post.json")
Call<APIResponseSiteDetails> addImages(@PartMap Map<String, RequestBody> params);
Now you have to call this method in your activity or fragment class where you want to call.
Map<String, RequestBody> params = new HashMap<>();
if (imageData != null && imageData.size() > 0) {
for (int i = 0; i < imageData.size(); i++) {
String key = "images[" + i + "]";
File file = new File(imageData.get(i).getImageurl());
try {
File compressedImages = new ImageZipper(AddDetailsActivity.this).compressToFile(file);
params.put("" + key + "\"; filename=\"" + file.getName(), getRequestFile(compressedImages));
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
above snippet code is only for sending image using MultiPart. Now if you want to send additional params with it. Then simply use this.
params.put("devicetype", toRequestBody(Constants.DEVICE_TYPE)); // This is String request body.
Now if you have another custom request using key pair value and want to send single item then you have to crate this way.
public Map<String, RequestBody> getAnotherRequest() {
Map<String, RequestBody> paStringRequestBodyMap = new HashMap<>();
paStringRequestBodyMap.put("userid", Functions.toRequestBody("1"));
paStringRequestBodyMap.put("id", Functions.toRequestBody("1"));
return paStringRequestBodyMap;
}
Now you have to add it in main request body then you have to perform.
for (Map.Entry<String, RequestBody> entry : getAnotherRequest().entrySet()) {
String key = entry.getKey();
RequestBody value = entry.getValue();
params.put("details[" + key + "]", value);
}
Now, if you want to send multiple requests, then you need to perform below code,
public Map<String, Map> getContacts() {
Map<String, Map> paStringRequestBodyMap = new HashMap<>();
if (showRoomDataList != null && showRoomDataList.size() > 0) {
for (int i = 0; i < showRoomDataList.size(); i++) {
Map<String, RequestBody> innnerList = new HashMap<>();
innnerList.put("id", Functions.toRequestBody("0"));
innnerList.put("visitorid", Functions.toRequestBody("0"));
innnerList.put("siteid", Functions.toRequestBody("0"));
innnerList.put("typeid", Functions.toRequestBody(showRoomDataList.get(i).getTypeid()));
paStringRequestBodyMap.put(i + "", innnerList);
}
}
return paStringRequestBodyMap;
}
Now put it to main request,
for (Map.Entry<String, Map> entry : getContacts().entrySet()) {
String key = entry.getKey();
Map value = entry.getValue();
Iterator it = value.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
RequestBody requestBody = (RequestBody) pair.getValue();
params.put("contacts[" + key + "]" + "[" + pair.getKey() + "]", requestBody);
it.remove(); // avoids a ConcurrentModificationException
}
}
For image multipart request body
private RequestBody getRequestFile(File file) {
RequestBody fbody = RequestBody.create(MediaType.parse("image/*"), file);
return fbody;
}
For String value request body
public static RequestBody toRequestBody(String value) {
RequestBody body = RequestBody.create(MediaType.parse("text/plain"), value);
return body;
}
So above are the code to perform with single, multiple post request using key pair value with image(Multipart) data. I hope this will help to all of you.