Async Task在2.3.3中没有运行doInBackground

时间:2013-03-27 14:33:48

标签: android android-asynctask

我正在开发一款Android应用。现在我想使用AsyncTask。它在android 4.1中运行完美但是doInBackground方法没有在android 2.3.3中运行。 logcat中没有错误..

我有一个扩展vom AsyncTask的类。所以我用新的Class()。execute()启动它。构造函数始终有效。

你知道如何解决android 2.3.3中的问题吗?

修改 我的代码:

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;
    }
}

我有目标版本17和minSdkVersion 7.您还需要更多信息吗?我使用这段代码执行它:

new TopicList().execute();

logcat中的错误日志仅显示构造函数的工作原理。

1 个答案:

答案 0 :(得分:5)

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

阅读上面的文档后,很明显AsyncTask在版本2.3.3中应该可以正常工作(它在API 3 = Android 1.5中添加)。

你确定正在传递正确的参数吗?下面是一个简单的异步任务示例,它调用WebService并在活动的TextView中显示结果,我希望这可以帮助您走上正确的轨道:

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);
    }

}

要调用AsyncTask,只需执行以下操作:

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