我想将以下 JSON 数据发送到服务器并读取 android 中的响应。
下面是 Json 数据。
{
"class": "OrderItemListDto",
"orderItemList": [
{
"class": "OrderItemDto",
"orderId": 24,
"itemId": 1,
"quantity": 2,
"status": "NEW",
"sRequest": "none"
},
{
"class": "OrderItemDto",
"orderId": 24,
"itemId": 2,
"quantity": 2,
"status": "NEW",
"sRequest": "none"
}
]
}
这里可能数据会增加。
检查这个代码
JSONArray json = //your array;
HttpClient httpClient = new DefaultHttpClient();
HttpContext httpContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost("http://your_url");
try {
StringEntity se = new StringEntity(json.toString());
httpPost.setEntity(se);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
HttpResponse response = httpClient.execute(httpPost, httpContext); //execute your request and parse response
HttpEntity entity = response.getEntity();
String jsonString = EntityUtils.toString(entity); //if response in JSON format
} catch (Exception e) {
e.printStackTrace();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)