【问题标题】:AsyncTask - what parameters are needed for extending and doInBackground?AsyncTask - 扩展和 doInBackground 需要哪些参数?
【发布时间】:2012-10-17 19:59:48
【问题描述】:

这段使用 AsyncTask 的代码有什么问题?特别是: - 我需要在 fetchSchools 中放入什么参数 - 我需要在 doInBackground 中放入哪些参数?

我找到了很多“有用”的示例,但它们都在这些参数中使用伪代码,并没有解释我实际需要放在那里的内容。

“我得到 Eclipse 错误,方法 fetchSchools 必须实现继承的抽象方法 AsynchTask...”

我不需要传递任何东西,我希望它返回一个字符串。

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

}

【问题讨论】:

    标签: android android-asynctask


    【解决方案1】:

    你给 doInBackground 一个字符串参数,所以异步任务的第一个参数必须是字符串而不是 void。

    AsyncTask<String , Void, String> {
    

    如果您不想传递参数,请不要将参数传递给 doInBackground 函数。

    查看此页面以获取异步任务参考: http://developer.android.com/reference/android/os/AsyncTask.html

    asynctask的第一个参数传给doInBackground函数,第二个传给onprogressUpdate函数,第三个参数foes传给onpostexecute函数。

    我想你想这样做:

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

    【讨论】:

    • 引用 OP:“我不需要传递任何东西,我希望它返回一个字符串。”
    • 所以我把它改成了 AsyncTask。我仍然收到错误消息:“类型 fetchSchools 必须实现继承的抽象方法 AsyncTask.doInBackground(String...)” 以及..
    • 您还需要更改 onpostexecute,因为您正在返回一个参数。 protected void onPostExecute(String retval)。我认为检查示例会有所帮助。
    • 错误仍然存​​在,我将它们全部设为 Void,并从 doInBackground 和 onPostExecute 中删除了参数...public class fetchSchools extends AsyncTask &lt;Void, Void, Void&gt; { public String doInBackground() { protected void onPostExecute() {
    • 您检查了上次编辑吗?你为什么要使第三个参数无效。你告诉过你要返回一个字符串?
    【解决方案2】:

    我不需要传递任何东西,我希望它返回一个字符串。

    然后

    public class fetchSchools extends AsyncTask<Void, Void, String> {
      @Override
      protected String doInBackground(Void... params) {
        // ...
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-26
      • 2018-10-22
      • 1970-01-01
      • 2014-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多