你必须使用 jsonArray 来发送 arraylist,使用这个代码
JSONObject outerJsonObject ;
JSONArray _mainArray ;
outerJsonObject = new JSONObject();
_mainArray = new JSONArray();
for (int i = 0; i < count; i++) {
JSONObject _jSubObj = new JSONObject();
try {
_jSubObj.put("user_id",_userId);
_jSubObj.put("first_name",name.get(i));
}
_mainArray.put(_jSubObj);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
outerJsonObject.put("address_details",_mainArray);
// System.out.println("OuterJSON request"+outerJsonObject);
// System.out.println("JSON ARRAY : " + _mainArray);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
在这个代号中是位于 i 位置的 arraylist。
和参数使用如
// Creating HTTP client
client = new DefaultHttpClient();
// Creating HTTP Post
HttpPost httpPost = new HttpPost(WebServiceConstants
.getMethodUrl(WebServiceConstants.METHOD_ADD_ADDRESS));
// Building post parameters, key and value pair
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
nameValuePair.add(new BasicNameValuePair("address_details", outerJsonObject.toString()));
System.out.println("Whole name value pair Address=" + nameValuePair.toString());
// Url Encoding the POST parameters
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
} catch (UnsupportedEncodingException e) {
// writing error to Log
e.printStackTrace();
}
try {
responseMain = client.execute(httpPost);
// Log.e("RESPONSe of My order", String.valueOf(responseMain));
HttpEntity entitity = responseMain.getEntity();
_responseMain = EntityUtils.toString(entitity); // content will be consume only once
System.out.println("Response of Address "+_responseMain);
} catch (Exception e)
{
e.printStackTrace();
}
我希望它正在工作