Reputation: 34360
I am using AsyncTask to download a webpage source(HTML) . It is working properly but it take to long to execute .. is there a better method. I think It is wasting time in UrlConnection .I have tried to do this with HttpClient but can't get Html source. Help me out in making it fast or .. tell me how to put this AsyncTask in thread?
class RetrieveFeedTask extends AsyncTask<String, Void, String>
{
@Override
protected void onPreExecute()
{
super.onPreExecute();
Utilities.hideSoftKeyboard(PlayListActivity.this);
progressDialog = ProgressDialog.show(PlayListActivity.this,
"Loading...", "Please wait...");
}
@Override
protected String doInBackground(String... urls) {
try {
URL url= new URL(urls[0]);
URLConnection conn = url.openConnection();
// open the stream and put it into BufferedReader
BufferedReader br = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String inputLine;
String HTML_response = "";
while ((inputLine = br.readLine()) != null) {
// System.out.println(inputLine);
HTML_response += inputLine;
}
br.close();
System.out.println("Done");
Parser(feed);
return HTML_response;
} /*catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}*/catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(String feed)
{
super.onPostExecute(feed);
PlayListAdapter adapter = new PlayListAdapter(
PlayListActivity.this, list);
list_of_songs.setAdapter(adapter);
progressDialog.dismiss();
}
}
@SuppressLint("NewApi")
void Parser(String x)
{
if (x.contains(spliter_start))
{
if (spliter_end.isEmpty())
{
x = x.substring(x.indexOf(spliter_start));
}
else
{
x = x.substring(x.indexOf(spliter_start),
x.indexOf(spliter_end));
}
}
int i = 0;
list.clear();
while (x.contains(loop_controller))
{
if (i > 50)
{
break;
}
HashMap<String, String> map = new HashMap<String, String>();
x = x.substring(x.indexOf(song_start));
map.put("songsName",x.substring(
x.indexOf(song_start) + song_start.length(),
x.indexOf(song_end)));// songsName.get(i));
x = x.substring(x.indexOf(song_url_start));
map.put("songsUrl",x.substring(
x.indexOf(song_url_start) + song_url_start.length(),
x.indexOf(song_url_end)));// songsUrl.get(i));
list.add(map);
i++;
}
}
Upvotes: 0
Views: 454
Reputation: 12636
This part:
String inputLine;
String HTML_response = "";
while ((inputLine = br.readLine()) != null) {
// System.out.println(inputLine);
HTML_response += inputLine;
}
Is insane - you should never do things like that with Strings as on every single iteration of the loop new String is created. User StringBuilder.append();
instead String HTML_response
Upvotes: 1
Reputation: 1037
First of all, AsyncTasks already run in a separate thread, so you don't have to worry about that here. Second all AsyncTasks must perform their long-time operations in the doInBackground
method since is here where the thread runs, so your problem here is that you're calling your parser in the onPostExecute
method. You could change it to this (assuming that your object list
is a global variable:
@Override
protected String doInBackground(String... urls) {
(...)
System.out.println("Done");
return Parser(HTML_response);
(..)
}
@Override
protected void onPostExecute(Void void)
{
super.onPostExecute(feed);
PlayListAdapter adapter = new PlayListAdapter(
PlayListActivity.this, list);
list_of_songs.setAdapter(adapter);
progressDialog.dismiss();
}
}
About the download code I think it's ok and I don't really know if it could be more efficient.
Hope that helps, greetings!
Upvotes: 0