从GWT调用一些在线服务

时间:2014-01-07 05:23:07

标签: java gwt

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

现在要求从Pure 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";
        }

      }

1 个答案:

答案 0 :(得分:2)

我不会转换您的代码,但这是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
}

你可能会在文档中错过这个

  

要在应用程序中使用HTTP类型,您需要首先通过将以下标记添加到模块XML文件来继承GWT HTTP模块:

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