0
好吧,我以前用POST工作过,但是我从来不需要POST数组。 (真的)如何使用POST与JSONArray
这是POST形式:
{
"about": "about me",
"sports": [
{
"id": 1,
"level": 3
},
{
"id": 2,
"level": 4
}
]
}
所以我要送一个JSONObject与“关于”键和值,和“体育” JSONArray,这可能是空了。
我试过如下:
1.
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
nameValuePair.add(new BasicNameValuePair("about", "Lorem ipsum about me"));
nameValuePair.add(new BasicNameValuePair("sports", "[]"));
2.
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
nameValuePair.add(new BasicNameValuePair("about", "Lorem ipsum about me"));
nameValuePair.add(new BasicNameValuePair("sports", new ArrayList<NameValuePair>()));
///Of course its silly it doesnt work at all
所以我的问题是如何实现这一目标POST形式?
我的帖子:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.addHeader("Content-Type", "application/json");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = httpclient.execute(httppost);
UrlEncodedFormEntity不是JSON。使用StringEntity和JSONObject.toString()。 – njzk2