gpt4 book ai didi

android - 将两个参数传递给 asynctask 方法。如何?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:41:48 26 4
gpt4 key购买 nike

我正在设计一个经常与网络服务器通信以获取更新的应用程序。通信仅在用户请求时发生。我发现 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().
}

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

   package com.project.main.external;

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

这是我要求 http 响应的主要 Activity --

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

最佳答案

AsyncTask 确实已经允许更多参数:
在你的情况下这样调用它:

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

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

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

等等

请注意,所有参数必须属于同一类型,在您的情况下为字符串。

关于android - 将两个参数传递给 asynctask 方法。如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9313913/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com