【发布时间】: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);
}
}
【问题讨论】: