我按照this link
如何使用XmlPullParser,的AsyncTask Android的一個URL
登錄貓讀取XML文件:
warning : org.xmlPull.v1.XmlPullParserException : expected : START_TAG { null } rss (position:START_TAG <html>@2:7 in java.io.InputStreamReader @41232f00)<br/> error : NullPoiterException
我一直在尋找和使用一些基本的測試方法但parser.require在readRss方法不跑到rss標籤
我想檢索標題的內容並鏈接標記到項目
對不起我的英文能力,我是新的android編程
非常感謝!從URL
XML內容
<rss xmlns:slash="http://purl.org/rss/1.0/modules/slash/" version="2.0">
<channel>
<title></title>
<description></description>
<pubDate></pubDate>
<generator></generator>
<link></link>
<item>
<title>CONTENTS</title>
<link>http://test.html/</link>
</item>
</channel>
</rss>
XmlParser的代碼
public class XmlParser {
private static final String nameSpace = null;
public XmlParser(){
}
public List<Item> parse(InputStream inputStream)
throws XmlPullParserException, IOException {
try {
XmlPullParser parser = Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(inputStream, null);
parser.nextTag();
return readRss(parser);
} finally {
inputStream.close();
}
}
private List<Item> readRss(XmlPullParser parser)
throws XmlPullParserException, IOException {
List<Item> itemList = new ArrayList<Item>();
int eventType = parser.getEventType();
System.out.println(parser.getName(); // <==== position is html
parser.require(XmlPullParser.START_TAG, nameSpace, "rss"); // <---- error
/** Code in here is failed**/
while (parser.next() != XmlPullParser.END_TAG) {
if (eventType!= XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
// Starts by looking for the Rss tag
if (name.equals("item")) {
itemList.add(readItem(parser));
} else {
skip(parser);
}
}
return itemList;
}
// This class represents a single entry (post) in the XML feed.
// It includes the data members "title," "link," and "summary."
public static class Item{
String title = "";
String link = "";
private Item() {
}
private Item(String title){
this.title = title;
}
private Item(String title, String link) {
this.title = title;
this.link = link;
}
}
// Parses the contents of an item. If it encounters a title, link tag, hands
// them
// off
// to their respective "read" methods for processing. Otherwise,
// skips the tag.
private Item readItem(XmlPullParser parser) throws XmlPullParserException,
IOException {
parser.require(XmlPullParser.START_TAG, nameSpace, "item");
String title = null;
String link = null;
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if (name.equals("title")) {
title = readTitle(parser);
/*} else if (name.equals("link")) {
link = readLink(parser);*/
}else{
skip(parser);
}
}
return new Item(title);
//return new Item(title, link);
}
// Skips tags the parser isn't interested in. Uses depth to handle nested tags. i.e.,
// if the next tag after a START_TAG isn't a matching END_TAG, it keeps going until it
// finds the matching END_TAG (as indicated by the value of "depth" being 0).
private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
if(parser.getEventType() != XmlPullParser.START_TAG){
throw new IllegalStateException();
}
int depth = 1;
while(depth != 0){
switch (parser.next()) {
case XmlPullParser.END_TAG:
depth--;
break;
case XmlPullParser.START_TAG:
depth++;
break;
}
}
}
// Processes title tags in the Rss.
private String readTitle(XmlPullParser parser)
throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, nameSpace, "title");
String title = readTitle(parser);
parser.require(XmlPullParser.END_TAG, nameSpace, "title");
return title;
}
// Processes link tags in the Rss.
/*private String readLink(XmlPullParser parser)
throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, nameSpace, "link");
String link = readLink(parser);
parser.require(XmlPullParser.END_TAG, nameSpace, "link");
return link;
}
}
ListActivity代碼
public class MainActivity extends ListActivity {
static final String URL = "http://vnexpress.net/rss/du-lich.rss";
ArrayAdapter<Item> arrAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new buildDataXml().execute(URL);
}
private class buildDataXml extends AsyncTask<String, Void, List<Item>> {
@Override
protected List<Item> doInBackground(String... params) {
try {
return loadXmlFromUrl(params[0]);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(List<Item> result) {
super.onPostExecute(result);
System.out.println(result.get(0));
arrAdapter = new ArrayAdapter<Item>(getApplicationContext(), android.R.layout.simple_list_item_1, result);
setListAdapter(arrAdapter);
}
}
private List<Item> loadXmlFromUrl(String strUrl) throws XmlPullParserException, IOException{
InputStream inputStream = null;
List<Item> item = null;
XmlParser parser = new XmlParser();
try {
inputStream = downloadUrl(strUrl);
System.out.println("InputStream : "+inputStream.available());
item = parser.parse(inputStream);
System.out.println("Item Size : " + item.size());
// Makes sure that the InputStream is closed after the app is
// finished using it.
} finally {
if (inputStream != null) {
inputStream.close();
}
}
return item;
}
private InputStream downloadUrl(String strUrl) throws IOException{
URL url = new URL(strUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.setRequestProperty("Content-Type", "application/xml");
// Starts the query
conn.connect();
InputStream stream = conn.getInputStream();
return stream;
}