How to use AsyncTask with xml Sax Parser?

I used the xml parser to read xml from the web and it works in android 2.3. But now I want it to work on Android 3.0. Since I am reading a file in the main thread, I get an error message:

android.os.NetworkOnMainThreadException

I tried to use AsyncTask, but I could not get it to work. Please tell me how to implement AsyncTask in my case. Here is my original code:

ListView lw = (ListView)findViewById(R.id.listView1); final ArrayList<String> list = new ArrayList<String>(); final ArrayAdapter<String> aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list); lw.setAdapter(aa); try { /* Create a URL we want to load some xml-data from. */ URL url = new URL("http://www.bnr.ro/nbrfxrates.xml"); //aded to verify link URI uri = new URI(url.toString()); HttpGet httpget = new HttpGet(uri); DefaultHttpClient httpclient = new DefaultHttpClient(); HttpResponse response = httpclient.execute(httpget); if (response.getStatusLine().getStatusCode()==200) { url = new URL("http://www.visual-efx.net/nbrfxrates.xml"); } //aded to verify link /* Get a SAXParser from the SAXPArserFactory. */ SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); /* Get the XMLReader of the SAXParser we created. */ XMLReader xr = sp.getXMLReader(); /* Create a new ContentHandler and apply it to the XML-Reader*/ ExampleHandler myExampleHandler = new ExampleHandler(); xr.setContentHandler(myExampleHandler); /* Parse the xml-data from our URL. */ xr.parse(new InputSource(url.openStream())); /* Parsing has finished. */ for (int i=1; i<=31; i++){ list.add(myExampleHandler.valuta[i] + " = " + myExampleHandler.pret[i] + " lei"); aa.notifyDataSetChanged(); } } catch (Exception e) { /* Display any Error to the GUI. */ Log.e(MY_DEBUG_TAG, "Error", e); } 
+2
android
source share
1 answer

In this case, you should use AsyncTaskLoader . This will simplify the situation. Just create your own class that extends AsyncTaskLoader and just has one method, loadInBackground() . In your loadInBackground() method, enter the code you use to parse the xml as follows:

 public List<String> loadInBackground(){ ArrayList toReturn = new ArrayList<String>(); try { URL url = new URL("http://www.bnr.ro/nbrfxrates.xml"); URI uri = new URI(url.toString()); HttpGet httpget = new HttpGet(uri); DefaultHttpClient httpclient = new DefaultHttpClient(); HttpResponse response = httpclient.execute(httpget); if (response.getStatusLine().getStatusCode()==200) { url = new URL("http://www.visual-efx.net/nbrfxrates.xml") } SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); ExampleHandler myExampleHandler = new ExampleHandler(); xr.setContentHandler(myExampleHandler); xr.parse(new InputSource(url.openStream())); for (int i=1; i<=31; i++){ toReturn.add(myExampleHandler.valuta[i] + " = " + myExampleHandler.pret[i] + " lei"); } } catch (Exception e) { Log.e(MY_DEBUG_TAG, "Error", e); } return toReturn; } 

Then, in the action in which you display the list, do your activity in the LoaderManager.LoaderCallbacks interface, for example

 public YourActivity implements LoaderManager.LoaderCallbacks<List<String>> 

Then follow the callback methods. For example, your onLoadFinished() method would look something like this:

 public void onLoadFinished(Loader<List<String>>, List<String> data){ aa.setData(data); } 

For more information about bootloaders, check this documentation . Please note: if you focus on API less than 3.0, you need to use the Android Support Package .

+2
source share

All Articles