Android:数据未发送到服务器-JSON包含空值
这是我从服务器获取的数据格式Android:数据未发送到服务器-JSON包含空值,android,json,android-json,Android,Json,Android Json,这是我从服务器获取的数据格式 [ { "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", "
[
{
"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
}
]
我收到这个JSON对象并将其设置为我的表单。同样,我可以做修改,我必须以相同的格式将其发送到服务器。如何在JSON中发送空值?如果我在没有引号的情况下给出null值,那么键和值在JSON对象中不可用
如果我在双引号内给出它,那么它就像“others”:“null”
。然而,我发送的数据,它不会去服务器。当我以其他形式做这件事时,这是不会发生的。“其他”窗体必须为所有字段指定一个值。但在这种形式下,我也可以有空值
我想知道我怎样才能做到这一点
请帮帮我
这是我的java代码
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();
}
});
}
RequestParams params=newrequestparams();
JSONArray req_数组=新的JSONArray();
整数estim=estimatedModified.size();
对于json中绑定值之前的(int g=0;g,只需检查它是否为NULL,然后绑定“”(空白字符串),否则绑定原始字符串。
- 试试这个
if(json.isNull(“其他”)){
othersStr=null;
}否则{
othersStr=json.getString(“其他”);
}
HttpClient=newdefaulthttpclient();
HttpPost-req=null;
List reqParams=新建ArrayList();
添加(新的BasicNameValuePair(“RequestJson”,reqObj.toString());
req=新的HttpPost(appUrl);
请求setEntity(新的UrlEncodedFormEntity(请求参数));
HttpResponse res=client.execute(req);
是的,我尝试使用空字符串和值。但如果您听说过BasicNameValuePair类,则它不会进入服务器。然后您也可以使用该类在GET或POST请求中将JSON对象传递到URL。java代码在哪里?向服务器发送null时是否有任何错误?您的服务器可能不接受该字段的null值。@ReMeesmssyde我没有收到任何错误。我只是从thr failiure收到toast消息。K,尝试在代码中打印“错误”。它应该说明问题所在。@remeemssyde没有错误。如果它不发送到服务器,则格式可能错误,或者我提供的url可能错误。但我已验证两者都正确。