我正在尝试编写一个 android RSS 阅读器,它将(暂时)解析来自这个 MangaPanda List的 xml 。我将这个 Android XML 示例用于基本结构(并且因为它使用 XMLPullParser)以及这个示例,因为它清楚地解释了它的EfficientAdapter
、AsyncTask
和ListView
用法。
到目前为止,我认为我已经很好地“合并”了这两者,但我无法弄清楚如何正确执行解析过程。
我的问题是:我将如何正确实现,LoadXmlFromNetwork()
以便它为我的适配器返回正确的格式?
主要活动:
//Implementation of AsyncTask used to download XML feed from stackoverflow.com.
private class loadingTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
try {
return loadXmlFromNetwork(urls[0]);
} catch (IOException e) {
return getResources().getString(R.string.hello_world);
} catch (XmlPullParserException e) {
return getResources().getString(R.string.menu_settings);
}
}
@Override
protected void onPostExecute(String result) {
setContentView(R.layout.activity_main);
// Displays the HTML string in the UI via a WebView
listview1.setAdapter(new EfficientAdapter(MainActivity.this, Manga));
ShowProgress.dismiss();
}
}
public Manga loadXmlFromNetwork(String urlString) throws XmlPullParserException, IOException {
InputStream stream = null;
// Instantiate the parser
MainParser MainParser = new MainParser();
List<Manga> mangas = null;
String mangaAlpha = null;
String mangaName = null;
boolean mangaComplete = false;
try {
stream = downloadUrl(urlString);
mangas = MainParser.parse(stream);
// Makes sure that the InputStream is closed after the app is
// finished using it.
} finally {
if (stream != null) {
stream.close();
}
}
// StackOverflowXmlParser returns a List (called "entries") of Entry objects.
// Each Entry object represents a single post in the XML feed.
// This section processes the entries list to combine each entry with HTML markup.
// Each entry is displayed in the UI as a link that optionally includes
// a text summary.
return mangas;
}
// Given a string representation of a URL, sets up a connection and gets
// an input stream.
private InputStream downloadUrl(String urlString) throws IOException {
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
// Starts the query
conn.connect();
InputStream stream = conn.getInputStream();
return stream;
}
}
主解析器:
public class MainParser {
// We don't use namespaces
private static final String ns = null;
public List<Manga> parse(InputStream in) throws XmlPullParserException, IOException {
try {
XmlPullParser parser = Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(in, null);
parser.nextTag();
return readFeed(parser);
} finally {
in.close();
}
}
private List<Manga> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
List<Manga> mangas = new ArrayList<Manga>();
parser.require(XmlPullParser.START_TAG, ns, "feed");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
// Starts by looking for the entry tag
if (name.equals("li")) {
mangas.add(readTag(parser));
} else {
skip(parser);
}
}
return mangas;
}
private Manga readTag(XmlPullParser parser) throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, ns, "li");
String mangaAlpha =null;
String mangaName = null;
String mangaLink = null;
boolean mangaComplete = false;
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if (name.equals("a")) {
mangaAlpha = parser.getAttributeValue(null, "name");
} else if (name.equals("a")) {
mangaName = parser.getText();
} else if (name.equals("a")) {
mangaLink = parser.getAttributeValue(null, "href");
} else if (name.equals("span")) {
mangaComplete = true;
} else {
skip(parser);
}
}
return new Manga(mangaAlpha, mangaName, mangaLink, mangaComplete);
}
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;
}
}
}
}
我的高效适配器:
public class EfficientAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<Manga> mangadata;
private static LayoutInflater inflater = null;
//public ImageLoader imageLoader;
ViewHolder holder;
EfficientAdapter(Activity a, ArrayList<Manga> d) {
activity = a;
mangadata = d;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//imageLoader = new ImageLoader(activity.getApplicationContext());
}
@Override
public int getCount() {
return mangadata.toArray().length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public static class ViewHolder {
public TextView label;
public TextView addr;
//public ImageView image;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null) {
vi = inflater.inflate(R.layout.textholder, null);
holder = new ViewHolder();
holder.label = (TextView) vi.findViewById(R.id.textView1);
holder.addr = (TextView) vi.findViewById(R.id.textView2);
//holder.image = (ImageView) vi.findViewById(R.id.icon);
vi.setTag(holder);
} else
holder = (ViewHolder) vi.getTag();
holder.label.setText(mangadata.get(position).getMangaAlpha());
holder.addr.setText(mangadata.get(position).getMangaName());
//imageLoader.DisplayImage((data.get(position).getThumbnail()), activity,
// holder.image, 72, 72);
return vi;
}
}
附带说明:我不知道要正确解释我的问题需要多少代码,所以请随时编辑掉不必要的部分。