AsyncTask - what parameters are needed for extending and doInBackground?

强颜欢笑 提交于 2019-12-31 01:49:09

问题


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() {
}

}


回答1:


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) 
    {

    }
  }



回答2:


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) {
    // ...
  }
}


来源:https://stackoverflow.com/questions/12942361/asynctask-what-parameters-are-needed-for-extending-and-doinbackground

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!