3

What is wrong with this code that uses AsyncTask? In particular: - what parameters do I need to put in fetchSchools - what parameters do I need to put in doInBackground?

I've found lots of "helpful" examples but they all use pseudocode in these parameters and don't explain what I actually need to put there.

"I get the Eclipse error the method fetchSchools must implement the inherited abstract method AsynchTask..."

I don't need to pass this anything, I expect it to return a string.

public class fetchSchools extends AsyncTask<Void, Void, String> {

public String doInBackground(String retval) {
       StringBuilder builder = new StringBuilder();
        HttpClient client = new DefaultHttpClient();

        HttpGet httpGet = new HttpGet("http://www.domain/schools.php");
        try 
     {
          HttpResponse response = client.execute(httpGet);
          StatusLine statusLine = response.getStatusLine();
          int statusCode = statusLine.getStatusCode();
          if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String
     line;

            int a=0;
            while ((line = reader.readLine()) != null) {
              builder.append(line);
            Log.i(MainActivity.class.getName(), "Reading in: " + a +" : "+ line);
            a++;
            }
          } else {
            Log.e(MainActivity.class.toString(), "Failed to download file");
          }
        } catch (ClientProtocolException e) {
          e.printStackTrace();
        } catch (IOException e)
     {
          e.printStackTrace();
        }

        return builder.toString(); 
}

protected void onPostExecute() {
}

}

rihallix
  • 356
  • 1
  • 8
  • 22

2 Answers2

11

you gave doInBackground a string parameter so async task first parameter must be string not void.

AsyncTask<String , Void, String> {

If you don't want to pass a parameter, don't give parameter to doInBackground function.

check this page for asynctask reference: http://developer.android.com/reference/android/os/AsyncTask.html

First parameter of asynctask goes to doInBackground function, second one goes to onprogressUpdate function, third parameter foes to onpostexecute function.

I think you want to do this:

 public class fetchSchools extends AsyncTask<Void, Void, String> {
    @Override
    protected String doInBackground(Void... arg0) {
      StringBuilder builder = new StringBuilder();
      HttpClient client = new DefaultHttpClient();
      // ...

     return builder.toString();
    }
    protected void onPostExecute(String retval) 
    {

    }
  }
Nesim Razon
  • 9,684
  • 3
  • 36
  • 48
  • Quoting the OP: "I don't need to pass this anything, I expect it to return a string." – Vincent Mimoun-Prat Oct 17 '12 at 20:08
  • So I changed it to AsyncTask. I still get the error: "The type fetchSchools must implement the inherited abstract method AsyncTask.doInBackground(String...)" and also.. – rihallix Oct 17 '12 at 20:10
  • you need to change onpostexecute also, because you are returning a parameter. `protected void onPostExecute(String retval)`. Checking an example helps I think. – Nesim Razon Oct 17 '12 at 20:12
  • Error persists, I made them all Void, and removed parameters from doInBackground and onPostExecute... `public class fetchSchools extends AsyncTask { public String doInBackground() { protected void onPostExecute() {` – rihallix Oct 17 '12 at 20:22
  • did you check last edit? why did you make void third parameter. you told you want to return a string? – Nesim Razon Oct 17 '12 at 20:24
  • OK, good point. Now set AsyncTask to but still have the same error. – rihallix Oct 17 '12 at 20:26
  • you are not reading my last example based on your code. Please copy my example based on your code. I tested on eclipse now It works. `public class fetchSchools extends AsyncTask {` – Nesim Razon Oct 17 '12 at 20:28
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/18191/discussion-between-rihallix-and-nesim-razon) – rihallix Oct 17 '12 at 20:30
1

I don't need to pass this anything, I expect it to return a string.

Then

public class fetchSchools extends AsyncTask<Void, Void, String> {
  @Override
  protected String doInBackground(Void... params) {
    // ...
  }
}
Vincent Mimoun-Prat
  • 28,208
  • 16
  • 81
  • 124