好的,我以前使用过 POST,但我从来不需要 POST Arrays。(真的)
这是 POST 表单:
{
"about": "about me",
"sports": [
{
"id": 1,
"level": 3
},
{
"id": 2,
"level": 4
}
]
}
所以我必须发送一个带有“about”键和值的 JSONObject,以及一个“sports”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);