Help on extending AsyncTask

  • Replies:2
  • Answered
Ole Aass
  • Forum posts: 2

Oct 31, 2012, 8:30:46 PM via Website

I'm trying to follow the code on android.com, but when I'm trying to compile and run the application I get the following errors

1-compile:
2 [javac] Compiling 2 source files to D:\Development\android\HttpExample\bin\classes
3 [javac] D:\Development\android\HttpExample\src\com\schas\httpexample\HttpExampleActivity.java:38: error: no suitable method found for execute(String)
4 [javac] new DownloadWebpageText().execute(stringUrl);
5 [javac] ^
6 [javac] method AsyncTask.execute(Runnable) is not applicable
7 [javac] (actual argument String cannot be converted to Runnable by method invocation conversion)
8 [javac] method AsyncTask.execute(URL...) is not applicable
9 [javac] (argument type String does not conform to vararg element type URL)
10 [javac] D:\Development\android\HttpExample\src\com\schas\httpexample\HttpExampleActivity.java:49: error: HttpExampleActivity.DownloadWebpageText is not abstract and does not override abstract method doInBackground(URL...) in AsyncTask
11 [javac] private class DownloadWebpageText extends AsyncTask<URL, Integer, Long> {
12 [javac] ^
13 [javac] D:\Development\android\HttpExample\src\com\schas\httpexample\HttpExampleActivity.java:50: error: method does not override or implement a method from a supertype
14 [javac] @Override
15 [javac] ^
16 [javac] D:\Development\android\HttpExample\src\com\schas\httpexample\HttpExampleActivity.java:62: error: method does not override or implement a method from a supertype
17 [javac] @Override
18 [javac] ^
19 [javac] 4 errors
20BUILD FAILED
21C:\Users\Ole\AppData\Local\Android\android-sdk\tools\ant\build.xml:679: The following error occurred while executing this line:
22C:\Users\Ole\AppData\Local\Android\android-sdk\tools\ant\build.xml:692: Compile failed; see the compiler error output for details.

The MainActivity code looks like this

1package com.schas.httpexample;
2
3import java.io.*;
4import java.net.*;
5
6import android.app.Activity;
7import android.os.Bundle;
8import android.os.AsyncTask;
9import android.view.View;
10import android.widget.EditText;
11import android.widget.TextView;
12import android.net.*;
13import android.util.*;
14import android.content.Context;
15
16public class HttpExampleActivity extends Activity {
17 private static final String DEBUG_TAG = "HttpExample";
18 private EditText urlText;
19 private TextView textView;
20
21 @Override
22 public void onCreate(Bundle savedInstanceState) {
23 super.onCreate(savedInstanceState);
24 setContentView(R.layout.main);
25 urlText = (EditText) findViewById(R.id.myUrl);
26 textView = (TextView) findViewById(R.id.myText);
27 }
28
29 // When user clicks button, calls AsyncTask.
30 // Before attempting to fetch the URL, makes sure that there is a network connection.
31 public void myClickHandler(View view) {
32 // Gets the URL from the UI's text field.
33 String stringUrl = urlText.getText().toString();
34 ConnectivityManager connMgr = (ConnectivityManager)
35 getSystemService(Context.CONNECTIVITY_SERVICE);
36 NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
37 if (networkInfo != null && networkInfo.isConnected()) {
38 new DownloadWebpageText().execute(stringUrl);
39 } else {
40 textView.setText("No network connection available.");
41 }
42 }
43
44 // Uses AsyncTask to create a task away from the main UI thread. This task takes a
45 // URL string and uses it to create an HttpUrlConnection. Once the connection
46 // has been established, the AsyncTask downloads the contents of the webpage as
47 // an InputStream. Finally, the InputStream is converted into a string, which is
48 // displayed in the UI by the AsyncTask's onPostExecute method.
49 private class DownloadWebpageText extends AsyncTask {
50 @Override
51 protected String doInBackground(String... urls) {
52
53 // params comes from the execute() call: params[0] is the url.
54 try {
55 return downloadUrl(urls[0]);
56 } catch (IOException e) {
57 return "Unable to retrieve web page. URL may be invalid.";
58 }
59 }
60
61 // onPostExecute displays the results of the AsyncTask.
62 @Override
63 protected void onPostExecute(String result) {
64 textView.setText(result);
65 }
66
67 // Given a URL, establishes an HttpUrlConnection and retrieves
68 // the web page content as a InputStream, which it returns as
69 // a string.
70 private String downloadUrl(String myurl) throws IOException {
71 InputStream is = null;
72 // Only display the first 500 characters of the retrieved
73 // web page content.
74 int len = 500;
75
76 try {
77 URL url = new URL(myurl);
78 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
79 conn.setReadTimeout(10000 /* milliseconds */);
80 conn.setConnectTimeout(15000 /* milliseconds */);
81 conn.setRequestMethod("GET");
82 conn.setDoInput(true);
83 // Starts the query
84 conn.connect();
85 int response = conn.getResponseCode();
86 Log.d(DEBUG_TAG, "The response is: " + response);
87 is = conn.getInputStream();
88
89 // Convert the InputStream into a string
90 String contentAsString = readIt(is, len);
91 return contentAsString;
92
93 // Makes sure that the InputStream is closed after the app is
94 // finished using it.
95 } finally {
96 if (is != null) {
97 is.close();
98 }
99 }
100 }
101
102 // Reads an InputStream and converts it to a String.
103 public String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException {
104 Reader reader = null;
105 reader = new InputStreamReader(stream, "UTF-8");
106 char[] buffer = new char[len];
107 reader.read(buffer);
108 return new String(buffer);
109 }
110 }
111}

I've been struggling with this for quite some time now, but without luck :(
Could anyone please guide me in the right direction?

Reply
Ole Aass
  • Forum posts: 2

Oct 31, 2012, 8:39:39 PM via Website

And of course I managed to solve this just after I had created this thread. The solution was this

1private class DownloadWebpageText extends AsyncTask<String, Void, String> {

Of course a few new errors appeared, but those were not related to this topic.

Reply
Ashish Tripathi
  • Forum posts: 211

Feb 4, 2013, 12:56:43 PM via Website

Asynctask parameter is missing.(String Void String)

still probs let me know

Reply