3

What I want to do is when onSuccess method executed, the queryLogin return true,while if onFailuer method executed , the queryLogin return false; But as you know, in java, I cannot modify an outer class value from the inner class. So I just wonder how can I solve the problem and achieve my aim.

 public static boolean queryLogin(String username, String passowrd){
    boolean isSuccess = false;
    params.put("username", username);
    params.put("password", passowrd);
    VStarRestClient.post(LOGIN_URL, params, new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            isSuccess = true;//cannot
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
            String responseContent = new String(responseBody);
            isSuccess = false;//cannot
        }
    });

    return isSuccess;
}
Shuai Wang
  • 335
  • 1
  • 8
  • 20

4 Answers4

1

Make the class methods returning a boolean and catch it

boolean isSucces = VStarRestClient.post(LOGIN_URL, params, new AsyncHttpResponseHandler() {
    @Override
    public boolean onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
        return true;
    }

    @Override
    public boolean onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
        String responseContent = new String(responseBody);
        return false;
    }
});

UPDATE:

As you can see 'onSuccess' is an override method, so if I change the return value from 'void' to 'boolean', it is not override still

Actually, if variables are not final you cannot manipulate them inside inner classes... But, being careful, knowing you didn't created the class, AND If it is used only locally, in this method scope, then you can use this:

final boolean isSuccess[] = {false};
VStarRestClient.post(LOGIN_URL, params, new AsyncHttpResponseHandler() {
    @Override
    public boolean onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
        isSuccess[0] = true;
    }

    @Override
    public boolean onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
        String responseContent = new String(responseBody);
        isSuccess[0] = true;
    }
});
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • As you can see 'onSuccess' is an override method, so if I change the return value from 'void' to 'boolean', it is not override still. – Shuai Wang Oct 14 '15 at 09:03
  • @ShuaiWang this does not mean you cannot change class declaration, but if you cannot, please, kindly check my update – Jordi Castilla Oct 14 '15 at 09:12
1

You should add a specific method to your outer class, and call it from inner class. This method should receive an argument and simply setup an outer class member variable; then use that variable as you wish.

Andrey Kopeyko
  • 1,556
  • 15
  • 14
1

You're trying to mix synchronous and asynchronous behaviour here, which cannot work.

The post() method just triggers a HTTP post and execution of your login method continues to its end without waiting for the result of that post. That's what the AsyncHttpResponseHandler is here for. It is called when the reply to the post() arrives.

Network activities (and all other tasks, which might take a long time) are always asynchonously to not freeze the UI of your app.

Ridcully
  • 23,362
  • 7
  • 71
  • 86
  • yes,thanks. I have a better understand of the callback method mechanism. – Shuai Wang Oct 19 '15 at 02:53
  • In `Dart` language of `Flutter` framework it is very simply possible to convert an `Async` method to `Sync` by using ".then(){}" keyword at the end of the method, I have the same problem in Android Java and I think Java more powerful to say it is impossible. – Mohsen Emami Feb 16 '20 at 14:46
0

You may create method inside your class like

processQueryResult(boolean isSuccess){
   //..some logic with isSuccess value
}

at call it from both onSuccess and onFailure with corresponding argument.

Ilya Blokh
  • 11,923
  • 11
  • 52
  • 84