我正在尝试为 adnroid 创建新闻应用程序。我从网站解析 RSS。现在我有 200 件物品,但将来会是 2000 件或更多。我使用本教程创建带有图像和文本的 ListView。 http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/
但我使用不同的解析器。
我遇到的问题是,它s - xml file load full. I
通过 10-15 个项目搜索部分加载和解析的方式。
例如,首先加载 10 个项目,用户向下滚动,然后加载接下来的 10 个项目。是否可以使用此解析器来完成,或者我应该使用其他一些:JSON 等/?
代码: 主要活动:
private NewsParser parser;
private List<PostItem> messages;
parser = new NewsParser();
messages = parser.parse();
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
try
{
for (PostItem msg : messages){
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put("Date", msg.getDate());
map.put("Title", msg.getTitle());
map.put("Description", msg.getDescription());
map.put("imgUrl", msg.getImgUrl());
// adding HashList to ArrayList
menuItems.add(map);
}
}
catch(Exception e)
{
e.printStackTrace();
}
return menuItems;
新闻解析器:
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 android.sax.Element;
import android.sax.EndElementListener;
import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.util.Xml;
public class NewsParser {
/**
* We transfer the address bar of RSS feeds in the URL, connect to the server at that address
* and receive the data stream from the RSS feed.
* @return
*/
protected InputStream getInputStream() {
URL feedUrl = null;
try {
feedUrl = new URL("http://www.mysite.com/test_xml");
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
try {
return feedUrl.openConnection().getInputStream();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* Parser XML
* @return List<PostItem>
*/
public List<PostItem> parse() {
final PostItem currentPost = new PostItem();
final List<PostItem> messages = new ArrayList<PostItem>();
RootElement root = new RootElement("rss");
Element channel = root.getChild("channel");
Element item = channel.getChild("item");
item.setEndElementListener(new EndElementListener(){
public void end() {
messages.add(currentPost.copy());
}
});
item.getChild("title").setEndTextElementListener(new EndTextElementListener(){
public void end(String body) {
currentPost.setTitle(body);
}
});
item.getChild("link").setEndTextElementListener(new EndTextElementListener(){
public void end(String body) {
currentPost.setLink(body);
}
});
item.getChild("description").setEndTextElementListener(new EndTextElementListener(){
public void end(String body) {
currentPost.setDescription(body);
}
});
item.getChild("pubDate").setEndTextElementListener(new EndTextElementListener(){
public void end(String body) {
currentPost.setDate(body);
}
});
item.getChild("imgUrl").setEndTextElementListener(new EndTextElementListener(){
public void end(String body) {
currentPost.setImgUrl(body);
}
});
item.getChild("imgUrlBig").setEndTextElementListener(new EndTextElementListener(){
public void end(String body) {
currentPost.setImgUrlBig(body);
}
});
try {
Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
} catch (Exception e) {
throw new RuntimeException(e);
}
return messages;
}
}
邮政项目:
import java.net.MalformedURLException;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class PostItem {
static SimpleDateFormat FORMATTER = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z", Locale.US);
static SimpleDateFormat OUT_FORMATTER = new SimpleDateFormat("dd.MM.yyyy", Locale.US);
private String title;
private URL link;
private String linkText;
private String description;
private Date date;
private String imgUrl;
private String imgUrlBig;
/**
* Setter для title
* @param title
*/
public void setTitle(String title) {
this.title = title.trim();
}
/**
* Getter для title
* @return
*/
public String getTitle() {
return title;
}
/**
* Setter для description
* @param description
*/
public void setDescription(String description) {
this.description = description.trim();
}
/**
* Getter для description
* @return
*/
public String getDescription() {
return description;
}
/**
* Setter для link и linkText
* @param link
*/
public void setLink(String link) {
this.linkText = link.trim();
try {
this.link = new URL(link);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
/**
* Getter для link
* @return
*/
public URL getLink() {
return link;
}
/**
* Getter для linkText
* @return
*/
public String getLinkText() {
return linkText;
}
/**
* Setter для date
* @param date
*/
public void setDate(String date) {
try {
this.date = FORMATTER.parse(date.trim());
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
/**
* Getter для date
* @return
*/
public String getDate() {
return OUT_FORMATTER.format(this.date);
}
/**
* Setter для imgUrl
* @param imgUrl
*/
public void setImgUrl(String imgUrl) {
this.imgUrl = imgUrl.trim();
}
/**
* Getter для imgUrl
* @return
*/
public String getImgUrl() {
return imgUrl;
}
/**
* Setter для imgUrlBig
* @param imgUrl
*/
public void setImgUrlBig(String imgUrlBig) {
this.imgUrlBig = imgUrlBig.trim();
}
/**
* Getter для imgUrlBig
* @return
*/
public String getImgUrlBig() {
return imgUrlBig;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(linkText);
return sb.toString();
}
public PostItem copy() {
PostItem copy = new PostItem();
copy.title = title;
copy.link = link;
copy.linkText = linkText;
copy.description = description;
copy.date = date;
copy.imgUrl = imgUrl;
copy.imgUrlBig = imgUrlBig;
return copy;
}
}