【问题标题】:Passing two parametes to asynctask method. how to?将两个参数传递给 asynctask 方法。如何?
【发布时间】:2012-02-16 23:29:35
【问题描述】:

我正在设计一个经常与网络服务器通信以进行更新的应用程序。只有在用户请求时才会发生通信。我发现 AsyncTask 在这里可能会有所帮助。所以我修改了一个类,将我的应用程序作为 AsyncTask 服务。

我想将 url 和 http post 参数传递给 anysc 类的 doInBackground 进程。 我不知道该怎么做。

这里是-

public class GetXMLFromServer extends
    AsyncTask< String, Void, String> {
private Context context;

GetXMLCallback gc = null;

ProgressDialog progressDialog;

public GetXMLFromServer(Context context, GetXMLCallback gc) {
    this.context = context;
    this.gc = gc;
    progressDialog = new ProgressDialog(this.context);
}

protected void onPreExecute() {
    progressDialog.setMessage("Fetching...");
    progressDialog.show();
}

@Override
protected void onPostExecute(String result) {
    gc.onSuccess(result);
    progressDialog.dismiss();
}


 @Override
protected String doInBackground(String... params) {
     String response = "";
    response=CustomHttpClient.executeHttpPost(params[0]);
    return null;
} 
    //Confused how to pass URL and http post parameters to doInBackground().
  }

我有一个接口用于处理从 onPostExecute() 发送的响应。就像休耕一样。

   package com.project.main.external;

   public interface GetXMLCallback {
       void onSuccess(String downloadedString);
       void onFailure(Exception exception);
   }

这是我需要 http 响应的主要活动 --

 public class UpdateList extends Activity implements GetXMLCallback { 
 //above line also throws error that interface methods are not implemented yet
 //they are (few lines below) defined.

private TextView textView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_layout);
    textView = (TextView) findViewById(R.id.TextView01);
}

GetXMLCallback gc = new GetXMLCallback() {

    public void onFailure(Exception exception) {

    }

    public void onSuccess(String downloadedString) {
        textView.setText(downloadedString);
    }

};

public void getUpdates(View view) {
    GetXMLFromServer task = new GetXMLFromServer(UpdateList.this, gc);
    task.execute(WebConstants.GET_UPDATES);
}
    }

【问题讨论】:

标签: android android-asynctask


【解决方案1】:

AsyncTask 已经允许更多参数:
在你的情况下这样称呼它:

task.execute("parameter_one","parameter_two","parameter_three");

在 doInBackground 中,您可以通过以下方式获取它们:

params[0]
params[1]
params[2]

等等

请注意,所有参数必须是相同的类型,在您的情况下为 String。

【讨论】:

    【解决方案2】:
    1. 您可以在已有的构造函数中添加一项或多项内容并填充 AsyncTask 的一些成员,然后使用这些成员在 doInBackground 中进行 POST
    2. 您可以将其更改为扩展 AsyncTask&lt;HttpPost, Void, String&gt;AsyncTask&lt;SOME_CLASS, Void, String&gt;,其中 SOME_CLASS 是字符串的 ArrayList、HashMap 或您可以创建的包含构建 HttpPost 所需的所有内容的其他类

    【讨论】:

      【解决方案3】:
      public class GetXMLFromServer extends
      AsyncTask< String, Void, String> {
      private Context context;
      
      GetXMLCallback gc = null;
      
      ProgressDialog progressDialog;
      
      public Type variable1;
      public Type variable2;
      

      并设置变量:

      GetXMLFromServer task = new GetXMLFromServer(UpdateList.this, gc);
      task.variable1 = XXXXX;
      task.variable2 = XXXXX;
      task.execute(WebConstants.GET_UPDATES);
      

      【讨论】:

      • 感谢它的工作。但是我仍然无法解决来自 GerXMLCallback 接口的“未实现方法”的错误。我该如何解决?
      • 我认为这回答了“将两个参数传递给 asynctask 方法。如何?”的问题
      • 这不是一个完全的答案。当asynctask doInBackground 方法接受任意数量的参数时,为什么要这样做。关注doInBackground(String... params)。这意味着您可以传入任意数量的 String 并在名为 params 的数组中访问它 :)
      猜你喜欢
      • 2014-07-29
      • 1970-01-01
      • 1970-01-01
      • 2012-08-17
      • 2020-08-18
      • 2011-03-05
      • 2011-07-18
      • 2019-10-27
      相关资源
      最近更新 更多