gpt4 book ai didi

android - 使用 xml 从 rss 提要中获取数据,然后存储在 ArrayList 中

转载 作者:行者123 更新时间:2023-11-29 22:19:57 25 4
gpt4 key购买 nike

我正在制作一个 android 应用程序以从 rss 提要中获取数据 https://spreadsheets.google.com/feeds/list/0AgSdFM6D0vX4dE5iQ0FfMGVWWGpieDZJY2lHX3I1S3c/od6/private/full

谁能提出一个从 xml 中获取值 gsx:brand 并将其存储在 Arraylist 中的想法

最佳答案

这是我做的一个例子,你可以看看这个并相应地工作,

MainRSSSDemo.java

public class MainRSSDemo extends Activity {

private List<Message> messages;
static String feedUrl = "http://rss.news.yahoo.com/rss/terrorism";
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";
static final String URL = "url";
static final String MEDIA = "content";
private InputStream is;
private URL feed_Url = null;
private ListView lv;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv =(ListView)findViewById(R.id.ListView01);

try {
feed_Url = new URL(feedUrl);
is = feed_Url.openConnection().getInputStream();
messages = parse();
is.close();

} catch (Exception e) {
Log.i("err", e.toString());
e.printStackTrace();
}

final List<String> titles = new ArrayList<String>(messages.size());
for (Message msg : messages){
titles.add(msg.getTitle());
}
lv.setAdapter(new ArrayAdapter<String>(this,R.layout.row,titles));
lv.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,long id) {

Toast.makeText(getBaseContext(),titles.get(position), Toast.LENGTH_LONG).show();

}
});
}

public List<Message> parse() {
List<Message> messages = null;
XmlPullParser parser = Xml.newPullParser();
try {
parser.setInput(is, null);
int eventType = parser.getEventType();
Message currentMessage = null;
boolean done = false;
while (eventType != XmlPullParser.END_DOCUMENT && !done){
String name = null;
switch (eventType){
case XmlPullParser.START_DOCUMENT:
messages = new ArrayList<Message>();
break;
case XmlPullParser.START_TAG:
name = parser.getName();
if (name.equalsIgnoreCase(ITEM)) {
currentMessage = new Message();
} else if (currentMessage != null) {
if (name.equalsIgnoreCase(LINK)) {
currentMessage.setLink(parser.nextText());
} else if (name.equalsIgnoreCase(DESCRIPTION)) {
String parse = parser.nextText().toString();
if (parse.contains("</a>")) {
parse = parse.substring(parse.indexOf("</a>") + 4, parse.lastIndexOf("</p>"));
currentMessage.setDescription(parse.toString());
} else {
currentMessage.setDescription(parse);
}
} else if (name.equalsIgnoreCase(PUB_DATE)) {
currentMessage.setDate(parser.nextText());
} else if (name.equalsIgnoreCase(TITLE)) {
currentMessage.setTitle(parser.nextText());
} else if (name.equalsIgnoreCase(MEDIA)) {
currentMessage.setImageUrl(parser.getAttributeValue(0));
parser.nextText();
}
}
break;
case XmlPullParser.END_TAG:
name = parser.getName();
if (name.equalsIgnoreCase(ITEM) && currentMessage != null){
messages.add(currentMessage);
} else if (name.equalsIgnoreCase(CHANNEL)){
done = true;
}
break;
}
eventType = parser.next();
}
} catch (Exception e) {

Log.e("AndroidNews::PullFeedParser", e.getMessage(), e);
throw new RuntimeException(e);
}
return messages;
}
}

Message.java

public class Message implements Comparable<Message> {
static SimpleDateFormat FORMATTER = new SimpleDateFormat(
"EEE, dd MMM yyyy HH:mm:ss Z");
private String title;
private URL link;
private String description;
private Date date;
private String image_url;

public void setImageUrl(String ImageUrl) {
image_url = ImageUrl;
}

public String getImageUrl() {
return image_url;
}

public String getTitle() {
return title;
}

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

// getters and setters omitted for brevity
public URL getLink() {
return link;
}

public void setLink(String link) {
try {
this.link = new URL(link);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}

public String getDescription() {
return description;
}

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

public String getDate() {
return FORMATTER.format(this.date);
}

public void setDate(String date) {
// pad the date if necessary
while (!date.endsWith("00")) {
date += "0";
}
try {
this.date = FORMATTER.parse(date.trim());
} catch (ParseException e) {
throw new RuntimeException(e);
}
}

public Message copy() {
Message copy = new Message();
copy.title = title;
copy.link = link;
copy.description = description;
copy.date = date;

return copy;
}

public int compareTo(Message another) {
if (another == null)
return 1;
// sort descending, most recent first
return another.date.compareTo(date);
}
}

关于android - 使用 xml 从 rss 提要中获取数据,然后存储在 ArrayList 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7496256/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com