当前位置:编程学习 > JS >>

Android开发——获取网络资源之json数据

一、项目背景
在Android开发中有一项非常广泛的应用:Android项目获取另一个web项目的资源或者返回的数据。
 
本文获取web项目返回的JSON数据。Android应用解析JSON比XML性能要好,但有许多项目仍然采用的是XML。
 
 
 
 
 
二、实例代码
 
Web项目
 
[java]  
/** 
 * 新闻业务类 
 *  
 * @author 徐越 
 *  
 */  
public class VideoNewsServiceImpl implements VideoNewsService  
{  
    public List<VideoNews> readNews()  
    {  
        List<VideoNews> lst = new ArrayList<VideoNews>();  
        lst.add(new VideoNews(1, "喜洋洋", 20));  
        lst.add(new VideoNews(2, "变形金刚", 10));  
        lst.add(new VideoNews(3, "功夫熊猫", 20));  
        return lst;  
    }  
}  
  
/** 
 * 新闻Servlet 
 *  
 * @author 徐越 
 *  
 */  
public class ListServlet extends HttpServlet  
{  
    private static final long serialVersionUID = 1L;  
    private VideoNewsService vs = new VideoNewsServiceImpl();  
  
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException  
    {  
        doPost(request, response);  
    }  
  
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException  
    {  
        List<VideoNews> news = vs.readNews();  
        JSONArray jsonarr = JSONArray.fromObject(news);  
        response.setCharacterEncoding("UTF-8");  
        response.setContentType("text/plain;charset=UTF-8");  
        response.setHeader("Pragma", "No-cache");  
        response.setHeader("Cache-Control", "no-cache");  
        response.setDateHeader("Expires", 0);  
        response.getWriter().print(jsonarr);  
    }  
}  
 
/**
 * 新闻业务类
 * 
 * @author 徐越
 * 
 */
public class VideoNewsServiceImpl implements VideoNewsService
{
public List<VideoNews> readNews()
{
List<VideoNews> lst = new ArrayList<VideoNews>();
lst.add(new VideoNews(1, "喜洋洋", 20));
lst.add(new VideoNews(2, "变形金刚", 10));
lst.add(new VideoNews(3, "功夫熊猫", 20));
return lst;
}
}
 
/**
 * 新闻Servlet
 * 
 * @author 徐越
 * 
 */
public class ListServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;
private VideoNewsService vs = new VideoNewsServiceImpl();
 
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doPost(request, response);
}
 
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
List<VideoNews> news = vs.readNews();
JSONArray jsonarr = JSONArray.fromObject(news);
response.setCharacterEncoding("UTF-8");
response.setContentType("text/plain;charset=UTF-8");
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.getWriter().print(jsonarr);
}
}
 
Android项目
 
[java] view plaincopyprint?
public class VideoNewsServiceImpl implements VideoNewsService  
{  
  
    /** 
     * 获取最新视频资讯,从JSON文件中,解析效率高 
     *  
     * @return 
     * @throws Exception 
     */  
    public List<VideoNews> getNewsFromJson() throws Exception  
    {  
        List<VideoNews> lst = new ArrayList<VideoNews>();  
        String path = "http://xxx.xxx.xxx.xxx:8080/web/ListServlet";  
        URL url = new URL(path);  
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
        conn.setReadTimeout(5000);  
        conn.setRequestMethod("GET");  
        if (200 == conn.getResponseCode())  
        {  
            InputStream instream = conn.getInputStream();  
            lst = parseJSON(instream);  
        }  
        return lst;  
    }  
  
    /** 
     * 解析JSON 
     */  
    private List<VideoNews> parseJSON(InputStream instream) throws Exception  
    {  
        List<VideoNews> lst = new ArrayList<VideoNews>();  
        byte[] data = IOUtils.read(instream);  
        String jsonStr = new String(data);  
        JSONArray array = new JSONArray(jsonStr);  
        for (int i = 0; i < array.length(); i++)  
        {  
            JSONObject jsonObj = (JSONObject) array.getJSONObject(i);  
            VideoNews v = new VideoNews(jsonObj.getInt("id"),   
                    jsonObj.getString("title"), jsonObj.getInt("timeLength"));  
            lst.add(v);  
补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,