后未调用doInbackground

后未调用doInbackground

本文介绍了AsyncTask;调用方法onPreExecute()后未调用doInbackground()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确实在我的项目中添加了一个异步库,并且已经检查过,我不知道为什么代码流不会进入异步任务

I did added async library at my project and checked already, I don't know why code flow doesn't go in to asynctask

代码

public void doMysql()
{
    Log.v("doMysql", "accessed");

    new AsyncTask<Void, Void, String>() {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            Log.e("AsyncTask", "onPreExecute");
        }
        @Override
        protected String doInBackground(Void... params) {
            Log.v("AsyncTask", "doInBackground");

            String msg = "";

            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://172.16.100.172:52273/mysql");

            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

            nameValuePairs.add(new BasicNameValuePair("myday", Integer.toString(day_picker.getYear()) +
                    addZero(day_picker.getMonth() + 1) +
                    addZero(day_picker.getDayOfMonth())));
            nameValuePairs.add(new BasicNameValuePair("mystar", changeStar(day_picker.getMonth() + 1, day_picker.getDayOfMonth())));
            nameValuePairs.add(new BasicNameValuePair("mybt", changeBloodType(blood_picker.getValue())));
            nameValuePairs.add(new BasicNameValuePair("mynum", "" + myPhone.getText()));
            nameValuePairs.add(new BasicNameValuePair("yournum", "" + partnerPhone.getText()));
            nameValuePairs.add(new BasicNameValuePair("myregID", regid));
            try {
                Log.v("setEntity", "before");
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                Log.v("setEntity", "after");

            } catch (UnsupportedEncodingException e1) {
                Log.v("UnsupportedEncodingException", "");
                e1.printStackTrace();
            }
            //172.16.101.28
            try {
                Log.v("post", "before");
                HttpResponse httpresponse = httpclient.execute(httppost);
                Log.v("post", "after");
                Log.v("HttpResponse ",httpresponse.getEntity().toString());
            } catch (ClientProtocolException e) {
                Log.v("ClientProtocolException", "ClientProtocolException");
                e.printStackTrace();
            } catch (IOException e) {
                Log.v("IOException", "IOException");

                e.printStackTrace();
            }

            return msg;
        }
        @Override
        protected void onPostExecute(String msg) {
            Log.v("AsyncTask", "onPostExecute");

        }
    }.execute(null, null, null);
}

我在代码'Log.v("AsyncTask","doInBackground");'中有一条日志语句.

I have a log statement in the code 'Log.v("AsyncTask", "doInBackground");'

但是它没有显示在记录器'Log.v("AsyncTask","doInBackground");'

But it doesn't show up in the logger 'Log.v("AsyncTask", "doInBackground");'

推荐答案

您应使用执行程序执行任务

You should execute your task with executor

task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

因为在较低版本的Android中,所有AsyncTasks都在单个后台线程中执行.因此,新任务可能正在等待,直到其他任务开始工作.

Because in lower versions of Android all AsyncTasks were executed at single background thread. So new tasks might be waiting, until other task working.

在Android的较低版本中(实际上是在HONEYCOMB之前的版本中),您无法在执行程序上执行AsyncTask.

In lower versions in Android (actually on pre-HONEYCOMB) you can't execute AsyncTask on executor.

将代码更改为

public void executeAsyncTask()
{
    AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            Log.e("AsyncTask", "onPreExecute");
        }

        @Override
        protected String doInBackground(Void... params) {
            Log.v("AsyncTask", "doInBackground");
            String msg = null;
            // some calculation logic of msg variable
            return msg;
        }
        @Override
        protected void onPostExecute(String msg) {
            Log.v("AsyncTask", "onPostExecute");
        }
    };

    if(Build.VERSION.SDK_INT >= 11/*HONEYCOMB*/) {
        task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    } else {
        task.execute();
    }
}

这篇关于AsyncTask;调用方法onPreExecute()后未调用doInbackground()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 03:37