Java 如何在android中使用名称-值对将数组发送到服务器
在我的Android应用程序中,我需要使用名称-值对将以下JSON数组发送到服务器。这是下面的json响应,我需要向服务器发送Java 如何在android中使用名称-值对将数组发送到服务器,java,android,json,keyvaluepair,Java,Android,Json,Keyvaluepair,在我的Android应用程序中,我需要使用名称-值对将以下JSON数组发送到服务器。这是下面的json响应,我需要向服务器发送insertedIDs数组 { "message": "Deal was successfully done", "insertedIDs": [ { "deal_id": "579", "name": "zzzz" }, { "de
insertedIDs
数组
{
"message": "Deal was successfully done",
"insertedIDs": [
{
"deal_id": "579",
"name": "zzzz"
},
{
"deal_id": "580",
"name": "zzzz"
}
],
"status": "1"
}
以下是与服务器通信的代码
httpClient = new DefaultHttpClient();
resHandler = new BasicResponseHandler();
httpPost = new HttpPost(payment);
nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("loggedin_id",loggedin_id));
nameValuePairs.add(new BasicNameValuePair("amount",amount));
nameValuePairs.add(new BasicNameValuePair("payment_card_id",id));
nameValuePairs.add(new BasicNameValuePair("insertedIDs", ????)); // need to add the json array here.
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
jsonResponse = httpClient.execute(httpPost, resHandler);
Log.e("payment response", jsonResponse);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
httpClient=newdefaulthttpclient();
resHandler=新的BasicResponseHandler();
httpPost=新的httpPost(付款);
nameValuePairs=新的ArrayList(4);
添加(新的BasicNameValuePair(“loggedin_id”,loggedin_id));
添加(新的BasicNameValuePair(“金额”,amount));
添加(新的BasicNameValuePair(“支付卡id”,id));
nameValuePairs.add(新的基本nameValuePairs(“insertedIDs”);//需要在这里添加json数组。
试一试{
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
jsonResponse=httpClient.execute(httpPost,resHandler);
Log.e(“付款响应”,jsonResponse);
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
为什么不同时生成json并将其发送到服务器?看看gson(它是一个很好的json库,有很多教程),你的问题是什么?@a.S:好的,也许我太快了。@murali_-ma:您是否也开发了这种通信的服务器端部分,或者您是否可以与这样做的人交谈?murali_-ma,一般来说,这是可能的。但如何做到这一点完全取决于开发服务器端的人员。你需要和他们协商。如果他们不知道怎么做,那么请他们给你一个工作的例子。否则,您可以向他们推荐解决方案,比如带有JSON的A.S.,或者此处带有“[i]”的解决方案。我认为A.S.的建议是最好的。@murali_ma选择POST变量或JSON数据,但不要混合使用2!