使用Map <String,RequestBody>将requestBody传递给另一个请求主体

时间:2019-09-24 12:22:05

标签: android retrofit2

我的代码用于使用Multiform数据将数据发送到服务器。

代码:

    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;
}

现在,我想向主请求正文中添加另一个请求正文。但是它不接受它。

 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()));

现在,我想将 paStringRequestBodyMap 添加到主要的 params 请求正文中。但这进展不顺利。不接受身体要求。我用 paStringRequestBodyMap.toString()进行了尝试,但我认为这不是一个好方法。

因此,请您提供高级帮助!

0 个答案:

没有答案