0

I know this must be simple, but I'm confused. I'm using AsyncHttpClient in my project. I want to create a new class, say AsyncHttpClient2 which will extend AsyncHttpClient. This class is currently adding a token to every request. I want that if the response is UNAUTHORIZED, it should perform some action.

This is the POST syntax:

String url = "https://ajax.googleapis.com/ajax/services/search/images";
AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.put("q", "android");
params.put("rsz", "8");
client.post(url, params, new JsonHttpResponseHandler() {            
    @Override
    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
       // handler code
   }

    @Override
    public void onFailure(int statusCode, Header[] headers, String res, Throwable t) {
       // error code
    }
});

This is my code:

public class TokenAsyncHttpClient extends AsyncHttpClient {
    public TokenAsyncHttpClient() {
        super();
        this.addHeader("x-access-token", "00000000000000000000000");
    }


    // THIS GIVES ERROR Method does not override method from its superclass

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

        // PERFORM SOME ACTION HERE

    }
}

But this gives me the following error on the @Override line:

Method does not override method from its superclass

What am I doing wrong ? and how can I add a default action in onSuccess ?

mrid
  • 5,782
  • 5
  • 28
  • 71

2 Answers2

3

You should create a class (say TokenHttpResponseHandler) that extends JsonHttpResponseHandler instead of AsyncHttpClient and pass it to the client in this way AsyncHttpClient client.post(url, params, new TokenHttpResponseHandler() {....

Then in the TokenHttpResponseHandler you can override OnSuccess or OnFailure and set its default behaviour.

E.g.

public class TokenHttpResponseHandler extends JsonHttpResponseHandler {
    public TokenHttpResponseHandler() {
        super();
    }


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

        // PERFORM SOME ACTION HERE

    }
}
dalla92
  • 432
  • 2
  • 8
  • I made this class: https://gist.github.com/mridah/7985d5beb0056849858d9a657eebbb75 and I'm using it like `client.get(dataUrl, new TokenHttpResponseHandler() {...}` but still the Logs are not displaying – mrid Nov 20 '17 at 11:43
  • 1
    got it to work by adding `super.onFailure(statusCode, headers, e, errorResponse);` whereever I was using it !! – mrid Nov 20 '17 at 11:50
1

Reading the documentation line 1094 turned out that AsyncHttpClient doesn't have a method called onSuccess. And by reading your question you are using the get method from that class which rely on ResponseHandlerInterface which have its onSuccess method so if you want to change the behaviour of this method you need to implement the ResponseHandlerInterface in some class and use that class in your call.

Yamen Nassif
  • 2,416
  • 2
  • 24
  • 48