-1

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();
                        }
                    });
        }
user3764346
  • 129
  • 2
  • 5
  • 15
  • 1
    where is the java code? – Matt Clark Nov 29 '14 at 06:46
  • 1
    have you got any error While sending null to server? Your server may not accept the null value for the field. – Remees M Syde Nov 29 '14 at 06:48
  • @RemeesMSyde I did not get any error. Just I got the toast message from thr failiure. – user3764346 Nov 29 '14 at 06:54
  • K, try to print the "error" in your code.It should say what the problem is. – Remees M Syde Nov 29 '14 at 06:57
  • @RemeesMSyde There is no error. If it is not going to server then the format might be wrong or the url which i have given might be wrong.. But I have verified both are correct. – user3764346 Nov 29 '14 at 07:05
  • I dosnt told you you are doing it in a wrong way, try to send the data without null and also try to send with " ", if the data is set without null and its not setting for " " means the problem is with your server its not accepting empty data for that field. – Remees M Syde Nov 29 '14 at 07:42

2 Answers2

0

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

Prashant Jajal
  • 3,469
  • 5
  • 24
  • 38
0
  • 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);

kks
  • 342
  • 5
  • 25