AsyncHttpClient usage inside listener functions (and reuse of these functions)

156 Views Asked by At

I'm using the AsynHttpClient from LoopJ for my Android/Java app.

After first block of commands, I do login call to the server. If it's ok, I call other APIs to retrieve data.

For now, I'm calling the next API after the previous one was ended ok. Only after the last call is done, I send the app to the next intent. Is this the best approach?

Second question, how to deal with the failure-send_refresh_call-retry API??

Finally, is there a way to write only one listener that is intelligent enough to discover when all the requests are answered and it proceeds to the next intent?

public void clickLogin(View view) {

[...]

    AsyncHttpClient client = new AsyncHttpClient();
    RequestParams rp = new RequestParams();
    rp.add("grant_type", "password");
    rp.add("username", String.valueOf(txtUsuario.getText()));
    rp.add("password", String.valueOf(txtSenha.getText()));
    rp.add("client_id", "mobileApp");
    HttpUtils.postEncoded("token", rp, new JsonHttpResponseHandler() {

        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
            super.onSuccess(statusCode, headers, response);

            [...]

            HttpUtils.get(getApplicationContext(),"api/coins", headers, reqparams, new JsonHttpResponseHandler() {

                @Override
                public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
                    super.onSuccess(statusCode, headers, response);
                    [...]
                    HttpUtils.get(getApplicationContext(),"api/travels", headers, reqparams, new JsonHttpResponseHandler() {

                        @Override
                        public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
                            super.onSuccess(statusCode, headers, response);

                            Intent intent = new Intent(getApplicationContext(), TravelsActivity.class);
                            startActivity(intent);
                        }

                        @Override
                        public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                            super.onFailure(statusCode, headers, throwable, errorResponse);
                            if (statusCode == 401) {
// *** Treat unauthorized. Call other API to refresh token ***
                            }
                            else {
                                e.printStackTrace();
                            }
                        }
                    }
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                    super.onFailure(statusCode, headers, throwable, errorResponse);
                    if (statusCode == 401) {
// *** Treat unauthorized. Call other API to refresh token ***
                    }
                    else {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
0

There are 0 best solutions below