JavaForAndroid
JavaForAndroid

Reputation: 1179

Async Task is not running doInBackground in 2.3.3

i'm developing an android app. Now i would like to use an AsyncTask. It runs perfectly in android 4.1 but the doInBackground Method is not running in android 2.3.3. There are no errors in the logcat..

I have a class that extends vom AsyncTask. So i start it with new Class().execute(). The constructor always works.

Do you have any idea how i can solve the problem in android 2.3.3?

Edit: My Code:

private class TopicList extends AsyncTask<String, Integer, String> {
    private TopicList(){
        Log.e("Constructor","Works");
    }
    @Override
    protected String doInBackground(String... arg0) {
        Log.e("InBackground","Works");
        new Thread(new Runnable() {
            public void run() {
       // Network processes
                }
        }).start();
        return null;
    }
}

I have target version 17 and minSdkVersion 7. Do you still need more information? I execute it by using this code:

new TopicList().execute();

The error log in logcat only shows that the constructor works.

Upvotes: 2

Views: 1643

Answers (1)

Pedro Trindade
Pedro Trindade

Reputation: 66

AsyncTask API: http://developer.android.com/reference/android/os/AsyncTask.html

After reading the documentation above, it's clear that AsyncTask should work fine in version 2.3.3 (it was added in API 3 = Android 1.5).

Are you sure the correct params are being passed? Here is a simple example of an Async Task which calls a WebService and shows the result in a TextView of the activity, I hope this helps you get in the right track:

private class AsyncTaskWebServiceCaller extends AsyncTask<Void, Void, String> {
    private ProgressDialog dialog = new ProgressDialog(MainActivity.this);

    @Override
    protected void onPreExecute() {
        dialog.setMessage("Connecting to Web Service...");
        dialog.show();
    }

    @Override
    protected String doInBackground(Void...voids) {
        //create the envelope for the message
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = false;

        //get the card (SOAPObject) that will be put into the envelope
        SoapObject soapObject = new SoapObject(WSDL_TARGET_NAMESPACE, OPERATION_NAME);
        envelope.setOutputSoapObject(soapObject);

        //put something on the card  
        PropertyInfo propertyInfo = new PropertyInfo();
        propertyInfo.setType(PropertyInfo.STRING_CLASS);
        propertyInfo.setName("macAddress");
        propertyInfo.setValue(macAddress);
        soapObject.addProperty(propertyInfo);

        //call WebService
        try {
            HttpTransportSE httpTransportSE = new HttpTransportSE(SOAP_ADDRESS);
            httpTransportSE.debug = true;
            httpTransportSE.call(SOAP_ACTION, envelope);
            Object response = envelope.getResponse();
            return response.toString();             
        } catch (Exception e) {
            String text = "Oops!! App has crashed with exception: "+e.getMessage() + "\n" + "Stack trace below:\n";
            StackTraceElement[] stackTrace = e.getStackTrace();
            for (StackTraceElement stackTraceElement : stackTrace) {
                text += stackTraceElement.toString() + "\n";
            }
            return text;
        }
    }

    @Override
    protected void onPostExecute(String result) {
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
        TextView textView = (TextView)findViewById(R.id.textView1);
        textView.setText(result);
    }

}

And to call the AsyncTask, just do this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    new AsyncTaskWebServiceCaller().execute();
}

Upvotes: 5

Related Questions