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

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

问题描述



我试过

<$>

我需要实现一个调用Web服务并返回响应的函数。 p $ p> 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);
尝试{
builder.sendRequest(null,new RequestCallback(){
@Override
public void onResponseReceived(Request request,
Response response){
result = response.getText();
System.out.println(response.getText());
//我需要返回result
}
@Override
public void onError(请求请求,Throwable异常){}
});
返回结果; //在收到响应之前返回结果。因此我得到返回值initial_value。
}
catch(RequestException e){}
return null;

在调用 getFolderJson() web服务被成功调用。但是在收到respnse之前返回 result 。所以我得到了retunr的值initial_value。

如何在 getFolderJson()函数中返回响应的值?

解决方案

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



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

因此,gwt的正常方式应该是您的方法返回 void ,并且您在响应可用时传递一个带回调的附加参数。

  public void getFolderJson(String path,Callback< String,String> callback){
RequestBuilder builder = new RequestBuilder(...);
尝试{
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 Promise 语法代替请求生成器:

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


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

I tried

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;
    }

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 does not support synchronous Ajax, so you have to code your app using asynchronous pattern.

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.

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());  
    }  
}

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 Request构建器 - 如何返回响应字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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