java - 如何在使用SAXParser解析XML中添加AsyncTask?

标签 java android

I need to add an AsyncTask with this because its not working on API LEVEL 10-15., or Gingerbread to ICS. tried to add but I dont have any success.

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

最佳答案

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


}

从 Activity 中调用它:

MyAsyncTask task = new AsyncTask(myActivity.this);
task.execute();

关于java - 如何在使用SAXParser解析XML中添加AsyncTask?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11989539/

相关文章:

Java:strings.xml 中的换行符不起作用

C++ 视角下的 Java 对象和指针

java - Android 警报确认对话框

java - jackson 在python中的替代品是什么?

java - 填充相交的两条线之间的区域?

java - 如何使用贝塞尔曲线绘制通过一组点的平滑线?

java - 在 Android/Java 中使用 itext 在单元格中添加删除线

java - 如何在 Cloud Firestore 中正确构建本地化内容?

java - log4j 性能

Java测试文件LocalDate