简体   繁体   中英

AsyncTask.doInBackground not getting called on 2.3, working on 4.0+

I have this AsyncTask:

public static void login(final String email, final String password,
            final String token, final SocketHandler handler) {
        execute(new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(final Void... params) {
                Log.d("ACEPTAR", "DOINBACKGROUND");
                String url = handler.servidor.getUrl();
                url += "/login-usuario";
                String str;
                try {
                    str = postResponseFromServer(url, "mail", email, "pass",
                            password, "tipo", "1", "token", token);
                    Log.d("ACEPTAR", str);
                    final CustomJSONObject object = new CustomJSONObject(str);
                    final CustomJSONObject object2 = new CustomJSONObject();
                    object2.put("datos", object);
                    final CustomJSONArray array = new CustomJSONArray();
                    array.put(object2);
                    handler.on("resultado-login", array);
                } catch (final Exception ex) {
                    ex.printStackTrace();
                    handler.on("error-login", new CustomJSONArray());
                }
                return null;
            }
        });
    }

    @SuppressLint("InlinedApi")
    private static void execute(final AsyncTask<Void, Void, Void> task) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            task.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
        } else {
            task.execute();
        }
    }

I try to do it on 3G, always works. Then I connect to Wi-Fi. The AsyncTask gets called on 4.0+, but not in 2.3.7-.

Am I missing something?

Try creating a separate Thread and do your stuff in there, post your results with Activity.runInUiThread(). See if there's any problem with that. If there's no problem, then it's perhaps because of some AsyncTask bug.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM