简体   繁体   English

从JSNI到Java传递字符串 - GWT

[英]Passing String from JSNI to Java - GWT

I am having problem passing String to a Java method in my GWT project: 我在GWT项目中将String传递给Java方法时遇到问题:

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): 从Chrome / Firefox的控制台日志中可以看到我得到了正确的数据(这是我得到的确切日志):

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

When the GWT java method received the data it is empty. 当GWT java方法接收到data它为空。 What could be the reason? 可能是什么原因?

What do you mean by: 你是什​​么意思:

When the GWT java method received the data it is empty. 当GWT java方法接收到数据时,它为空。

Are you talking about the string that waveIt() should return? 您是否在谈论waveIt()应该返回的字符串?

The bug may be that there is no return statement in waveIt() . 错误可能是waveIt()没有return语句。

  1. This method should be void, because you do not return a string - you call a Java method from it. 此方法应该为空,因为您不返回字符串-您从该字符串中调用Java方法。

  2. Looking at your code, you don't need var instance = this; 查看您的代码,您不需要var instance = this; and you can remove instance. 您可以删除实例。 before @com. @com之前。

  3. You declare var data twice: before the loop and inside the loop. 您两次声明var数据:在循环之前和循环内部。 Instead of calling your Java method with data.toString(), you can call it with arr[i]. 不用使用data.toString()来调用Java方法,而是可以使用arr [i]来调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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