Davidrd91
Davidrd91

Reputation: 169

Parsing Xml with Sax on Android

I'm trying to parse an XML from a url page. To do so I have used the SAX implementation explained in this IBM example with the Adapter and other changes I got from this article. I've also tried to implement an AsyncTask to do the parsing and show a ProgressDialog but I think this is where my application starts to break down.

I don't really know exactly how to implement the AsyncTask into my code, and I believe my poor implementation is causing my app to force close.

MainActivity:

public class MainActivity extends Activity {
/** Called when the activity is first created. */
ListView lv1;
ProgressDialog ShowProgress;
public static ArrayList<MangaItem> MangaItemList = new ArrayList<MangaItem>();


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    lv1 = (ListView) findViewById(R.id.listView1);

    ShowProgress = ProgressDialog.show(MainActivity.this, "",
            "Loading. Please wait...", true);
    //new loadingTask().execute("http://www.mangapanda.com/alphabetical");
    new loadFeedTask().execute();

    lv1.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri
                    .parse(MangaItemList.get(position).getMangaLink()));
            startActivity(intent);

        }
    });

}

class loadFeedTask extends AsyncTask<String, Void, ArrayList<MangaItem>> {
    private String feedUrl;

    protected void onPostExecute(String s) {
        lv1.setAdapter(new EfficientAdapter(MainActivity.this, MangaItemList));
        //new MangaParserTask().execute();
        ShowProgress.dismiss();

    }

    protected ArrayList<MangaItem> doInBackground(String... params) {
        ArrayList<MangaItem> ParsedMangaItemList = new ArrayList<MangaItem>();
        feedUrl = "http://www.mangapanda.com/alphabetical"; 
        FeedParser parser = new SaxFeedParser(feedUrl);
        ParsedMangaItemList = parser.parse();
        for (MangaItem mitem : ParsedMangaItemList) {
            MangaItemList.add(mitem);
        }
        return MangaItemList;
    }


}
}

How can I properly use AsyncTask so that my parser will return an ArrayList that I can then put into an ArrayAdapter

Upvotes: 0

Views: 288

Answers (2)

KV Prajapati
KV Prajapati

Reputation: 94645

Improper use of type parameters in subclass (AsyncTask<Params, Progress, Result>). Re-write the AsyncTask sub-class.

  class loadFeedTask extends AsyncTask<String, Void, ArrayList<MangaItem>> {
    protected void onPostExecute(ArrayList<MangaItem> list) {
        lv1.setAdapter(new EfficientAdapter(MainActivity.this, list));
        ShowProgress.dismiss();
    }
    protected ArrayList<MangaItem> doInBackground(String... params) {
        ArrayList<MangaItem> list=null;
        String feedUrl = "http://www.mangapanda.com/alphabetical"; 
        FeedParser parser = new SaxFeedParser(feedUrl);
        list = parser.parse();
        MangaItemList=list;
        return list;
    }
  }

Upvotes: 1

SubbaReddy PolamReddy
SubbaReddy PolamReddy

Reputation: 2113

use this code

try {

        items = new ArrayList<String>();

        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();
        xpp.setInput(new InputStreamReader(
                getUrlData(" url")));

        while (xpp.getEventType() != XmlPullParser.END_DOCUMENT) {
            Log.i(TAG, "doc started");
            if (xpp.getEventType() == XmlPullParser.START_TAG) {
                if (xpp.getName().equals("entry")) {
                    items.add(xpp.getAttributeValue(0));
                }
            }
            xpp.next();

        }
    } catch (Throwable t) {
        Toast.makeText(this, "Request failed: " + t.toString(),
                Toast.LENGTH_LONG).show();
    }

get url data method

public InputStream getUrlData(String url) throws URISyntaxException,
        ClientProtocolException, IOException {

    DefaultHttpClient client = new DefaultHttpClient();
    HttpGet method = new HttpGet(new URI(url));
    HttpResponse res = client.execute(method);
    return res.getEntity().getContent();
}

Upvotes: 1

Related Questions