Reputation: 15
I'm doing some revision as I am a noob programmer- currently trying to understand the AsyncTask. I have this example of how it is used to download and display the contents of a webpage but I'm struggling to get my head around which bits do what. My notes are unfortunately rubbish. Can anyone help out explaining it?
Upvotes: 0
Views: 109
Reputation: 246
First, an aside: I finally "got" AsyncTask a few days ago, so I am really excited to help someone else get it now too. Anyway, I went through a bunch of tutorials without really understanding how it worked (the official docs were not helpful in this regard). This is the one that finally let it click: http://www.brighthub.com/mobile/google-android/articles/82805.aspx
Upvotes: 0
Reputation: 523
When you start an AsyncTask, it executes the doInBackground() method on a different thread, so that it doesn't lock up your UI or any other threads. When the doInBackground() method is complete, the return value of that method is passed to onPostExecute(). The onPostExecute method is executed on the UI thread, so this is where you should make any changes to the views, etc.
Upvotes: 0
Reputation: 17140
private class DownloadWebpageTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
String response = "";
for (String url : urls) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
BufferedReader buffer = new BufferedReader(
new InputStreamReader(content));
String s = "";
while ((s = buffer.readLine()) != null) {
response += s;
}
} catch (Exception e) {
e.printStackTrace();
}
}
// since this is the background thread, we need to return the string reponse so that onPostExecute can update the textview.
return response
}
@Override
protected void onPostExecute(String result) {
// only onPostExecute,onProgressUpdate(Progress...), and onPreExecute can touch modify UI items since this is the UI thread.
textView.setText(result);
}
Upvotes: 1
Reputation: 723
You can't touch in a View inside the doInBackground, like textView.setText(response); its wrong, you need touch the view in the onPreExecute and in the onPostExecute, and in the doInBackground don't lock the UI Thread, besides, the onPre and onPost lock the UI Thread.
AsyncTask look in this link, the doc of the AsyncTask.
Upvotes: 0
Reputation: 328
So I do not really know which parts are the most unclear to you but let me explain the basics:
You create a private Async-Task in your code. Methods of this class can run in the background while your app continues to run normally (otherwise, it would freeze). Everything that is declared in the doInBackground()-method is executed in the background.
To start the execution, you call the execute()-method which is of course outside your private Async-Task. Before calling the execute()-method, you instantiate the Async-Task.
The method for onPostExecute() can be used to for example handle results or return values. This method is called when the doInBackground() is finished.
Upvotes: 0