2017-08-08 56 views
0

我正在学习GWT,我想下面的例子中,我试图通过Java函数中的JSON对象。如何使用JSNI在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 [email protected]::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 
+0

难道注释掉一样,在你的代码? – Henry

+0

@Jim Garrison谢谢!!,请帮我学习如何编辑问题 – Ameen

+0

中的错误部分单击“编辑1小时前”链接以比较两个版本。 –

回答

1

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

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

+0

此外,使用此聊天提问,提出建议或初步指导等。https://gitter.im/gwtproject/gwt –

0

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

native的主体是JavaScript,而不是Java。

0

你声明你的变量jsonResult到你的解析器中:function(),jsonResult只存在于那个函数中。这就是为什么系统说你缺少 ;声明之前 因为您永远不会将varieble声明为createTestNativeFunction()。

Plus 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 [email protected]::onParse(Lorg/sgx/jsutil/client/JsObject;)(jsonResult); 
    } 

    void onParse(jsonResult){ 
     console.log(jsonResult); 
    } 
} 
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) { 

     [email protected]::onParse(Lorg/sgx/jsutil/client/JsObject;)(jsonResult); 
    }; 

}-*/; 

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

}

相关问题