Im trying to code an android RSS reader that will (for now) parse the xml from this MangaPanda List. I’m using this Android XML example for basic structure (and because it uses XMLPullParser) as well as this example because of its clear explanation of its EfficientAdapter, AsyncTask, and ListView usage.
So far I think I’ve “merged” the two pretty well but I cant quite figure out how to properly execute the parsing process.
My question is: How would I properly implement the LoadXmlFromNetwork() so that it would return the proper format for my adapter?
MainActivity:
//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;
}
}
MainParser:
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;
}
}
}
}
My EfficientAdapter:
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;
}
}
On a side note: I didn’t know how much of my code would be necessary to properly explain my problem so please feel free to edit out unnecessary parts.
Change your asynctask like that: