1
votes

I'm running Android 4.2.2 on using loopj android-async-http. I recently upgraded from android-async-http-1.4.3.jar to android-async-http-1.4.4.jar. As I upgraded, I noticed a change in my app, which I traced back to a change in order of callbacks onFailure(), onSuccess(), and onFinish(). Now onFinish() is systematically invoked before onFailure() or onSuccess() on failure or success respectively.

I looked into loopj error #411 and into https://github.com/loopj/android-async-http/blob/master/library/src/main/java/com/loopj/android/http/AsyncHttpRequest.java#L51 to follow the argument, that onFinish() cannot be invoked before either onFailure() or onSuccess(). I tend to agree.

None the less, for me the order is changed and for me this change came with the upgrade from 1.4.3 to 1.4.4.

It seems issue error #411 was resolved. Only, I dont understand how. Can anyone please help me understand what I may be doing wrong?

The snippet of code below demonstrates the order i'm seeing as follows

ASYNC-TEST, onStart ASYNC-TEST, onFinish ASYNC-TEST, onFailure

or

ASYNC-TEST, onStart ASYNC-TEST, onFinish ASYNC-TEST, onSuccess

public static void testAsync() {
    JSONObject body = new JSONObject();
    try {
        body.put("phone", "18037771234");
        body.put("password", "secret"); 
    } catch (JSONException e) {
        e.printStackTrace();
    }
    try {
        new AsyncHttpClient().post(context, "http://myapp.nodejitsu.com/sessions", new StringEntity(body.toString()), "application/json", new JsonHttpResponseHandler() {
            @Override
            public void onStart() {
                Log.d("ASYNC-TEST", "onStart");
            }
            @Override
            public void onSuccess(JSONObject response) {
                Log.d("ASYNC-TEST", "onSuccess");
            }
            @Override
            public void onFailure(Throwable t, JSONObject response) {
                Log.d("ASYNC-TEST", "onFailure");
            }
            @Override
            public void onFinish() {
                Log.d("ASYNC-TEST", "onFinish");
            }
        });
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
}
1

1 Answers

0
votes

I had some similiar problems because I was parsing JSON and apparently it takes more time than you would think. Moved that part of code in another async task and it worked like a charm. Hope it will help you, it did to me.