首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在用AsyncTask解析SAXParser时添加SAXParser?

如何在用AsyncTask解析SAXParser时添加SAXParser?
EN

Stack Overflow用户
提问于 2012-08-16 14:35:51
回答 1查看 310关注 0票数 0

我需要在此添加一个AsyncTask,因为它不适用于API级别10-15,或者将姜饼添加到ICS中。试着添加,但我没有任何成功。

代码语言:javascript
复制
public class GBXMLParser extends Activity {
/** Create Object For SiteList Class */
SiteList sitesList = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    /** Create a new layout to display the view */
    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(1);
    /** Set the layout view to display */
    this.setContentView(layout);

    /** Create a new TextView array to display the results */
    TextView name[];
    TextView website[];
    TextView category[];

    try {
        /** Handling XML */
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();

        /** Send URL to parse XML Tags */
        URL sourceUrl = new URL("http://url.xml");

        /** Create handler to handle XML Tags ( extends DefaultHandler ) */
        MyXMLHandler myXMLHandler = new MyXMLHandler();
        xr.setContentHandler(myXMLHandler);
        xr.parse(new InputSource(sourceUrl.openStream()));
    } catch (Exception e) {
        System.out.println("XML Pasing Excpetion = " + e);
    }

    /** Get result from MyXMLHandler SitlesList Object */
    sitesList = MyXMLHandler.sitesList;

    /** Assign TextView array length by arrayList size */
    name = new TextView[sitesList.getName().size()];
    website = new TextView[sitesList.getName().size()];
    category = new TextView[sitesList.getName().size()];

    /** Set the result text in TextView and add it to layout */
    for (int i = 0; i < sitesList.getName().size(); i++) {
        name[i] = new TextView(this);
        name[i].setText("Name = " + sitesList.getName().get(i));
        website[i] = new TextView(this);
        website[i].setText("Website = " + sitesList.getWebsite().get(i));
        category[i] = new TextView(this);
        category[i].setText("Website Category = "
                + sitesList.getCategory().get(i));
        layout.addView(name[i]);
        layout.addView(website[i]);
        layout.addView(category[i]);
    }
}
}
EN

回答 1

Stack Overflow用户

发布于 2012-08-16 14:38:33

代码语言:javascript
复制
  public class MyAsyncTask extends AsyncTask<Void, Void, Result>{

                        private Activity activity;
                        private ProgressDialog progressDialog;

            public MyAsyncTask(Activity activity) {
                            super();
                this.activity = activity;
            }

            @Override
            protected void onPreExecute() {
            super.onPreExecute();
                progressDialog = ProgressDialog.show(activity, "Loading", "Loading", true);
            }

            @Override
            protected Result doInBackground(Void... v) {
            //do your stuff here
            return null;

            }

            @Override
            protected void onPostExecute(Result result) {
                progressDialog.dismiss();
                Toast.makeText(activity.getApplicationContext(), "Finished.", 
                        Toast.LENGTH_LONG).show();
            }


}

从活动中调用它:

代码语言:javascript
复制
MyAsyncTask task = new AsyncTask(myActivity.this);
task.execute();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11989539

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档