2017-08-08 10 views
0

私はGWTを学んでいますが、私はJSONオブジェクトをjava関数で渡そうとした例を試しています。JSNIを使用してJSONオブジェクトをJava関数で渡すにはどうすればよいですか?

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

2時間前に編集した「編集済み1時間前」のリンクをクリックしてください。 –

答えて

1

あなたは本当にJSNIを回避しようとしてください。 JSNIをまったく使用しないコードの99%を書くことができます。本当に必要な場合は、代わりに新しいJsInteropを使用してください。文書はまだ早い段階ですが、これはdocumentation hereです。

JsInteropまたはJSNIを使用する必要がある場合は、通常、JS libをラップする必要があるため、最初に既にラップされているかどうかを確認してください。それ以外のラッパーライブラリを使用してJSライブラリをラップする方法を学ぶことができます。

+0

また、このチャットを使用して簡単な質問をしたり、提案を求めたり、初期ガイダンスなどを求めたりします。https://gitter.im/gwtproject/gwt –

0

System.out.println()は、Javaの機能である、あなたはconsole.log()を探しています。

nativeの本文はJavaScriptであり、Javaではありません。

0

変数jsonResultをパーサに宣言しています:function()、jsonResultはその関数内にのみ存在します。システムがあなたのことを言っているのはなぜですか? がありません。 before文 variebleをcreateTestNativeFunction()に宣言することは決してないからです。

プラス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; 
} 

}

関連する問題