要创建一个通用的AsyncTask [英] Want to create a Generic AsyncTask
问题描述
在我的项目。几乎每一个活动我必须做出一个AsyncTask的..而且几乎工作是同样为所有的人。我想创建一个通用的AsyncTask并将其扩展。我也想发送的方法构造<一个href=\"http://stackoverflow.com/questions/4685563/how-to-pass-a-function-as-a-parameter-in-java\">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..
我的一个AsyncTask的是
One of my asyncTask is
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();
}
}
}
我只是想改变在岗code逐条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..
推荐答案
让所有的活动工具
接口说的 Ixxx ,具有方法, activityOnPostExecute(字符串JSON)
,并通过在的AsyncTask
的构造类型 Ixxx
, GenerucTask(Ixxx调用者)
,并在 postExecute()
的 GenerucTask
调用 caller.activityOnPostExecute(结果);
现在每一个活动覆盖的方法 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()
.
编辑:
这里是啥子我的意思是
here is waht i mean
1 - 创建接口
所有活动应该实现,用 onPoseExec()
方法:
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
onClick(View v){
GenericAsyncTask task = new GenericAsyncTask(this);
task.execute("some", "params","...");
}
这篇关于要创建一个通用的AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!