cnflood 写道
谢谢分享,但是这样的数据传到后服务端后,服务端怎么解析呢
就正常解析JSON就可以了。
例如:
/**查询新闻列表 */
public void queryByNewsList()throws Exception{
List<Map<Object,Object>> newsList = newsService.QueryByTop_Android(10);//查询新闻列表前10条
//System.out.println("已经到服务器了!"+newsList);
net.sf.json.JSONArray jsonAry = new net.sf.json.JSONArray();
for(Map<Object,Object> m:newsList){
String news_date = new SimpleDateFormat("yyyy-MM-dd").format((Date)m.get("news_date"));
net.sf.json.JSONObject json = new net.sf.json.JSONObject();
json.put("news_id", m.get("news_id")+"");
json.put("news_title", m.get("news_title")+"");
json.put("news_date", news_date+"");
json.put("sort_id", m.get("sort_id")+"");
json.put("sort_name", m.get("sort_name")+"");
jsonAry.add(json);
}
//System.out.println("服务端组装JSON数据:");
//System.out.println(jsonAry);
byte[] jsonBytes = jsonAry.toString().getBytes("utf-8");
response.setContentType("text/html;charset=utf-8");
response.setContentLength(jsonBytes.length);
response.getOutputStream().write(jsonBytes);
response.getOutputStream().flush();
response.getOutputStream().close();
}
/**查询新闻列表-查询指定条数区间 */
public void queryByNewsLimit()throws Exception{
//System.out.println("查询"+num01+"条开始,的后"+num02+"条新闻");
List<Map<Object,Object>> newsList = newsService.QueryByLimit(num01, num02);//查询新闻列表前10条
//System.out.println("已经到服务器了!"+newsList);
net.sf.json.JSONArray jsonAry = new net.sf.json.JSONArray();
for(Map<Object,Object> m:newsList){
String news_date = new SimpleDateFormat("yyyy-MM-dd").format((Date)m.get("news_date"));
net.sf.json.JSONObject json = new net.sf.json.JSONObject();
json.put("news_id", m.get("news_id")+"");
json.put("news_title", m.get("news_title")+"");
json.put("news_date", news_date+"");
json.put("sort_id", m.get("sort_id")+"");
json.put("sort_name", m.get("sort_name")+"");
jsonAry.add(json);
}
//System.out.println("服务端组装JSON数据:");
//System.out.println(jsonAry);
byte[] jsonBytes = jsonAry.toString().getBytes("utf-8");
response.setContentType("text/html;charset=utf-8");
response.setContentLength(jsonBytes.length);
response.getOutputStream().write(jsonBytes);
response.getOutputStream().flush();
response.getOutputStream().close();
}
/** 查询新闻内容 */
public void queryByNewsInfo()throws Exception{
Map<Object,Object> newsinfo = newsService.QueryByNews_Id(news.getNews_id()).get(0);
//System.out.println("已经到服务器了!"+newsinfo);
net.sf.json.JSONArray jsonAry = new net.sf.json.JSONArray();
String news_date = new SimpleDateFormat("yyyy-MM-dd").format((Date)newsinfo.get("news_date"));
net.sf.json.JSONObject json = new net.sf.json.JSONObject();
json.put("news_id", newsinfo.get("news_id")+"");
json.put("news_title", newsinfo.get("news_title")+"");
json.put("news_content", newsinfo.get("news_content")+"");
json.put("news_img", newsinfo.get("news_img")+"");
json.put("news_date", news_date+"");
json.put("sort_id", newsinfo.get("sort_id")+"");
json.put("sort_name", newsinfo.get("sort_name")+"");
jsonAry.add(json);
//System.out.println("服务端组装JSON数据:");
//System.out.println(jsonAry);
byte[] jsonBytes = jsonAry.toString().getBytes("utf-8");
response.setContentType("text/html;charset=utf-8");
response.setContentLength(jsonBytes.length);
response.getOutputStream().write(jsonBytes);
response.getOutputStream().flush();
response.getOutputStream().close();
}
/** 保存客户端提交过来的问题。 */
public void SaveQuestion()throws Exception{
question.setQ_state("1");//默认为未公开
question.setQ_date(new Date());
question.setQ_content(question.getQ_title());
questionService.Save(question);
}
/**查询问题分类列表 */
public void queryByQuestionSortList()throws Exception{
List<Map<Object,Object>> questionSortList ;
questionSortList = all?questionSortService.QueryByAll():questionSortService.QueryByAll_Android();
//System.out.println("已经到服务器了!"+questionSortList);
net.sf.json.JSONArray jsonAry = new net.sf.json.JSONArray();
for(Map<Object,Object> m:questionSortList){
net.sf.json.JSONObject json = new net.sf.json.JSONObject();
json.put("sort_id", m.get("sort_id")+"");
json.put("sort_name", m.get("sort_name")+"");
jsonAry.add(json);
}
//System.out.println("服务端组装JSON数据:");
//System.out.println(jsonAry);
byte[] jsonBytes = jsonAry.toString().getBytes("utf-8");
response.setContentType("text/html;charset=utf-8");
response.setContentLength(jsonBytes.length);
response.getOutputStream().write(jsonBytes);
response.getOutputStream().flush();
response.getOutputStream().close();
}
/**通过分类ID查询问题列表 */
public void queryQuestionBySortIdList()throws Exception{
List<Map<Object,Object>> questionSortList = questionService.QueryBySort_Id_Android(questionSort.getSort_id());
//System.out.println("已经到服务器了!"+questionSortList);
net.sf.json.JSONArray jsonAry = new net.sf.json.JSONArray();
for(Map<Object,Object> m:questionSortList){
String q_date = new SimpleDateFormat("yyyy-MM-dd").format((Date)m.get("q_date"));
String a_date = new SimpleDateFormat("yyyy-MM-dd").format((Date)m.get("a_date"));
net.sf.json.JSONObject json = new net.sf.json.JSONObject();
json.put("q_id", m.get("q_id")+"");
json.put("q_title", m.get("q_title")+"");
json.put("q_date", q_date+"");
json.put("a_date", a_date+"");
json.put("a_content", m.get("a_content")+"");
json.put("sort_name", m.get("sort_name")+"");
jsonAry.add(json);
}
//System.out.println("服务端组装JSON数据:");
//System.out.println(jsonAry);
byte[] jsonBytes = jsonAry.toString().getBytes("utf-8");
response.setContentType("text/html;charset=utf-8");
response.setContentLength(jsonBytes.length);
response.getOutputStream().write(jsonBytes);
response.getOutputStream().flush();
response.getOutputStream().close();
}