java - 从 GWT 调用一些在线服务

标签 java gwt

我有这段 JavaScript 代码,它与服务连接并发送回结果。

现在的要求是从纯 Java 调用相同的服务。

下面是调用该服务的 javascript 代码。

如果有人可以指导我在我的 GWT 应用程序中将此 Javascript 转换为 Java

谢谢

         function verifyValidationSyntax(textToValidate)

         {   
         var url = "https://validation-grammar.example.com/validation_grammar_service/rest/validation_step_validation";  
         var client = new XMLHttpRequest();
         client.open("POST", url, false);
         client.setRequestHeader("Content-Type", "text/plain");
         client.send(textToValidate);
         if (client.responseText==='true') {
         return "true";
         } else {
        return "false";
        }

      }

最佳答案

我不会转换你的代码,但这是 docs 中最甜蜜的例子

String url = "http://www.myserver.com/getData?type=3";
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, URL.encode(url));

try {
  Request request = builder.sendRequest(null, new RequestCallback() {
    public void onError(Request request, Throwable exception) {
       // Couldn't connect to server (could be timeout, SOP violation, etc.)
    }

    public void onResponseReceived(Request request, Response response) {
      if (200 == response.getStatusCode()) {
          // Process the response in response.getText()
      } else {
        // Handle the error.  Can get the status text from response.getStatusText()
      }
    }
  });
} catch (RequestException e) {
  // Couldn't connect to server
}

您可能会在文档中错过这一点

To use the HTTP types in your application, you'll need to first inherit the GWT HTTP module by adding the following tag to your module XML file:

<inherits name="com.google.gwt.http.HTTP" />

关于java - 从 GWT 调用一些在线服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20964500/

相关文章:

java - 存储给定类的所有实例的值

java - 将 RowFilter 与 ArrayList 一起使用

java - 将坏词保留到数据库实体中的最佳方法是什么?

java - 对象[]变量名; vs 对象变量名[];

java - 有更快的 "Map"吗?

GWT 设计器滞后于 xml 源自动完成

java - 正常运行GWT shell,编译时使用-noserver?

java - 如何将元素转换为小部件?

java - 如何在GWT中添加进度条

Java OpenNLP从句子中提取所有名词