PlayN 项目中的 HTTP 请求
我想使用 RequestBuilder 在我的 PlayN 项目中发出 HTTP 请求,如下所述: http://code.google.com/webtoolkit/doc/latest/DevGuideServerCommunication .html#DevGuideHttpRequests
我在模块 xml 文件中添加了标签:
但我仍然有以下编译错误:
无法解析导入 com.google
我还应该做些什么来使我的项目编译吗?
这是代码:
import com.google.gwt.http.client.*;
...
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
}
I want to use RequestBuilder to make HTTP requests in my PlayN project as described here:
http://code.google.com/webtoolkit/doc/latest/DevGuideServerCommunication.html#DevGuideHttpRequests
I added the tag in my module xml file:
but I still have the following compilation error:
The import com.google cannot be resolved
Is there something else I should do to make my project compile?
Here is the code:
import com.google.gwt.http.client.*;
...
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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用 Maven 进行构建(我怀疑您可能是这样),请绝对确保以下依赖项位于您的 html/pom.xml 中。
如果您使用的是 GWT 其他版本,则可能需要更改版本高于 2.4.0
编辑: 现在我知道您正在运行 Java 应用程序(基于下面的注释)而不是 GWT 应用程序,您可能需要使用除以下内容之外的其他内容发出 HTTP 请求GWT 的 HTTP 客户端。您需要删除上述依赖项,并查看此问题的答案,以深入了解如何操作如果
您需要在 GWT 和 Java PlayN 目标中发出 HTTP 请求,您可能需要抽象核心模块中所需的 HTTP 客户端接口,并在 java 和GWT 模块。我描述了使用 Gin 和 Guice 注入 AsyncService<> 的 java 和 GWT 特定实例。 这里的答案中的对象,并且可以使用类似的方法注入每个平台所需的适当的 HTTP 客户端实例如果需要的话...
If you're using Maven for your build (which I suspect you may be), make absolutely sure that the following dependency is in your html/pom.xml
You might need to change the version if you're using a version of GWT other than 2.4.0
Edit: Now that I know you are running a Java application (based on the comments below) and not a GWT application, you're likely going to need to make HTTP requests with something other than GWT's HTTP client. You'll want to remove the aforementioned dependency and take a look at the answers to this question for some insight into how to do that...
If you're needing to make HTTP requests in both the GWT and Java PlayN targets, you're likely going to need to abstract the HTTP client interface needed in the core module and provide the appropriate concrete implementations in the java and GWT modules. I describe using Gin and Guice to inject java and GWT specific instances of AsyncService<> objects in this answer here, and a similar approach can be used in injecting the appropriate HTTP client instance required on a per platform basis if necessary...