0

我正在尝试XML从 url 页面解析一个。为此,我使用了这个 IBM 示例中解释的 SAX 实现Adapter以及我从本文中获得的其他更改。我也尝试实现 anAsyncTask来进行解析并显示 aProgressDialog但我认为这是我的应用程序开始崩溃的地方。

我真的不知道如何在AsyncTask我的代码中实现,而且我相信我的糟糕实现导致我的应用程序强制关闭。

主要活动:

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;
    }


}
}

如何正确使用AsyncTask,以便我的解析器返回一个ArrayList然后我可以放入ArrayAdapter

4

2 回答 2

1

使用此代码

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();
    }

获取url数据方法

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();
}
于 2012-08-29T04:31:44.083 回答
1

AsyncTask<Params, Progress, Result>子类 ( )中的类型参数使用不当。重新编写AsyncTask子类。

  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;
    }
  }
于 2012-08-29T05:35:44.307 回答