Search code examples
javagwtjsni

Passing String from JSNI to Java - GWT


I am having problem passing String to a Java method in my GWT project:

public final native String waveIt()/*-{
    var instance = this;
    var data = $wnd.Waverecorder.data();
    var strData = data.toString();
    var arr = strData.split(',');
    for (var i = 0; i < arr.length; i++) {
        var data = arr[i];
        console.log(data);
        [email protected]::updateWave(Ljava/lang/String;)(data.toString());
    }
}-*/;

Looking from the console log of Chrome/Firefox I can see that I get the right data (this is the exact log I get):

-0.00006103515625
-0.00006103515625
-0.00006103515625
-0.05072021484375
-0.553833007812
 (more data omitted)

When the GWT java method received the data it is empty. What could be the reason?


Solution

    1. This method should be void, because you do not return a string - you call a Java method from it.

    2. Looking at your code, you don't need var instance = this; and you can remove instance. before @com.

    3. You declare var data twice: before the loop and inside the loop. Instead of calling your Java method with data.toString(), you can call it with arr[i].