0

I want to send Params in particular format to the server.

This is the format:

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

And I can send in this format but the problem is a value inside institute and user is going as String. And I want value inside it also go as key and value.

So I need to return params like Map<String,Map<String,String>> this

I tried this but it didn't work

 @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;
        }
Himanshu Dwivedi
  • 7,934
  • 3
  • 31
  • 52
Ajay Gohel
  • 243
  • 7
  • 17
  • did you tried setting those values directly to json object instead of passing them via map? – karan Feb 15 '19 at 06:20
  • Yes I tried to put JSONObject of institute and user directly into params Map<> but it also doesn't work. @KaranMer – Ajay Gohel Feb 15 '19 at 06:23
  • I am telling to replace map with using json directly. like setting `objectInstitute.put("name", institute);` and so on. – karan Feb 15 '19 at 06:29

1 Answers1

0

@Ajay there are many way to do so. We can parse from custom user Object using gson library or Map (approach you follow) or directly create JSONObject.

I had created 2 versions of the method, although ver2 seems best if your result won;t be heavy enough and also its very descriptive.

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

Try to figure out your mistake on ver1 of the method. But again i'll say ver2 is best fit in this scenario and very neat, hierarchical method.

Angad Tiwari
  • 1,738
  • 1
  • 12
  • 23