根据上一个案例:http://blog.csdn.net/acmman/article/details/38755237
下午又写了关于安卓网络传输的另外一种方法,采用JSON格式进行网络信息传递并解析。
用的WEB应用与Android客户端都是在上一个样例的基础上改写的。
WEB应用端
Servlet有所变化
package deu.hpu.servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import deu.hpu.servise.dima.News; import deu.hpu.servise.impl.VideoNewsService; import deu.hpu.servise.impl.VideoNewsServiceImpl; public class ListServlet extends HttpServlet { private VideoNewsService service=new VideoNewsServiceImpl(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List<News> videos=service.getLastNews(); //得到请求参数 String format=request.getParameter("format"); //如果是以json的请求参数,就以就向客户端返回json格式的数据,否则就返回XML格式的数据 if("json".equals(format)){ StringBuilder builder=new StringBuilder(); builder.append('['); for(News news:videos){ builder.append('{'); builder.append("id:").append(news.getid()).append(','); builder.append("title:\"").append(news.getTitle()).append("\","); builder.append("timelength:").append(news.getTimelength()); builder.append("},"); } builder.deleteCharAt(builder.length()-1); builder.append(']'); request.setAttribute("json", builder.toString()); request.getRequestDispatcher("/WEB-INF/Page/jsonvideonews.jsp").forward(request, response); } else{ request.setAttribute("videos", videos); request.getRequestDispatcher("/WEB-INF/Page/videonews.jsp").forward(request, response); } } }
相应的,在WEB-INF/Page下创建了新的JSP文件,用来接受request带过来的json数据,并描述成json数据的格式
相比XML格式的,json属于普通文本格式。代码很简单:
<%@ page language="java" import="java.util.*" contentType="text/plain; charset=UTF-8" pageEncoding="UTF-8"%>${json}
完成,之后在地址栏输入
http://10.20.124.72:8080/videonews/ListServlet?format=json
注意:后面必须加?format=json,不然无法以json格式输出数据!!
效果:
Android客户端:
我们在VedioService业务类中新增getJSONLastNews() 与parseJSON()两个方法,用来得到json数据和解析json数据以 List形式返回
/* * 获取最新的视频资讯 * */ public static List<News> getJSONLastNews() throws Exception{ String path="http://10.20.124.72:8080/videonews/ListServlet?format=json"; URL url=new URL(path); HttpURLConnection conn=(HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5000); conn.setRequestMethod("GET"); if(conn.getResponseCode()==200){ InputStream instream=conn.getInputStream(); return parseJSON(instream); } return null; } /* * 解析JSON数据 * */ private static List<News> parseJSON(InputStream instream) throws Exception{ List<News> newses=new ArrayList<News>(); byte [] data=streamTool.read(instream); String json=new String(data); //将String格式的json文本转换为数组格式 JSONArray array=new JSONArray(json); for (int i = 0; i < array.length(); i++) { JSONObject jsonObject=array.getJSONObject(i); News news=new News(jsonObject.getInt("id"),jsonObject.getString("title"),jsonObject.getInt("timelength")); newses.add(news); } return newses; }
运行之后,安卓客户端以json形式接受数据,运行效果:
时间: 2024-10-04 09:53:47