问题
My code is for send data to server using Multiform data.
Code:
Map<String, RequestBody> params = new HashMap<>();
params.put("devicetype", toRequestBody(Constants.DEVICE_TYPE));
params.put("deviceid", toRequestBody(Functions.getDeviceId(AddDetailsActivity.this)));
params.put("appversion", toRequestBody(Constants.APP_VERSION));
params.put("apiversion", toRequestBody(Constants.API_VERSION));
params.put("timezone", toRequestBody(Functions.getTimeStamp()));
params.put("modeltype", toRequestBody(Functions.getDeviceModel()));
params.put("deviceos", toRequestBody(Build.VERSION.RELEASE));
params.put("userdeviceid", toRequestBody(MySharedPreferences.getCustomPreference(AddDetailsActivity.this, Constants.SHAREDPREFERENCE_USERDETAILS).getUserdeviceid()));
if (edit.equalsIgnoreCase("Y")) {
params.put("visitorid", toRequestBody(visitorid));
params.put("siteid", toRequestBody(siteid));
} else {
params.put("visitorid", toRequestBody("0"));
params.put("siteid", toRequestBody("0"));
}
for (int i = 0; i < Constants.images_path.size(); i++) {
String key = "images[" + i + "]";
try {
File compressedImages = new ImageZipper(AddDetailsActivity.this).compressToFile(new File(Constants.images_path.get(i)));
params.put("" + key + "\"; filename=\"" + key + ".png", getRequestFile(compressedImages));
} catch (IOException e) {
e.printStackTrace();
}
}
public static RequestBody toRequestBody(String value) {
RequestBody body = RequestBody.create(MediaType.parse("text/plain"), value);
return body;
}
Now i want to add another request body to main request body.. But it is not accepting it.
Map<String, RequestBody> paStringRequestBodyMap = new HashMap<>();
if (addDetailsActivity.getVisitorSites() != null) {
paStringRequestBodyMap.put("id", Functions.toRequestBody(addDetailsActivity.getVisitorSites().getSiteid()));
paStringRequestBodyMap.put("visitorid", Functions.toRequestBody(addDetailsActivity.getVisitorSites().getVisitorid()));
} else {
paStringRequestBodyMap.put("visitorid", Functions.toRequestBody("0"));
paStringRequestBodyMap.put("id", Functions.toRequestBody("0"));
}
paStringRequestBodyMap.put("name", Functions.toRequestBody(editTextSitename.getText().toString().trim()));
Now I want to add paStringRequestBodyMap to main params request body. But it is not going well. Not accepting the body request. I tried it with paStringRequestBodyMap.toString() but i think that is not a good way.
So advanced help would be appreciated!
来源:https://stackoverflow.com/questions/58080241/pass-requestbody-to-another-request-body-using-mapstring-requestbody