当显示进程对话框时,不执行doInBackground()

时间:2013-02-05 07:36:26

标签: android

我正在尝试显示进程对话框,它正在按预期显示,但是当它被显示时,doInBackground()没有被执行,当我按下模拟器的屏幕时,doInBackground()再次开始执行。

这是我的AsyncTask类:

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

private CaptureActivity activity;
//private ProgressDialog progressDialog;
public FetchEmployeeAsyncTask(CaptureActivity nextActivity) {
    this.activity = nextActivity;
}

@Override
protected void onPreExecute() {
    // TODO Auto-generated method stub
    super.onPreExecute();
    /*progressDialog= new ProgressDialog(activity);
    progressDialog.setCancelable(true);
    progressDialog.setTitle("Fetching Employees!!");
    progressDialog.setMessage("Please wait...");
    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    progressDialog.setProgress(0);
    progressDialog.show();*/
}

@Override
protected ArrayList<Employee> doInBackground(String... url) {
    // TODO Auto-generated methoVoidd 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.employeeParser(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
public void onPostExecute(ArrayList<Employee> employees){
    super.onPostExecute(employees);

    System.out.println("in post execxute "+employees.size());
    //progressDialog.dismiss(); 
    activity.showEmployees(employees);


}

}

我在此活动类中调用AsyncTask:

public class CaptureActivity extends Activity {


private String url = "http://192.168.2.223:8680/capture/clientRequest.do?r=employeeList&cid=0";

FetchEmployeeAsyncTask employeeAsyncTask;

private ArrayList<Employee> employees = null;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setTitle("");


     employeeAsyncTask = new FetchEmployeeAsyncTask(this);

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

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


    setContentView(R.layout.activity_capture);

}

2 个答案:

答案 0 :(得分:0)

你想在这做什么?你试图从数据库中获取一些值,如果是这样,如果你正确地传递了值,请检查url的分配。

另外,请尝试详细解释您的问题并粘贴更多代码。

答案 1 :(得分:0)

试试这个:

  protected void onPreExecute() {

   progressDialog = ProgressDialog.show(currentActivity.this, "",
                "Message Here", true);
    }
   protected void onPostExecute(String str) {

   dialog.dismiss();

   }