5

I want to pass a success and a failure callback Java function to a JSNI method.

This is what I get so far but it does not work. How can I fix it?

package c;

public class A {

test(new Callback<String, String>() {

    @Override
    public void onFailure(String reason) {
        Window.alert("fail");
    }

    @Override
    public void onSuccess(String result) {
        Window.alert("suc");
    }
});


native void test(Callback<String, String> callback) /*-{

  var callback = $entry(function(event) {
     [email protected]::onSuccess(Ljava/lang/String;)("success!");
  });

}-*/;

}

1 Answer 1

8

You can call the callback methods in this way:

native void test(Callback<String, String> callback) /*-{
  [email protected]::onSuccess(Ljava/lang/Object;)("success!");
}-*/;
Sign up to request clarification or add additional context in comments.

3 Comments

does this should work for a Callback<Integer,String> too? It gives me back a class cast exception when I try using the returned Integer converted to int with .intValue()
I found out for integer value of Callback<Integer,String> this should be used like: [email protected]::onSuccess(Ljava/lang/Object;)(@java.lang.Integer::valueOf(Ljava/lang/String;)("1"));
Will onFailure() ever be called in this case?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.