如何使用JSNI在java函数中传递JSON对象?

时间:2017-08-08 17:59:32

标签: java gwt jsni

我正在学习GWT,我正在尝试以下示例,其中我尝试在java函数中传递JSON对象。

public class HomeController implements EntryPoint {

public void onModuleLoad() {
    createTestNativeFunction();
    Presenter presenter = new PersenterImpl();
    presenter.go(RootPanel.get());

}

public native void createTestNativeFunction()/*-{

        parser: function() {
            var that = this;
            var jsonResult = JSON.parse({id:42,name:'yo'});
            return this.@com.easylearntutorial.gwt.client.HomeController::onParse(Lorg/sgx/jsutil/client/JsObject;)(jsonResult);
        }

        void onParse(jsonResult){
          System.out.println(jsonResult);
        }
    }
}-*/;
}

我收到以下错误:

Tracing compile failure path for type 'com.easylearntutorial.gwt.client.HomeController'
[ERROR] Errors in 'file:/C:/Users/ameen/workspace/Tutorial/src/com/easylearntutorial/gwt/client/HomeController.java'
[ERROR] Line 31: missing ; before statement
        void onParse(jsonResult){
--------------------------------^
[ERROR] Hint: Check the inheritance chain from your module; it may not be inheriting a required module or a module may not be adding its source path entries properly
[WARN] Server class 'com.google.gwt.dev.shell.jetty.JDBCUnloader' could not be found in the web app, but was found on the system classpath
[WARN] Adding classpath entry 'file:/C:/Program%20Files/gwt-2.7.0/gwt-dev.jar' to the web app classpath for this session
For additional info see: file:/C:/Program%20Files/gwt-2.7.0/doc/helpInfo/webAppClassPath.html

4 个答案:

答案 0 :(得分:1)

你真的应该尽量避免使用JSNI。您可以编写99%的代码,而不是使用JSNI。如果你确实需要它,你应该使用新的JsInterop,文档仍处于早期阶段,但你可以看到这个documentation here

如果你需要使用JsInterop或JSNI,通常是因为你需要包装一个JS库,所以首先,尝试查找它是否已被包装。如果不是,你总是可以使用其他一些包装器库来学习如何包装你的JS库。

答案 1 :(得分:0)

System.out.println()是一个java函数,您正在寻找console.log()

native的正文是JavaScript,而不是Java。

答案 2 :(得分:0)

您将变量jsonResult声明为解析器:function(),jsonResult仅存在于该函数中。这就是系统为什么这么说的原因 失踪 ;在声明之前 因为你永远不会将varieble声明为createTestNativeFunction()。

加上sjakubowski是正确的System.out.println()是一个java函数,你需要在JavaScript上使用console.log()。

试试这个:

public native void createTestNativeFunction(){
    var jsonResult = {};

       parser: function() {
        var that = this;
        jsonResult = JSON.parse({id:42,name:'yo'});
        return this.@com.easylearntutorial.gwt.client.HomeController::onParse(Lorg/sgx/jsutil/client/JsObject;)(jsonResult);
    }

    void onParse(jsonResult){
      console.log(jsonResult);
    }
}

答案 3 :(得分:0)

我做了以下工作来解决我的错误。

public class HomeController implements EntryPoint {

public void onModuleLoad() {
    createTestNativeFunction();
    Presenter presenter = new PersenterImpl();
    presenter.go(RootPanel.get());

}
//  var jsonResult = JSON.parse({id:42,name:'yo'});
public native void createTestNativeFunction()/*-{
    var that = this;
    $wnd.testFunction = function(jsonResult) {

        that.@com.easylearntutorial.gwt.client.HomeController::onParse(Lorg/sgx/jsutil/client/JsObject;)(jsonResult);   
    };  

}-*/;

 public void onParse(JsObject jsonResult){
     int i =42;
 }

}