想要创建Generic AsyncTask

时间:2014-05-15 14:02:10

标签: inheritance android-asynctask overloading

在我的项目中。几乎每个Activity我都要制作一个AsyncTask ..而且几乎所有的工作都是一样的。我想创建一个通用的AsyncTask并扩展它。我还想在构造函数like中发送方法。每当我上课时,我都会这样做。只需要创建一个方法,它就能轻松完成我们的工作。而且编码较少..这很耗时..感谢您提前帮助..

我的一个asyncTask是

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的post执行中的代码。如果我创建一个泛型类并将Particular Method传递给它。这将非常有帮助..

1 个答案:

答案 0 :(得分:0)

让您的所有活动implements界面说明 Ixxx ,它有一个方法activityOnPostExecute(String json),并将asyncTask构造函数中的调用者活动作为类型传递每个活动现在IxxxGenerucTask(Ixxx caller)postExecute()调用GenerucTask的{​​{1}}覆盖方法caller.activityOnPostExecute(result);

修改 这是我的意思

1-使用activityOnPostExecute()方法创建所有活动应实施的interface

onPoseExec()

2-创建GenericAsyncTask类,

public interface IAsyncHandler {
    public void onPostExec(String json);
}

3-在您的活动中,覆盖onPostExec()

方法
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

4-在您的活动@Override public void onPostExec(String json) { // TODO Auto-generated method stub } 或某处执行onClick()

GenericAsyncTask