学习java不到两个月,遇到了一个问题啊,求助大牛们啊~~ String strAddress = "苏州街3号大恒科技大厦";
String strCityName = "北京市";
List<JSONObject> jsonLists = new ArrayList<JSONObject>();
String strPoiid = "";
String strSimValue = "";
//批量获取地址和城市名称 for (int i = 0; i<2; i++)
{
JSONObject jsonObj = new JSONObject();
jsonObj.element("Address", strAddress);
jsonObj.element("CityName", strCityName);
jsonLists.add(jsonObj);
}
HttpClient httpclient = new DefaultHttpClient(); try {
HttpPost httppost = new HttpPost(" 一个URL ");
String strJSON = jsonLists.toString();
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("path", new StringBody(strJSON, Charset.forName("UTF-8")));
httppost.setEntity(reqEntity);
HttpResponse responseSim = httpclient.execute(httppost);
int statusCode = responseSim.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
//logger.info("服务器正常响应.....");
HttpEntity resEntity = responseSim.getEntity();
String json = EntityUtils.toString(resEntity); if(StringUtils.isNotBlank(json))
{
SimilarPoiMappers spms = (SimilarPoiMappers) JSONUtil.json2Object(json, SimilarPoiMappers.class);
JSONArray SimPoiMapperListArray = JSONArray.fromObject(JSONUtil.array2json(spms.getSimPoiMapperList())); for (int i = 0, len = SimPoiMapperListArray.size(); i < len; i++)
{
//JSONArray dataArray = JSONArray.fromObject(JSONUtil.array2json(SimPoiMapperListArray.get));
JSONObject jsonSpm = SimPoiMapperListArray.getJSONObject(i);
JSONArray jsonArray = JSONArray.fromObject(jsonSpm);
for (int j = 0, len2 = jsonArray.size(); j<len2;j++)
{
JSONObject jsonVideo = jsonArray.getJSONObject(j);
strPoiid = jsonVideo.get("GlobalID").toString();
strSimValue = jsonVideo.get("SimValue").toString();
} break;
}
} }
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
httpclient.getConnectionManager().shutdown();
} catch (Exception ignore) { }
}想通过批量post的方式传入多个json对象的一个list,返回处理后的json对象的list。上面的代码返回
[
{
"error" : "missing parameter"
}
]
感觉应该是代码写的有点问题,请大牛们指点迷津啊~~
String strCityName = "北京市";
List<JSONObject> jsonLists = new ArrayList<JSONObject>();
String strPoiid = "";
String strSimValue = "";
//批量获取地址和城市名称 for (int i = 0; i<2; i++)
{
JSONObject jsonObj = new JSONObject();
jsonObj.element("Address", strAddress);
jsonObj.element("CityName", strCityName);
jsonLists.add(jsonObj);
}
HttpClient httpclient = new DefaultHttpClient(); try {
HttpPost httppost = new HttpPost(" 一个URL ");
String strJSON = jsonLists.toString();
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("path", new StringBody(strJSON, Charset.forName("UTF-8")));
httppost.setEntity(reqEntity);
HttpResponse responseSim = httpclient.execute(httppost);
int statusCode = responseSim.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
//logger.info("服务器正常响应.....");
HttpEntity resEntity = responseSim.getEntity();
String json = EntityUtils.toString(resEntity); if(StringUtils.isNotBlank(json))
{
SimilarPoiMappers spms = (SimilarPoiMappers) JSONUtil.json2Object(json, SimilarPoiMappers.class);
JSONArray SimPoiMapperListArray = JSONArray.fromObject(JSONUtil.array2json(spms.getSimPoiMapperList())); for (int i = 0, len = SimPoiMapperListArray.size(); i < len; i++)
{
//JSONArray dataArray = JSONArray.fromObject(JSONUtil.array2json(SimPoiMapperListArray.get));
JSONObject jsonSpm = SimPoiMapperListArray.getJSONObject(i);
JSONArray jsonArray = JSONArray.fromObject(jsonSpm);
for (int j = 0, len2 = jsonArray.size(); j<len2;j++)
{
JSONObject jsonVideo = jsonArray.getJSONObject(j);
strPoiid = jsonVideo.get("GlobalID").toString();
strSimValue = jsonVideo.get("SimValue").toString();
} break;
}
} }
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
httpclient.getConnectionManager().shutdown();
} catch (Exception ignore) { }
}想通过批量post的方式传入多个json对象的一个list,返回处理后的json对象的list。上面的代码返回
[
{
"error" : "missing parameter"
}
]
感觉应该是代码写的有点问题,请大牛们指点迷津啊~~
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货