简述
此技术结合JSP技术,在JSP上的数据,可以用XML传输到安卓客户端,然后用户用手机查询到WEB应用上的信息(可以看到数据库保存的讯息)
学到的东西:
如何从网络中得到自定义的数据,得到自定义数据后,又如何完成对它的解析,然后显示出来
WEB应用部分
首先是WEB应用部分,写的是一个视屏网站中视频的信息,为了方便看,没有加入数据库数据,只是自己定义了一些数据来测试:
采用JSP写的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,IOException { //得到所有的视频信息 List<News> videos=service.getLastNews(); //把视频信息放在request上 request.setAttribute("videos",videos); //转发至videonews.jsp界面(一个XML) request.getRequestDispatcher("/WEB-INF/Page/videonews.jsp").forward(request,response); } }
JSP界面(其实是一个XML)
//注意<?xmlversion="1.0"encoding="UTF-8"?>一定要写在第一行
//此处采用了jstl标签解析技术(详情请看http://blog.csdn.net/acmman/article/details/38750809)
//用jstl标签解析技术必须导入jstl.jar与standard.jar
<%@ page language="java" import="java.util.*" contentType="text/xml; charset=UTF-8" pageEncoding="UTF-8"%><?xml version="1.0" encoding="UTF-8"?><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <videonews><c:forEach items="${videos}" var="video"> <news id="${video.id}"> <title>${video.title}</title> <timelength>${video.timelength}</timelength> </news> </c:forEach> </videonews>
业务类VideoNewsService接口
package deu.hpu.servise.impl; import java.util.List; import deu.hpu.servise.dima.News; public interface VideoNewsService { /* * 获取最新视频资讯 * */ public abstract List<News> getLastNews(); } 业务类VideoNewsServiceImpl实现类 package deu.hpu.servise.impl; import java.util.ArrayList; import java.util.List; import deu.hpu.servise.dima.News; public class VideoNewsServiceImpl implements VideoNewsService { public List<News> getLastNews(){ List<News> newses=new ArrayList<News>(); newses.add(new News(12,"古剑奇谭",90)); newses.add(new News(45,"万万没想到",45)); newses.add(new News(89,"超验骇客",120)); return newses; } }
News的类
package deu.hpu.servise.dima; public class News { private Integer id; private String title; private Integer timelength; public News(){} public News(Integer id,String title,Integer timelength) { super(); this.id = id; this.title = title; this.timelength = timelength; } public Integer getid() { return id; } public void setid(Integer id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public Integer getTimelength() { return timelength; } public void setTimelength(Integer timelength) { this.timelength = timelength; } }
一个简单的WEB应用出来了,在浏览器访问Servlet效果:
Android客户端部分
主界面布局:
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/listView" /> </LinearLayout>
列表元素ListView的布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="230dp" android:layout_height="wrap_content" android:id="@+id/title" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/timelength" /> </LinearLayout>
MainActivity.java
package com.example.vedionewses; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.widget.ListView; import android.widget.SimpleAdapter; import deu.hpu.servise.dima.News; import en.deu.service.VideoService; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ListView listview=(ListView) findViewById(R.id.listView); try { List<News> videos=VideoService.getLastNews(); List<HashMap<String,Object>> data=new ArrayList<HashMap<String,Object>>(); for(News news:videos){ HashMap<String,Object> item=new HashMap<String,Object>(); item.put("id",news.getid()); item.put("title",news.getTitle()); item.put("timelength",news.getTimelength()); data.add(item); } SimpleAdapter adapter=new SimpleAdapter(this,data,R.layout.item,new String []{"title","timelength"},new int []{R.id.title,R.id.timelength}); listview.setAdapter(adapter); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
上面出现的VideoService类以及其方法(其中用XML解析器解析传来的XML文件)
package en.deu.service; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.List; import org.xmlpull.v1.XmlPullParser; import android.util.Xml; import deu.hpu.servise.dima.News; public class VideoService { /* * 获取最新的视频资讯 * */ public static List<News> getLastNews() throws Exception{ String path="http://10.20.124.72:8080/videonews/ListServlet"; 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 parseXML(instream); } return null; } /* * 解析服务器返回的XML数据 * */ //parser.getName()得到字段的名称,parser.getText()得到字段的内容 /*如<news id="12"> <title>古剑奇谭</title> <timelength>90</timelength> </news> parser.getName()可以得到news、title、timelength parser.getText()可以得到title、timelength中的“古剑奇谭”和90 */ private static List<News> parseXML(InputStream instream) throws Exception{ List<News> newses=new ArrayList<News>(); News news=null; //par技术,通过安卓提供的工具类new一个XML破解器 XmlPullParser parser=Xml.newPullParser(); parser.setInput(instream,"UTF-8"); //得到开始文档事件 int event=parser.getEventType(); //循环,让它不断的进入后面的节点 //只要不等于文档的结束事件,就不断的循环 while(event!=XmlPullParser.END_DOCUMENT){ switch (event) { case XmlPullParser.START_TAG: if("news".equals(parser.getName())){ int id= new Integer(parser.getAttributeValue(0)); news=new News(); news.setid(id); }else if("title".equals(parser.getName())){ news.setTitle(parser.nextText()); } else if("timelength".equals(parser.getName())){ news.setTimelength(new Integer(parser.nextText())); } break; case XmlPullParser.END_TAG: if("news".equals(parser.getName())){ //把这一组的new给添加到ArrayList<News>容器中 newses.add(news); //将news清空,准备下一组 news=null; } break; default: break; } event=parser.next(); } return newses; } }
这里也有一个News类,和WEB应用中的一模一样,就不再写了
最后要在配置文件AndroidMainifest.xml中加入访问internet的权限,不然手机访问不了网络:
<uses-permission android:name="android.permission.INTERNET"/>
运行虚拟机,效果如下: