如何使用Volley在JsonObject内部发布参数

时间:2019-02-15 06:05:29

标签: android android-volley

我想将特定格式的Params发送到服务器。

这是格式:

{
                   "institute": {
                       "name": $scope.formData.institute_name,
                       "state": $scope.formData.state,
                       "city": $scope.formData.city,
                       "pin_code": $scope.formData.pincode,
                       "nature": $scope.formData.nature,
                   },
                   "user": {
                       "role": $scope.formData.role,
                       "user_id": $scope.user_id
                   }
               };

我可以以这种格式发送,但是问题是institute中的一个值,而user却是String。而且我希望其中的价值也成为关键和价值。

所以我需要返回类似Map<String,Map<String,String>>这样的参数

我尝试了此操作,但没有成功

 @Override
        protected Map<String, String> getParams() throws AuthFailureError {

            Map<String, String> instituteParams = new HashMap<>();
            instituteParams.put("name", institute);
            instituteParams.put("state", state);
            instituteParams.put("city", city);
            instituteParams.put("pin_code", pincode);
            instituteParams.put("nature", nature);

            JSONObject objectInstitute = new JSONObject(instituteParams);

            Map<String, String> userParams = new HashMap<>();
            userParams.put("role", role);
            userParams.put("user_id", userid);

            JSONObject objectUser = new JSONObject(userParams);

            Map<String, String> params = new HashMap<>();
            params.put("institute", objectInstitute.toString());
            params.put("user", objectUser.toString());
            return params;
        }

1 个答案:

答案 0 :(得分:0)

@Ajay有很多方法可以做到这一点。我们可以使用gson库或Map(您遵循的方法)从自定义用户Object进行解析,也可以直接创建JSONObject。

我已经创建了该方法的2个版本,尽管如果您的结果会胜于ver2,它似乎是最好的;不够繁琐且也非常具有描述性。

private JSONObject getParamsVer1() throws JSONException {
    Map<String, String> instituteParams = new HashMap<>();
    instituteParams.put("name", "");
    instituteParams.put("state", "");
    instituteParams.put("city", "");
    instituteParams.put("pin_code", "");
    instituteParams.put("nature", "");

    JSONObject objectInstitute = new JSONObject(instituteParams);

    Map<String, String> userParams = new HashMap<>();
    userParams.put("role", "");
    userParams.put("user_id", "");

    JSONObject objectUser = new JSONObject(userParams);

    Map<String, JSONObject> params = new HashMap<>();
    params.put("institute", objectInstitute);
    params.put("user", objectUser);

    return new JSONObject(params);
}

private JSONObject getParamsVer2() throws JSONException {
    JSONObject jsonObject = new JSONObject() {
        {
            put("institute", new JSONObject(){
                {
                    put("name", "");
                    put("state", "");
                    put("city", "");
                    put("pin_code", "");
                    put("nature", "");
                }
            });
            put("user", new JSONObject(){
                {
                    put("role", "");
                    put("user_id", "");
                }
            });
        }
    };
    return jsonObject;
}
  

尝试找出您在方法ver1上的错误。但是我还是要说ver2最适合这种情况,并且是非常整洁的分层方法。

相关问题