java - GWT 中的同步 RPC 调用

标签 java gwt jsni

(光是这个头衔就应该让人们从木制品中走出来用棍子砸我,但请听我说完)。

我有一个用例,我需要从异步调用中返回一个值。 (我使用的是 GWT 平台,但概念是相同的。)我声明了一个最终的 JavaScriptObject 数组,然后在 AsyncCallback 中分配了值。但是,我需要返回值,并且该方法在 AsyncCallback 完成之前返回。因此,我需要以某种方式阻止直到 AsyncCallback 完成。我需要另一个方法中的返回值,或者我只是在 onSuccess() 中执行我需要的操作。

我已经尝试过循环、定时器和其他一些方法,但都没有成功。谁能帮忙?

@Override
public JavaScriptObject doGetWhereAmIMarker(final double lng, final double lat) {

    final JavaScriptObject[] markerArray = new JavaScriptObject[1];  // ugly hack, I know
    dispatch.execute(new GetLocationDescriptionsAction(lng, lat), new AsyncCallback<GetLocationDescriptionsResult>() {
        @Override
        public void onFailure(Throwable caught) {
            caught.printStackTrace();
        }

        @Override
        public void onSuccess(GetLocationDescriptionsResult result) {
            Map<String, Location> resultMap = result.getResult();
            StringBuffer message = new StringBuffer();
            for (String key : resultMap.keySet()) {
                message.append(key).append(": ").append(resultMap.get(key)).append("\n");
            }

            Map tempMap = new HashMap();
            tempMap.put("TITLE","Location Information");
            tempMap.put("LAT", lat);
            tempMap.put("LNG", lng);
            tempMap.put("CONTENTS", message.toString());

            JavaScriptObject marker = GoogleMapUtil.createMarker(tempMap);
            markerArray[0] = marker;
            if (markerArray[0] != null) {
                GWT.log("Marker Array Updated");
            }

        }
    });

    return markerArray[0];
}

更新:根据要求,这里是调用 doGetWhereIAmMarker() 的代码。我试过使用一个单独的 native 方法,将 Google Map 对象(作为 JavaScriptObject)作为参数,但似乎在 native 方法之间传递该对象会破坏更新该对象的能力。

public native void initMap(JavaScriptObject mapOptions, JavaScriptObject bounds, JavaScriptObject border, JsArray markerArray, Element e) /*-{

    // create the map and fit it within the given bounds
    map = new $wnd.google.maps.Map(e, mapOptions);
    if (bounds != null) {
        map.fitBounds(bounds);
    }

    // set the polygon for the borders
    if (border != null) {
        border.setMap(map);
    }

    // set up the info windows
    if (markerArray != null && markerArray.length > 0) {
        var infoWindow = new $wnd.google.maps.InfoWindow({
            content:"InfoWindow Content Goes Here"
        });

        for (var i = 0; i < markerArray.length; i++) {
            var marker = markerArray[i];
            marker.setMap(map);
            $wnd.google.maps.event.addListener(marker, 'click', function() {
                infoWindow.setContent(marker.content);
                infoWindow.open(map, this);
            });
        }
    }

    // need to reference the calling class inside the function(), so set a reference to "this"
    var that = this;

   $wnd.whereAmI=function(lng, lat) {
        that.@org.jason.mapmaker.client.view.MapmakerMapViewImpl::whereAmI(DD)(lng,lat);
   }

    $wnd.google.maps.event.addListener(map, 'click', function(event) {
        var lat = event.latLng.lat();
        var lng = event.latLng.lng();
        $wnd.whereAmI(lng, lat);
    });

}-*/;

最佳答案

在某些时候我不得不做类似的事情,但最终我删除了那些支持异步内容的代码。因此,我无法提供您需要使用的确切代码,只能给出一些关于如何使用它的指示。

  • 首先,this blog描述了如何使用 javascript 执行同步 AJAX。
  • 其次,您必须提供对同步调用的支持。问题是GWT 不支持提供同步AJAX 调用的参数。很可能是他们不想鼓励使用它。因此,您需要使用 JSNI 将适当的方法添加到 XMLHttpRequest(您可能会扩展),然后添加到 RequestBuilder(也应该扩展它)。
  • 最后,使用扩展的 RequestBuilder 修改您的服务。像

((ServiceDefTarget)service).setRpcRequestBuilder(requestBuilder);

最后 - 来自同一篇博文(略有断章取义):

Because of the danger of a request getting lost and hanging the browser, synchronous javascript isn't recommended for anything outside of (onbefore)unload event handlers.

关于java - GWT 中的同步 RPC 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7353310/

相关文章:

javascript - (MyClass) JavaScriptObject.createObject() 与 JavaScriptObject.createObject().cast()

json - GWT - JSNI - 传递 json 对象

java - 引用注释处理器生成的类

java - 使用 JSNI 将 List<String> 转换为 Javascript

java - 功能修改数据库时是否可以使用功能标志/切换?

java - 使用返回通用接口(interface) <T> 的工厂的类解析 T 列表,而不仅仅是 T

android - 用于构建 Android 应用程序的 Gwt

java - 无法将 selectonHandler 添加到 Gwt 中的 TabPanel

java - 如何在 Spring MVC 中映射 JSON 对象?

java - Joda 时间,以毫秒为单位的周期