将两个参数传递给 asynctask 方法。怎样?
我正在设计一个经常与 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
AsyncTask 已经允许更多参数:
在您的情况下,这样称呼它:
在 doInBackground 中,您可以通过以下方式获取它们:
等等
请注意,所有参数必须具有相同的类型,在您的例子中为字符串。
AsyncTask does already allow more parameters:
In your case call it like this:
In doInBackground you can get them with:
And so on
Just notice that all parameters must be of the same Type, in your case String.
doInBackground
中进行 POSTAsyncTask
HttpPost, Void, String>
或AsyncTask
其中 SOME_CLASS 是字符串的 ArrayList, HashMap,或者您可以创建的其他一些类,其中包含构造 HttpPost 所需的所有内容doInBackground
AsyncTask<HttpPost, Void, String>
orAsyncTask<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并设置变量:
and set up the variables: