简体   繁体   English

AsyncTask.doInBackground没有在2.3上调用,工作在4.0+上

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

I have this AsyncTask: 我有这个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. 我尝试在3G上做,总是有效。 Then I connect to Wi-Fi. 然后我连接到Wi-Fi。 The AsyncTask gets called on 4.0+, but not in 2.3.7-. AsyncTask在4.0+上调用,但在2.3.7-中调用。

Am I missing something? 我错过了什么吗?

Try creating a separate Thread and do your stuff in there, post your results with Activity.runInUiThread(). 尝试创建一个单独的线程并在那里做你的东西,用Activity.runInUiThread()发布你的结果。 See if there's any problem with that. 看看是否有任何问题。 If there's no problem, then it's perhaps because of some AsyncTask bug. 如果没有问题,那么可能是因为一些AsyncTask错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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