您的位置:首页 > 移动开发 > Android开发

RSS阅读器(使用android解析技术解析xml文件并以列表的形式显示出来)

2012-12-25 16:49 351 查看

1.效果图

2.页面设计

(1)main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">

<ListView android:id="@+id/listview" android:layout_width="fill_parent"
android:layout_height="fill_parent"></ListView>
</LinearLayout>

(2)show.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView android:id="@+id/description" android:layout_width="fill_parent"
android:layout_height="wrap_content" text="描述" android:autoLink="web"/>
<ImageView android:id="@+id/image" android:layout_width="fill_parent"
android:layout_height="fill_parent" />

</LinearLayout>

3.功能的实现

(1)实体类

package cn.edu.bzu.entity;

/**
* 新闻解析的实体类
* @author Administrator
*
*/
public class News {
private String title;
private String link;
private String author;
private String guid;
private String image;
private String video;
private String category;
private String pubDate;
private String comments;
private String description;

public String getTitle() {
return title;
}

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

public String getLink() {
return link;
}

public void setLink(String link) {
this.link = link;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

public String getGuid() {
return guid;
}

public void setGuid(String guid) {
this.guid = guid;
}

public String getImage() {
return image;
}

public void setImage(String image) {
this.image = image;
}

public String getVideo() {
return video;
}

public void setVideo(String video) {
this.video = video;
}

public String getCategory() {
return category;
}

public void setCategory(String category) {
this.category = category;
}

public String getPubDate() {
return pubDate;
}

public void setPubDate(String pubDate) {
this.pubDate = pubDate;
}

public String getComments() {
return comments;
}

public void setComments(String comments) {
this.comments = comments;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

@Override
public String toString() {
return "News [author=" + author + ", category=" + category
+ ", comments=" + comments + ", description=" + description
+ ", guid=" + guid + ", image=" + image + ", link=" + link
+ ", pubDate=" + pubDate + ", title=" + title + ", video="
+ video + "]";
}

}

2.业务逻辑层

package cn.edu.bzu.service;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import cn.edu.bzu.entity.News;

/**
* 利用Pull解析技术解析文件的业务逻辑层
*
* @author Administrator
*
*/
public class PullXMLService {

// 把要解析的数据以输入流的方式传进来
public List<News> getNews(InputStream is) throws Throwable {
List<News> newsList = null;
News news = null;
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser parser = factory.newPullParser();
parser.setInput(is, "UTF-8");
int eventType = parser.getEventType();// 产生第一个事件
while (eventType != XmlPullParser.END_DOCUMENT) {// 只要不是文档结束
String name = parser.getName();// 获取解析器当前指向的元素的名称
switch (eventType) {

case XmlPullParser.START_DOCUMENT:
newsList = new ArrayList<News>();
break;
case XmlPullParser.START_TAG:

if ("item".equals(name)) {
news = new News();
}
if (news != null) {
if ("title".equals(name)) {
news.setTitle(parser.nextText());
}
if ("link".equals(name)) {
news.setLink(parser.nextText());
}
if ("author".equals(name)) {
news.setAuthor(parser.nextText());
}
if ("guid".equals(name)) {
news.setGuid(parser.nextText());
}
if ("image".equals(name)) {
news.setImage(parser.nextText());
}
if ("video".equals(name)) {
news.setVideo(parser.nextText());
}
if ("category".equals(name)) {
news.setCategory(parser.nextText());
}
if ("pubDate".equals(name)) {
news.setPubDate(parser.nextText());
}
if ("comments".equals(name)) {
news.setComments(parser.nextText());
}
if ("description".equals(name)) {
news.setDescription(parser.nextText());
}
}
break;
case XmlPullParser.END_TAG:

if ("item".equals(name)) {
newsList.add(news);
news = null;
}

}
// 进入下一个元素
eventType = parser.next();
}
return newsList;

}
}

3.测试类

package cn.edu.bzu.activity;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import cn.edu.bzu.entity.News;
import cn.edu.bzu.service.PullXMLService;
import android.test.AndroidTestCase;

public class PullParserTest extends AndroidTestCase {

public void testGetNews() throws Throwable {
URL url = new URL("http://blog.sina.com.cn/rss/1267454277.xml");
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();
InputStream is = urlConnection.getInputStream();
PullXMLService pullXMLService = new PullXMLService();
List<News> newList = pullXMLService.getNews(is);
for (News news : newList) {
System.out.println(news);
}
}
}

4.PullXMLParserActivity

package cn.edu.bzu.activity;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.edu.bzu.entity.News;
import cn.edu.bzu.service.PullXMLService;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class PullXMLParserActivity extends Activity {
ListView listView = null;
List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView) this.findViewById(R.id.listview);
System.out.println(listView);
data = getData();
System.out.println(data.size());
SimpleAdapter adapter = new SimpleAdapter(this, data,
android.R.layout.simple_list_item_2, new String[] { "title",
"comments" }, new int[] { android.R.id.text1,
android.R.id.text2 });

listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent();
intent.setClass(PullXMLParserActivity.this, ShowActivity.class);
HashMap<String, Object> item = data.get(position);
Bundle bundle = new Bundle();
bundle.putString("description",
(String) (item.get("description")));
intent.putExtras(bundle);
startActivity(intent);
}
});

}

public List<HashMap<String, Object>> getData() {

PullXMLService pullXMLService = new PullXMLService();
URL url = null;
try {
url = new URL("http://blog.sina.com.cn/rss/1267454277.xml");
} catch (MalformedURLException e) {
Toast.makeText(this, "URL拼写错误", Toast.LENGTH_LONG).show();
}
InputStream is = null;
try {
is = url.openStream();
} catch (IOException e) {
Toast.makeText(this, "无法打开输入流", Toast.LENGTH_LONG).show();
}
try {
List<News> newsList = pullXMLService.getNews(is);
for (News news : newsList) {
HashMap<String, Object> item = new HashMap<String, Object>();
item.put("title", news.getTitle());
item.put("link", news.getLink());
item.put("author", news.getAuthor());
item.put("category", news.getCategory());
item.put("comments", news.getComments());
item.put("description", news.getDescription());
item.put("guid", news.getGuid());
item.put("image", news.getImage());
item.put("pubdate", news.getPubDate());
item.put("video", news.getVideo());
data.add(item);
}
} catch (Throwable e) {
e.printStackTrace();
}
return data;
}
}

5.ShowActivity

package cn.edu.bzu.activity;

import java.io.IOExce
1cca8
ption;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class ShowActivity extends Activity {
private TextView description = null;
private ImageView image = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.show);
description = (TextView) this.findViewById(R.id.description);
Bundle bundle = getIntent().getExtras();
String descriptionValue = bundle.getString("description");
System.out.println(descriptionValue);
description.setText(descriptionValue);

}

private Bitmap returnBitMap(String url) {
URL myFileUrl = null;
Bitmap bitmap = null;
try {
myFileUrl = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}

}
阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: