4

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?

Charlie-Blake
  • 10,832
  • 13
  • 55
  • 90
  • From the code you posted, a call to the method `doInBackground()` does not depend on you being connected to the wifi or mobile data. You might want to clarify your question – Gil Vegliach Jan 24 '14 at 12:47
  • I know, that's what's driving me mad – Charlie-Blake Jan 24 '14 at 13:16
  • Is it always reproducible? I mean, each time you try via wifi it fails? Have you tried using wifi for other apps, for example a web browser? Have you tried on several devices to see whether it happens in all of them? – nKn Jan 24 '14 at 14:19
  • Curious is that you execute your AsyncTask serially post honeycomb and in parallel before it. That might be some source of bugs but I do not see the link with the wifi... – Gil Vegliach Jan 24 '14 at 14:41
  • @Gil as far as I know it's exactly to prevent the trigger of a bug, see more here: http://www.jayway.com/2012/11/28/is-androids-asynctask-executing-tasks-serially-or-concurrently/ – nKn Jan 24 '14 at 14:43
  • @NKN what bug? I know the execution catch of AsyncTask. That piece of code looks like santirivera92 wants to execute it **always** in parallel, except that is basically equivalent to just call execute() without the if branching. So it looks buggy to me, but still not related to the issue – Gil Vegliach Jan 24 '14 at 15:08
  • @Gil Sorry, instead of bug I would had to say "implementation change"; if you read the article I attached, you'll see that this check is needed if you're executing more than one `AsyncTask` within an app, if you just fire it through execute, just one will trigger. – nKn Jan 24 '14 at 15:39
  • 1
    @NKN I understand what you say but I believe you didn't notice that the code by santirivera92 and the article is essentially different: santiriviera92 has an implementation equivalent to just calling execute(). The article has an implementation that runs the tasks always in parallel. Writing an implementation that performs always serial execution requires using your custom executor (SERIAL_EXECUTOR is not present between Gingerbread and Honeycomb) – Gil Vegliach Jan 24 '14 at 16:10
  • Check out this issue reported : https://code.google.com/p/android/issues/detail?id=68013 – dharmendra Apr 01 '14 at 13:56
  • Could it be that the culprit is in the impl. of postResponseFromServer(...) – Royi Benyossef Apr 28 '14 at 08:42

1 Answers1

0

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.

einschnaehkeee
  • 1,858
  • 2
  • 17
  • 20