Matteo Cardellini
Matteo Cardellini

Reputation: 896

ProgressBar when AsyncTask.get() is called

I wrote this little application that take some datas from a web service and put them in a custom ListView:

SOAP CALL

public String getVoy(String voy) {
    String SOAP_ACTION = "XXXXXXXXXX";
    String METHOD_NAME = "XXXXXXXXXX";
    String NAMESPACE = "urn:DefaultNamespace";
    String URL = "http://xxxxxxxxxxxx/xxxxx.nsf/xxxxxxx?wsdl";

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    request.addProperty("VOY", voy);
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.setOutputSoapObject(request);
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    try {
        androidHttpTransport.call(SOAP_ACTION, envelope);
        final SoapObject result = (SoapObject) envelope.bodyIn;
        rt = (result != null) ? result.getProperty(0).toString() : "Nessuna risposta 1";

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (XmlPullParserException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } // Invio il webservice

    return rt;
}

AsyncTask

 private class LineCall extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        final String text = WebService.getInstance().getVoy(params[0]);

        return text;
    }
}

Call the AsyncTask and get the datas

LineCall line = new LineCall();
    line.execute(voy);

    try {
        **string** = line.get();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Then I take the string variable, split my datas and put them in the custom list...

It works very well but the problem is that is very slow so I tried to create a ProgressDialog but there are some problems...

If I put it in the previous activity:

 Button partenze = (Button) findViewById(R.id.partenze);
    partenze.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            StartDeapartures call = new StartDeapartures();
            call.execute();

        }
    });
}
private class StartDeapartures extends AsyncTask<Void, Void, Void> {

    private ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        dialog = new ProgressDialog(Home.this);
        dialog.setTitle("Loading..");
        dialog.setCancelable(false);
        dialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        Intent intent = new Intent(getApplicationContext(), Partenze.class);
        startActivity(intent);
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        dialog.dismiss();
    }

}

The progress image don't move and it seems freezed and if i put it in the LineCall asyncTask the ProgressDialog is not showed...

I found in google that this is dued to string = line.get(); that takes a long time to run...

So how can i put a ProgressDialog when get() is working ?

Upvotes: 1

Views: 496

Answers (1)

Timu&#231;in
Timu&#231;in

Reputation: 4673

If you call AsyncTask.get(), it will block the main thread until the doInBackground completes. So, you should not call AsyncTask.get(), instead you should handle the result and do UI operations in AsyncTask.onPostExecute() method. This is the designed behaviour of AsyncTask.

In your StartDeapartures asynctask, why do you start an activity in doInBackground? What is the point of using an AsyncTask for starting an activity?

Upvotes: 1

Related Questions