簡體   English   中英

AsyncTask.doInBackground沒有在2.3上調用,工作在4.0+上

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

我有這個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();
        }
    }

我嘗試在3G上做,總是有效。 然后我連接到Wi-Fi。 AsyncTask在4.0+上調用,但在2.3.7-中調用。

我錯過了什么嗎?

嘗試創建一個單獨的線程並在那里做你的東西,用Activity.runInUiThread()發布你的結果。 看看是否有任何問題。 如果沒有問題,那么可能是因為一些AsyncTask錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM