I am trying to parse the following page with AsyncTask, urlConnection and InputStreamReader
public class DownloadTask extends AsyncTask<String, Void, String> {
URL url;
URLConnection urlConnection;
String result = null;
@Override
protected String doInBackground(String... urls) {
try {
url = new URL(urls[0]);
urlConnection = (URLConnection) url.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
int data = reader.read();
while (data != -1) {
char current = (char) data;
result += current;
data = reader.read();
}
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
And I am using this on this way:
DownloadTask downloadTask = new DownloadTask();
String data = null;
try {
data = downloadTask.execute("http://www.imdb.com/movies-in-theaters/").get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
The problem is that it takes more than 3 mins to finish the DownloadTask. Finally after this time it works on the emulator but not in a real device.
I know that this is not a good way (parsing a web page) to do stuff like that, but I am doing it for educational reasons.
Any advice how I can speed up the procedure?
Thanks!