0

I have an application, it will parse data from Youtube's playlist, the data is formatted like XML. I am using SAXParser to do this. But having a issue when using InputStream with SAXParser.

SAXParser saxParser = saxFactory.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();
xmlReader.setContentHandler(this);
URLConnection urlConnection = new URL(pURL).openConnection();
urlConnection.setConnectTimeout(10000);
InputStream inputStream = urlConnection.getInputStream();/*always die here*/
xmlReader.parse(new InputSource(inputStream));

The class that contains code is extending DefaultHandler. With this code, if I run with class DataASyn extends AsyncTask<Void, Void, Void> it is ok and run well, but when I run without DataAsyn, it is always died as I described above. I do not know why ???? I also debug when on background, and recognize that at InputStream inputStream = urlConnection.getInputStream(); program will pause some seconds before continuing run, maybe it was waiting for data ?!? I hope all information above, anyone will know what has happened and give me any ideas about it.

Thanks, Ryan

ryan_le
  • 69
  • 1
  • 8
  • which version of android are you running this on? and when you say it "dies" do you mean ANR (crashes Application Not Responding)? – Ryhan Jun 14 '14 at 19:17
  • I am using Android 4.0.3 with Samsung Galaxy S2. "Dies" I mean like your post - Crash appears! – ryan_le Jun 15 '14 at 06:58

1 Answers1

0

The fact it works in your async task and "dies" otherwise, might mean your getting the NetworkOnMainThreadException. When the code executes in the AsyncTask it does so on a different thread other than the UI-Thread. When you run this code outside the AsyncTask, presumably your UI-Thread, this exception will get thrown on android 3.0+ if I remember correctly.

Also, getting the input stream from any endpoint will take some time as it is a network operation. Don't expect it to be another instantaneous when stepping through it with breakpoints.

Community
  • 1
  • 1
Ryhan
  • 1,815
  • 1
  • 18
  • 22