4月21日,中雨。春游三日,“峡谷寻芳,云台探胜,兴冲冲,看几回飞瀑流泉。”
采用JDBC方法主要问题是安全性不高,而且一旦要访问的数据量过多,容易出问题。另外,Android系统本身有对json或者xml直接解析的api,所以建议采用第二种方法,实用性与安全性都提高了。
1、服务器端生成和发布XML代码
(1)工程视图
(2)web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ServerForXML</display-name>
<servlet>
<servlet-name>ServletForXML</servlet-name>
<servlet-class>com.server.xml.ServletForXML</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletForXML</servlet-name>
<url-pattern>/ServletForXML</url-pattern>
</servlet-mapping>
</web-app>
(3)ServletForXML.java
package com.server.xml;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.server.domain.News;
import com.server.service.XMLService;
import com.server.service.implement.XMLServiceBean;
public class ServletForXML extends HttpServlet {
private static final long serialVersionUID = 1L;
private XMLService newsService = new XMLServiceBean();
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
List<News> newes = newsService.getLastNews();
request.setAttribute("newes", newes);
request.getRequestDispatcher("/WEB-INF/page/news.jsp").forward(request,
response);
}
}
(4)News.java
package com.server.domain;
public class News {
private Integer id;
private String title;
private Integer timelength;
public News(Integer id, String title, Integer timelength) {
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;
}
}
(5)XMLService.java
package com.server.service;
import java.util.List;
import com.server.domain.News;
public interface XMLService {
public List<News> getLastNews();
}
(6)
XMLServiceBean.java
package com.server.service.implement;
import java.util.ArrayList;
import java.util.List;
import com.server.domain.News;
import com.server.service.XMLService;
public class XMLServiceBean implements XMLService {
public List<News> getLastNews() {
List<News> newes = new ArrayList<News>();
newes.add(new News(10, "马航失联", 20));
newes.add(new News(45, "韩国沉船", 20));
newes.add(new News(89, "乌克兰局势", 50));
return newes;
}
}
(6)news.jsp
<%@ page language="java" contentType="text/xml; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><?xml version="1.0" encoding="UTF-8"?>
<newslist>
<c:forEach items="${newes}" var="news">
<news id="${news.id}">
<title>${news.title}</title>
<timelength>${news.timelength}</timelength>
</news>
</c:forEach>
</newslist>
(7)运行http://192.168.23.1:8080/ServerForXML/ServletForXML
注意:IP:192.168.23.1:8080,是WIFI共享精灵设置的虚拟的无线网卡的IP,IP自然由虚拟的无线路由分配,使用手机连接,会自动给手机分配同一网段的IP:如:192.168.23.2。
2、手机端代码
2、手机端代码
(1)工程视图
(2)
AndroidgetXml.java
4、
手机运行结果
-手机截屏
package com.androidgetxml;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.SimpleAdapter;
public class AndroidgetXml extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SimpleAdapter adapter = new SimpleAdapter(this, getData(), R.layout.main,
new String[] { "testid", "title", "timelength" },
new int[] { R.id.testid, R.id.title, R.id.timelength });
setListAdapter(adapter);
}
private List<Map<String, Object>> getData() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
try {
List<News> newes = NewsService.getLastNews();
for (News news : newes) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("testid", news.getId());
map.put("title", news.getTitle());
map.put("timelength", news.getTimelength());
list.add(map);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
}
(3)
News.java
package com.androidgetxml;
public class News {
private Integer id;
private String title;
private Integer timelength;
public News() {
}
public News(Integer id, String title, Integer timelength) {
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;
}
}
(4)
NewsService.java
package com.androidgetxml;
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;
public class NewsService {
public static List<News> getLastNews() throws Exception {
String path = "http://192.168.23.1:8080/ServerForXML/ServletForXML";
HttpURLConnection conn = (HttpURLConnection) new URL(path)
.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if (conn.getResponseCode() == 200) {
InputStream xml = conn.getInputStream();
return parseXML(xml);
}
return null;
}
private static List<News> parseXML(InputStream xml) throws Exception {
List<News> newes = null;
News news = null;
XmlPullParser pullParser = Xml.newPullParser();
pullParser.setInput(xml, "UTF-8");
int event = pullParser.getEventType();
while (event != XmlPullParser.END_DOCUMENT) {
switch (event) {
case XmlPullParser.START_DOCUMENT:
newes = new ArrayList<News>();
break;
case XmlPullParser.START_TAG:
if ("news".equals(pullParser.getName())) {
int id = new Integer(pullParser.getAttributeValue(0));
news = new News();
news.setId(id);
}
if ("title".equals(pullParser.getName())) {
news.setTitle(pullParser.nextText());
}
if ("timelength".equals(pullParser.getName())) {
news.setTimelength(new Integer(pullParser.nextText()));
}
break;
case XmlPullParser.END_TAG:
if ("news".equals(pullParser.getName())) {
newes.add(news);
news = null;
}
break;
}
event = pullParser.next();
}
return newes;
}
}
(5)
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".AndroidgetXml" >
<TextView
android:id="@+id/testid"
android:layout_width="50dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/title"
android:layout_width="150dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/timelength"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
(6)
strings.xml<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">AndroidgetXml</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
</resources>
(7)
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidgetxml"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.androidgetxml.AndroidgetXml"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
3、
安卓模拟器运行结果