简体   繁体   中英

GWT JSNI - how to call external javascript from java code using jsni

I want to call an external javascript function in my gwt application

when i use this code it works fine : i have imported javaScript and i can see it in firebug in client side

public static native void showOfflineMap(String pRegionId, String pMapServerHost, String pMapServerPath, String pMapTileLayers, String pFoiLayers,
            double pMapCenterLon, double pMapCenterLat, int pInitZoom, String pNavPanel, boolean pCustJsFunction, int pMapWidth, int pMapHeight,
            String pAdditional) /*-{
                                alert("ff");
                                                $wnd.createPluginMap(
                                                pRegionId,
                                                pMapServerHost,
                                                pMapServerPath,
                                                pMapTileLayers,
                                                pFoiLayers,
                                                pMapCenterLon,
                                                pMapCenterLat,
                                                pInitZoom,
                                                pNavPanel,
                                                pCustJsFunction,
                                                pMapWidth,
                                                pMapHeight,
                                                pAdditional
                                                )


                                                }-*/;

but when i remvoe

alert("ff");

it throws this exception :

Sun Jul 06 16:36:49 GMT+03:30 2014 com.google.gwt.logging.client.LogConfiguration
SEVERE: (TypeError) @ir.dena.avl.client.offlinemap.MapUtil::showOfflineMap(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;DDILjava/lang/String;ZIILjava/lang/String;)([string: 'offline_map', string: 'http://192.168.10.196:7001', string: '/mapviewer', string: 'mp.LINE_TILE', string: 'NONE', double: 56.0046, double: 32.629365, int: 3, string: 'WEST', bool: false, int: 1000, int: 800, string: 'OVER:MARQUEE:DISTANCE:REDLINE:FOISELECT']): $wnd.createPluginMap is not a functioncom.google.gwt.core.client.JavaScriptException: (TypeError) @ir.dena.avl.client.offlinemap.MapUtil::showOfflineMap(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;DDILjava/lang/String;ZIILjava/lang/String;)([string: 'offline_map', string: 'http://192.168.10.196:7001', string: '/mapviewer', string: 'mp.LINE_TILE', string: 'NONE', double: 56.0046, double: 32.629365, int: 3, string: 'WEST', bool: false, int: 1000, int: 800, string: 'OVER:MARQUEE:DISTANCE:REDLINE:FOISELECT']): $wnd.createPluginMap is not a function
    at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:249)
    at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:576)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNativeVoid(ModuleSpace.java:304)
    at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeVoid(JavaScriptHost.java:107)
    at ir.dena.avl.client.offlinemap.MapUtil$.showOfflineMap(MapUtil.java)
    at ir.dena.avl.client.offlinemap.OfflineMapView.createMainPanel(OfflineMapView.java:31)
    at ir.jalal.core.client.view.BaseView.<init>(BaseView.java:71)
    at ir.dena.avl.client.offlinemap.OfflineMapView.<init>(OfflineMapView.java:20)
    at ir.dena.avl.client.ViewFactory$32.onSuccess(ViewFactory.java:354)
    at com.google.gwt.core.client.GWT$1.execute(GWT.java:286)
    at com.google.gwt.core.client.impl.SchedulerImpl$Task$.executeScheduled$(SchedulerImpl.java:50)
    at com.google.gwt.core.client.impl.SchedulerImpl.runScheduledTasks(SchedulerImpl.java:180)
    at com.google.gwt.core.client.impl.SchedulerImpl.flushPostEventPumpCommands(SchedulerImpl.java:345)
    at com.google.gwt.core.client.impl.SchedulerImpl$Flusher.execute(SchedulerImpl.java:78)
    at com.google.gwt.core.client.impl.SchedulerImpl.execute(SchedulerImpl.java:138)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
    at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:338)
    at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:219)
    at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:576)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:284)
    at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
    at com.google.gwt.core.client.impl.Impl.apply(Impl.java)
    at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:356)
    at sun.reflect.GeneratedMethodAccessor88.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
    at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:293)
    at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:547)
    at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:364)
    at java.lang.Thread.run(Unknown Source)

please help me,

thanks in advance

确保 .nocache.js 之前将脚本与createPluginMap函数链接,并且不要异步链接(我的意思是应该没有<script defer<script async

I assume it's a race condition and the function createPluginMap doesn't exist when you expect it be.

Try replaceing

alert("ff");

with

alert(''+!!$wnd.createPluginMap);

If you get 'false' in the alert, this is the reason. If true, i will try to think of a different answer ;)

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