将两个参数传递给 asynctask 方法。怎样?

发布于 2025-01-05 21:06:02 字数 2128 浏览 1 评论 0原文

我正在设计一个经常与 Web 服务器通信以进行更新的应用程序。仅当用户请求时才会发生通信。我发现 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);
}
    }

I'm designing an application that frequently communicates to a web server for updates. Communication happens only when user requests. I found AsyncTask could be helpful here. So I modified one class to serve my application as AsyncTask.

I want to pass an url and http post parameters to anysc class's doInBackground process.
I can't figure out how to do it.

Here's it-

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

I've one interface that is used to process response sent from onPostExecute(). It is as fallows.

   package com.project.main.external;

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

And here is my main activity that calls for http response --

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心房的律动 2025-01-12 21:06:02

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

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

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

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

等等

请注意,所有参数必须具有相同的类型,在您的例子中为字符串。

AsyncTask does already allow more parameters:
In your case call it like this:

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

In doInBackground you can get them with:

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

And so on

Just notice that all parameters must be of the same Type, in your case String.

笙痞 2025-01-12 21:06:02
  1. 您可以将一项或多项内容添加到已有的构造函数中,并填充 AsyncTask 的某些成员,然后使用这些成员在 doInBackground 中进行 POST
  2. 您可以更改它以扩展 AsyncTaskHttpPost, Void, String>AsyncTask 其中 SOME_CLASS 是字符串的 ArrayList, HashMap,或者您可以创建的其他一些类,其中包含构造 HttpPost 所需的所有内容
  1. You could add one or more things to the constructor that you already have and populate some members of the AsyncTask and then use those members to make your POST within doInBackground
  2. You could change it to extend AsyncTask<HttpPost, Void, String> or AsyncTask<SOME_CLASS, Void, String> where SOME_CLASS is an ArrayList of Strings, a HashMap, or some other class that you could create that would contain everything you need to construct the HttpPost
玩物 2025-01-12 21:06:02
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);
public class GetXMLFromServer extends
AsyncTask< String, Void, String> {
private Context context;

GetXMLCallback gc = null;

ProgressDialog progressDialog;

public Type variable1;
public Type variable2;

and set up the variables:

GetXMLFromServer task = new GetXMLFromServer(UpdateList.this, gc);
task.variable1 = XXXXX;
task.variable2 = XXXXX;
task.execute(WebConstants.GET_UPDATES);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文