当我只解析一个然后它正常运行时,我在我的应用程序中解析 RSS 提要,但我有 8 个不同的 URL 地址要在按钮上设置,我想为每个按钮单击解析 RSS 提要。我正在使用 SAX 解析器。我的代码如下
BaseFeedParser.java
package com.example.shareslab;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import com.example.shareslab.Message;
import android.sax.Element;
import android.sax.EndElementListener;
import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.util.Xml;
public class BaseFeedParser {
static String feedUrlString = "http://www.xxxx.com/rss.xml";
// names of the XML tags
static final String RSS = "rss";
static final String CHANNEL = "channel";
static final String ITEM = "item";
static final String PUB_DATE = "pubDate";
static final String DESCRIPTION = "description";
static final String LINK = "link";
static final String TITLE = "title";
private final URL feedUrl;
protected BaseFeedParser(){
try {
this.feedUrl = new URL(feedUrlString);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
protected InputStream getInputStream() {
try {
return feedUrl.openConnection().getInputStream();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public List<Message> parse() {
final Message currentMessage = new Message();
RootElement root = new RootElement(RSS);
final List<Message> messages = new ArrayList<Message>();
Element itemlist = root.getChild(CHANNEL);
Element item = itemlist.getChild(ITEM);
item.setEndElementListener(new EndElementListener(){
@Override
public void end() {
messages.add(currentMessage.copy());
}
});
item.getChild(TITLE).setEndTextElementListener(new EndTextElementListener(){
@Override
public void end(String body) {
currentMessage.setTitle(body);
}
});
item.getChild(LINK).setEndTextElementListener(new EndTextElementListener(){
@Override
public void end(String body) {
currentMessage.setLink(body);
}
});
item.getChild(DESCRIPTION).setEndTextElementListener(new EndTextElementListener(){
@Override
public void end(String body) {
currentMessage.setDescription(body);
}
});
item.getChild(PUB_DATE).setEndTextElementListener(new EndTextElementListener(){
@Override
public void end(String body) {
currentMessage.setDate(body);
}
});
try {
Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
} catch (Exception e) {
throw new RuntimeException(e);
}
return messages;
}
}
我有 8 个按钮和 8 个 RSS 提要的 URL 地址,我想为每个按钮解析它
消息列表.java
package com.example.shareslab;
import java.util.ArrayList;
import java.util.Arrays;
import com.example.shareslab.R;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MessageList extends ListActivity implements OnClickListener {
Button home,socialmedia,tech,usworld,business,fashion,people,political;
public static String singleDescription;
public static String title,URLToPost,imageURL;
public static ArrayList<String> galleryImages;
private static class EfficientAdapter extends BaseAdapter
{
private LayoutInflater mInflater;
public EfficientAdapter(Context context)
{
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
System.out.println("description COUNT : "+SplashActivity.description.size());
return SplashActivity.description.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.inflate_list_item, null);
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.inflate_title);
holder.des = (TextView) convertView.findViewById(R.id.inflate_description);
holder.im= (ImageView) convertView.findViewById(R.id.inflate_image);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
UrlImageViewHelper.setUrlDrawable(holder.im, SplashActivity.imageURLAmit.get(position),null);
holder.title.setText(SplashActivity.titles.get(position));
holder.des.setText(SplashActivity.description.get(position));
return convertView;
}
public static class ViewHolder {
TextView title,des;
ImageView im;
}
} // close class Efficent adapter
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
home=(Button)findViewById(R.id.buttonhome);
socialmedia=(Button)findViewById(R.id.buttonsocial);
tech=(Button)findViewById(R.id.buttontech);
usworld=(Button)findViewById(R.id.buttonusworld);
business=(Button)findViewById(R.id.buttonbusiness);
fashion=(Button)findViewById(R.id.buttonfashion);
people=(Button)findViewById(R.id.buttonpeople);
political=(Button)findViewById(R.id.buttonpolitical);
home.setOnClickListener(this);
socialmedia.setOnClickListener(this);
tech.setOnClickListener(this);
usworld.setOnClickListener(this);
business.setOnClickListener(this);
fashion.setOnClickListener(this);
people.setOnClickListener(this);
political.setOnClickListener(this);
ListView listView = getListView();
listView.setTextFilterEnabled(true);
this.setListAdapter(new EfficientAdapter(this));
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
galleryImages=new ArrayList<String>();
singleDescription=SplashActivity.description.get(position);
title=SplashActivity.titles.get(position);
URLToPost=SplashActivity.link.get(position);
imageURL=SplashActivity.imageURLAmit.get(position);
System.out.println("ON CLICK URL: "+URLToPost);
galleryImages.addAll(Arrays.asList(SplashActivity.arrays[position]));
startActivity(new Intent(MessageList.this,MessageListDetail.class));
}
});
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}