问题描述
在我的项目中.对于几乎每个 Activity,我都必须创建一个 AsyncTask .. 而且几乎所有的工作都是一样的.我想创建一个通用的 AsyncTask 并扩展它.而且我还想在构造函数中发送方法 like 这个.然后每当我一个类扩展它.只是它必须创建一个方法,它会很容易地完成我们的工作.和更少的编码..这很耗时..提前感谢您的帮助..
In my project. For almost every Activity I have to make an AsyncTask .. And Almost the work is same for all of them. I want to create a generic AsyncTask and extends it. And i also want to send the methods in the constructor like this. Then Whenever I a class extends it . Just it have to create a method and it will do our work easily . and less coding .. It is time consuming .. Thanks for help in Advance..
我的异步任务之一是
private class GetData extends AsyncTask<String, Void, JSONObject> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(Calendar.this,
"", "");
}
@Override
protected JSONObject doInBackground(String... params) {
String response;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse responce = httpclient.execute(httppost);
HttpEntity httpEntity = responce.getEntity();
response = EntityUtils.toString(httpEntity);
Log.d("response is", response);
return new JSONObject(response);
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(JSONObject result)
{
super.onPostExecute(result);
progressDialog.dismiss();
if(result != null)
{
try
{
JSONObject jobj = result.getJSONObject("result");
String status = jobj.getString("status");
if(status.equals("true"))
{
JSONArray array = jobj.getJSONArray("data");
for(int x = 0; x < array.length(); x++)
{
HashMap<String, String> map = new HashMap<String, String>();
map.put("name", array.getJSONObject(x).getString("name"));
map.put("date", array.getJSONObject(x).getString("date"));
map.put("description", array.getJSONObject(x).getString("description"));
list.add(map);
}
CalendarAdapter adapter = new CalendarAdapter(Calendar.this, list);
list_of_calendar.setAdapter(adapter);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
else
{
Toast.makeText(Calendar.this, "Network Problem", Toast.LENGTH_LONG).show();
}
}
}
我只想更改每个 AsyncTask 执行后的代码.如果我创建一个通用类并将 特定方法
传递给它.这会很有帮助..
I just want to change the code in the post execute for each AsyncTask.. If I create a generic class and pass the Particular Method
to it. It will be very helpful..
推荐答案
让你所有的活动implements
一个接口说Ixxx,它有一个方法,activityOnPostExecute(String json)
,并在 asyncTask
构造函数中以 Ixxx
、GeneucTask(Ixxx caller)
和 GeneucTask(Ixxx caller)
类型传递调用者活动在 GeneucTask
的 postExecute()
调用 caller.activityOnPostExecute(result);
现在在每个活动覆盖方法 activityOnPostExecute()代码>.
let all your activities implements
an interface say Ixxx, that has a method, activityOnPostExecute(String json)
, and pass the caller activity at the asyncTask
constructor as type Ixxx
, GenerucTask(Ixxx caller)
, and at postExecute()
of the GenerucTask
invoke caller.activityOnPostExecute(result);
now at every activity override the method activityOnPostExecute()
.
这就是我的意思
1- 使用 onPoseExec()
方法创建所有活动都应该实现的 interface
:
1- Create the interface
that all activities should implement, with the onPoseExec()
method:
public interface IAsyncHandler {
public void onPostExec(String json);
}
2- 创建 GenericAsyncTask 类,
2- Create the GenericAsyncTask class,
public class GenericAsyncTask extends AsyncTask<String, Integer, String> {
IAsyncHandler handler = null;
public GenericAsyncTask(IAsyncHandler h){
handler = h;
}
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... params) {
//whatever work goes here ...
//return str as a result
return str;
}
@Override
protected void onPostExecute(String result) {
//call the caller's class onPostExec(), it should be ovedrridden in the activity
handler.onPostExec(result);
}
}//CLASS
3- 在您的活动中,覆盖方法 onPostExec()
3- At your activity, override the method onPostExec()
@Override
public void onPostExec(String json) {
// TODO Auto-generated method stub
}
4- 在您的活动 onClick()
或某处执行 GenericAsyncTask
4- At your activity onClick()
, or somewhere execute the GenericAsyncTask
onClick(View v){
GenericAsyncTask task = new GenericAsyncTask(this);
task.execute("some", "params","...");
}
这篇关于想要创建一个通用的 AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!