Gwt 请求构建器 - 如何返回响应字符串 [英] Gwt Request builder - how to return the response string

查看:17
本文介绍了Gwt 请求构建器 - 如何返回响应字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要实现一个调用 Web 服务并返回响应的函数.

I need to implement a function that calls a web service and return the response.

我试过了

public String getFolderJson(String path) {  
           String result="initial_value";
           StringBuilder param = new StringBuilder();  
           param.append("?sessionId=").append(getSessionId());  
           param.append("&path=").append(path);  
           RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, "https://localhost/folder" + param);  
                   try {  
                        builder.sendRequest(null, new RequestCallback() {  
                        @Override  
                        public void onResponseReceived(Request request,
                                Response response) {  
                              result = response.getText();
                              System.out.println(response.getText());  
                                            //I need to return "result"   
                        }  
                        @Override  
                        public void onError(Request request, Throwable exception) {}  
                          });  
                    return result; //the result get returned before the response is recieved.So i am getting the return value "initial_value".
                   }      
                   catch (RequestException e) {}  
        return null;
    }

在调用 getFolderJson() 时,Web 服务被成功调用.但是 result 在收到响应之前返回.所以我得到了返回值initial_value".
getFolderJson() 函数时如何从响应中返回值?

On calling getFolderJson() the web service is called succesfully. But result is returned before the respnse is recieved. So I am getting the retunr value "initial_value".
How to return the value from the response when getFolderJson() function ?

推荐答案

GWT 不支持同步 Ajax,因此您必须使用异步模式编写应用程序.

GWT does not support synchronous Ajax, so you have to code your app using asynchronous pattern.

GWT 用于执行请求的低级对象是 XMLHttpRequest(除了对于旧的 IE 版本),并且 GWT 总是调用它的 open() 方法,并将 async 设置为 true.因此,同步ajax 的唯一方法是维护您自己的XMLHttpRequest.java 修改版本.但是同步 ajax 是个坏主意,甚至 jQuery 也弃用了这种可能性.

The low level object that GWT uses to perform the request is a XMLHttpRequest (except for old IE versions), and GWT always calls it's open() method with async set to true. So the only way to have synchronous ajax is maintaining your own modified version of XMLHttpRequest.java. But synchronous ajax is a bad idea, and even jQuery has deprecated this possibility.

所以 gwt 中的正常方式应该是您的方法返回 void,并且您传递一个带有回调的附加参数以在响应可用时执行.

So the normal way in gwt should be that your method returns void, and you passes an additional parameter with a callback to execute when the response is available.

public void getFolderJson(String path, Callback<String, String> callback) {  
    RequestBuilder builder = new RequestBuilder(...);
    try {
      builder.sendRequest(null, new RequestCallback() {  
        @Override  
        public void onResponseReceived(Request request, Response response) {
          callback.onSuccess(response.getText());  
        }  
        @Override  
        public void onError(Request request, Throwable exception) {}
          callback.onFailure(exception.getMessage());  
        });
    } catch (RequestException e) {
        callback.onFailure(exception.getMessage());  
    }  
}

我宁愿使用 gwtquery Promises 语法来代替请求构建器:

I'd rather gwtquery Promises syntax for this instead of request-builder one:

  Ajax.get("http://localhost/folder?sessionId=foo&path=bar")
    .done(new Function(){
      public void f() {
        String text = arguments(0);
      }
    });

这篇关于Gwt 请求构建器 - 如何返回响应字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆