简体   繁体   中英

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);
        instance.@com.mycode.wave.showcase.client.Showcase::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?

What do you mean by:

When the GWT java method received the data it is empty.

Are you talking about the string that waveIt() should return?

The bug may be that there is no return statement in waveIt() .

  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].

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM