GET request in TeaVM

78 Views Asked by At

I want to make a GET request to an URL in Java so that it can be transpiled to Javascript via TeaVM. I understand that I can't use the java.net API, because it is not supported by TeaVM. So I tried to make a Javascript-function which can be used by TeaVM.

@JSBody(params = { "url", "callback" }, script = "fetch(url)\n"
                                                     + ".then(response => response.text())\n"
                                                     + ".then(text => callback.complete(text))\n"
                                                     + ".catch(error => callback.error(error.message));")
    static native void fetch(String url, FetchCallback callback);

My Java Interface is simple:

public interface FetchCallback {
        void complete(String result);
        void error(String message);
    }

When i run the install Maven target, I get this error:

[ERROR] Method org.example.Client.fetch(Ljava/lang/String;Lorg/example/Client$FetchCallback;)V is not a proper native JavaScript method declaration: its 2th parameter has wrong type

Unfortunately my JS knowledge is very limited (hence TeaVM) so I can't make sense of this error message. Could someone please help me with how to make a GET request in TeaVM?

1

There are 1 best solutions below

0
Alexey Andreev On BEST ANSWER

According to documentation FetchCallback must extend JSObject interface. BTW, TeaVM already comes with wrappers for XMLHttpRequest.