0

我正在尝试在活动类之外实现 AsyncTask。现在的问题是这个 asynctask 类的状态总是显示为正在运行,我该如何让它完成?

这是我的活动课

protected void onCreate(Bundle savedInstanceState) {

    employeeAsyncTask.execute(new String[] {url});

    System.out.println("Status "+employeeAsyncTask.getStatus());
    //employeeAsyncTask.cancel(true);

    while(employeeAsyncTask.getStatus() == AsyncTask.Status.RUNNING){

    }

    while (employeeAsyncTask.getStatus()== AsyncTask.Status.FINISHED)   {
        System.out.println("hereeeeeeeeeeeeee");
        ArrayList<Employee> list = employeeAsyncTask.getEmpList();
        System.out.println("Size of list "+list);
    }

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_capture);


}

这是我的异步任务类

public class FetchEmployeeAsyncTask extends AsyncTask<String, Void, ArrayList>{

private ArrayList<Employee> empList = null;
private boolean done = false;

/**
 * @return the done
 */
public boolean isDone() {
    return done;
}

/**
 * @param done the done to set
 */
public void setDone(boolean done) {
    this.done = done;
}

@Override

protected ArrayList doInBackground(String... url) {
    // TODO Auto-generated method stub
    ArrayList<Employee> employees = null;
    for(String employeeUrl : url){

        employees = fetch(employeeUrl);
    }
    return employees;
}

private ArrayList<Employee> fetch(String url) {
    // TODO Auto-generated method stub
    ArrayList<Employee> employees = null;
    String response = null;
    try {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);

        HttpResponse httpResponse = httpClient.execute(httpGet);
        HttpEntity httpEntity = httpResponse.getEntity();
        response = EntityUtils.toString(httpEntity);
        employees = EmployeeXMLParser.XMLfromString(response);
        System.out.println("Size in fetch "+employees.size());

        //System.out.println("Employee Name :: " + employees.get(0).getFirstName() + " " + employees.get(0).getLastName());
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } /*catch (XmlPullParserException e) {
        // TODO Auto-generated catch block
        System.out.println("Error parsing the response :: " + response);
        e.printStackTrace();
    }*/
    return employees;
}

@Override
protected void onPreExecute() {
    // TODO Auto-generated method stub
    super.onPreExecute();
    done = false;
}

@Override
public void onPostExecute(ArrayList employeeList){

    setEmpList(employeeList);
    System.out.println("in post execxute "+employeeList.size());
    done = true;
}

public ArrayList<Employee> getEmpList() {
    return empList;
}

public void setEmpList(ArrayList<Employee> empList) {
    this.empList = empList;
}

}

我无法弄清楚 onPostExecute() 中应该包含什么。

4

1 回答 1

0

添加一个将活动作为参数并将其存储在字段中的构造函数:

private MyActivity activity;

public FetchEmployeeAsyncTask(MyActivity activity) {
    this.activity = activity;
}

然后在 onPostExecute 方法中,您可以传回员工列表:

@Override
public void onPostExecute(ArrayList employeeList) {
    activty.onEmpListLoaded(employeeList);
}

并在您的活动中执行onEmpListLoaded任何需要做的事情。

于 2013-01-29T11:03:40.270 回答