Android : Data is not sending to server - JSON contains null value

973 Views Asked by At

This is the format of data I'am getting from server.

[  
{  
  "rid":"1",
  "srid":"0",
  "estimated_value":"100000",
  "expected_close_date":"2014-11-22",
  "sid":"2",
  "others":null
},
{  
  "rid":"1",
  "srid":"6",
  "estimated_value":0,
  "expected_close_date":null,
  "sid":"2",
  "others":null
},
{  
  "rid":"1",
  "srid":"7",
  "estimated_value":0,
  "expected_close_date":null,
  "sid":"2",
  "others":null
},
{  
  "rid":"11",
  "srid":"0",
  "estimated_value":"300000",
  "expected_close_date":"2014-11-14",
  "sid":"1",
  "others":null
},
{  
  "rid":"11",
  "srid":"38",
  "estimated_value":0,
  "expected_close_date":null,
  "sid":"1",
  "others":null
},
{  
  "rid":"11",
  "srid":"39",
  "estimated_value":0,
  "expected_close_date":null,
  "sid":"1",
  "others":null
}
]

Am receiving this JSON objects and setting to my form. Again I can do modifications and I have to send it to server in the same format. How do I send the null value in JSON? If I give null value without giving in quotes then the key and values are not available in the JSON object.

If I give it within double quotes then it is going like "others" : "null". However I send the data , It is not going to server. This is not happening when am doing this in other forms. The others form must have a value for all the fields. But In this form I can also have null values.

I want to know how can I achieve this?

Please help me.

This is my java code.

RequestParams params = new RequestParams();
            JSONArray req_array = new JSONArray();
            Integer estim = estimatedModified.size();
            for(int g=0;g<selectedReq.size();g++)
            {
                    JSONObject req_object = new JSONObject();
                    try {
                        req_object.put("rid", selectedReq.get(g).toString());
                        req_object.put("srid", 0);
                        req_object.put("sid", 1);
                        req_object.put("estimated_value", estimatedModified.get(g));
                        req_object.put("expected_close_date", expectedCloseModified.get(g));
                        req_object.put("others", "");
                        req_array.put(req_object);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
            }
                for (int j = 0; j < selectedSubReq.size(); j++) {
                    selectedSubReq.get(j);
                    JSONObject req_object = new JSONObject();
                    try {
                        SubRequirement temp = SubRequirement.getSubRequirement(selectedSubReq.get(j));
                        req_object.put("rid", temp.requirement_id);
                        req_object.put("srid", selectedSubReq.get(j).toString());
                        req_object.put("sid", 2);
                        req_object.put("estimated_value", 0);
                        req_object.put("expected_close_date", "null");
                        req_object.put("others", "null");
                        req_array.put(req_object);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
            }

            params.add("req", req_array.toString());
            System.out.println("ReqArray" + req_array.toString());
            AsyncClientHandler.post("projectRequirements/save/"+proj.p_id, params,
                    new AsyncHttpResponseHandler() {
                        @Override
                        public void onFailure(int statusCode,
                                Throwable error, String content) {
                            super.onFailure(statusCode, error, content);

                            Toast.makeText(getActivity(), "failiure push"+proj.p_id,Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onSuccess(int statusCode,
                                Header[] headers, String content) {
                            super.onSuccess(statusCode, headers, content);
                            Toast.makeText(getActivity(), "success push",Toast.LENGTH_SHORT).show();
                        }
                    });
        }
2

There are 2 best solutions below

0
On
  • try this one

    if(json.isNull("others")) { othersStr = null; } else { othersStr = json.getString("others"); }

    HttpClient client=new DefaultHttpClient(); HttpPost req=null; List<NameValuePair> reqParams = new ArrayList<NameValuePair>(); reqParams.add(new BasicNameValuePair("RequestJson", reqObj.toString()));
    req=new HttpPost(appUrl);
    req.setEntity(new UrlEncodedFormEntity(reqParams)); HttpResponse res=client.execute(req);

2
On

before binding value in json, just check it if it NULL then bind "" (blank string) else bind original string..