欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android自学笔记 博客分类: android web 

程序员文章站 2024-03-21 21:34:40
...
==================================2012-12-11=======================
访问web的xml数据
1、web端
domain:
package com.caiz.web.domain;

import java.util.Date;

/**
* 新闻实体类
* @author HuangYucai
*/
public class News {
private Long id;
private String title;
private Date createTime;

public News() {
}
public News(Long id, String title, Date createTime) {
this.id = id;
this.title = title;
this.createTime = createTime;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public Date getCreateTime() {
return createTime;
}

public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
}


servlet:

package com.caiz.web.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.caiz.web.domain.News;
import com.caiz.web.service.INewsService;

/**
* 新闻的service类
* @author HuangYucai
*
*/
public class NewsServiceImpl implements INewsService {


@Override
public List<News> getNewsList(){
List<News> newsList=new ArrayList<News>();
Date dt=new Date();
newsList.add(new News(1L,"国家兴亡,匹夫有责",dt));
newsList.add(new News(2L,"静水流深,谦者有为",dt));
newsList.add(new News(3L,"自助者,天助之",dt));
return newsList;
}
}
package com.caiz.web.service;

import java.util.List;

import com.caiz.web.domain.News;

public interface INewsService {

public abstract List<News> getNewsList();

}

jsp
<%@ page language="java" contentType="text/xml; charset=utf-8" pageEncoding="utf-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%> <%@ taglib  prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> <?xml version="1.0" encoding="utf-8"?>
<root>
<c:forEach items="${newsList}" var="news">
<news id="${news.id}">
<title>${news.title}</title>
<createTime>
<fmt:formatDate value="${news.createTime}" pattern="yyyy-MM-dd"/>
</createTime>
</news>
</c:forEach>
</root>

servlet:
package com.caiz.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.caiz.web.domain.News;
import com.caiz.web.service.INewsService;
import com.caiz.web.service.impl.NewsServiceImpl;

/**
* Servlet implementation class NewsServeLet
*/
@WebServlet("/NewsServeLet")
public class NewsServelet extends HttpServlet {
private static final long serialVersionUID = 1L;
   
private INewsService newsService;

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
newsService=new NewsServiceImpl();
List<News> newsList=newsService.getNewsList();
request.setAttribute("newsList", newsList);
request.getRequestDispatcher("news_xml.jsp").forward(request, response);

}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}

}

==============================2012-12-12============================
android端
package com.caiz.news.service;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;

import android.util.Log;
import android.util.Xml;

import com.caiz.news.domain.News;

public class NewsService {

public static List<News> getNewsList() throws Exception {
String path = "http://192.168.0.186:8080/web/NewsServeLet";
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if (conn.getResponseCode() == 200) {
InputStream iStream = conn.getInputStream();
Log.i("net", "OK");
List<News> newsList=ParseXML(iStream);
if(newsList!=null){
Log.i("size", "OK"+newsList.size());
System.out.print("size=="+newsList.size());
}else{
Log.i("size", "0000000");
System.out.print("size==0000");
}
return newsList;
}
List<News> newsList =new ArrayList<News>();
newsList.add(new News(1L,"cccccccc",new Date()));
return newsList;
}

public static String ParseString(InputStream iStream) throws Exception {

if(iStream==null){
return "null";
}
ByteArrayOutputStream bos=new ByteArrayOutputStream();
byte[] buf=new byte[1024];
int len=0;
while((len=iStream.read(buf))>0){
bos.write(buf,0,len);
}

return new String(bos.toByteArray());
}

public static List<News> ParseXML(InputStream iStream) throws Exception {
XmlPullParser parser = Xml.newPullParser();
parser.setInput(iStream, "UTF-8");
int event = parser.getEventType();
News news = null;
ArrayList<News> newsList = new ArrayList<News>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
while (event != XmlPullParser.END_DOCUMENT) {
switch (event) {
case XmlPullParser.START_TAG:
if ("news".equals(parser.getName())) {
Long id = new Long(parser.getAttributeValue(0));
news = new News();
news.setId(id);
}else if ("title".equals(parser.getName())) {
news.setTitle(parser.nextText());
}else if("createTime".equals(parser.getName())) {
news.setCreateTime(sdf.parse(parser.nextText()));
}
break;
case XmlPullParser.END_TAG:
if ("news".equals(parser.getName())) {
newsList.add(news);
news=null;
}
break;
}
event = parser.next();
}

return newsList;
}
}

package com.caiz.news;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.caiz.news.domain.News;
import com.caiz.news.service.NewsService;

public class NewsActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listView = (ListView) this.findViewById(R.id.newsView);
try {
List<News> newsList = NewsService.getNewsList();
List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
if(newsList!=null){
Log.i("length",String.valueOf(newsList.size()));
}else{
Log.i("length","null");
}
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
for (News news : newsList) {
HashMap<String, Object> mps = new HashMap<String, Object>();
mps.put("id", news.getId());
mps.put("title", news.getTitle());
mps.put("createTime", df.format(news.getCreateTime()));
data.add(mps);
}
SimpleAdapter adapter = new SimpleAdapter(this, data,
R.layout.items, new String[] { "title", "createTime" },
new int[] { R.id.title, R.id.createTime });

listView.setAdapter(adapter);
} catch (Exception e) {
e.printStackTrace();
}
}
}

相关标签: web