Pass requestBody to another request body using Map<String,RequestBody>

让人想犯罪 __ 提交于 2020-01-06 05:40:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!