gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:21:39 25 4
gpt4 key购买 nike

我有这段 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com