【问题标题】:HTTP requests in a PlayN projectPlayN 项目中的 HTTP 请求
【发布时间】:2012-02-08 15:32:40
【问题描述】:

我想使用 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
}

【问题讨论】:

  • 你能发布编译错误所抱怨的完整导入语句吗?在我看来,您在某处缺少依赖项...
  • gwt-user jar 在你的类路径中吗?

标签: playn


【解决方案1】:

如果您在构建中使用 Maven(我怀疑您可能是),请绝对确保以下依赖项位于您的 html/pom.xml 中

<dependency>
  <groupId>com.google.gwt</groupId>
  <artifactId>gwt-user</artifactId>
  <version>2.4.0</version>
  <scope>provided</scope>
</dependency>

如果您使用的 GWT 版本不是 2.4.0,则可能需要更改版本

编辑: 现在我知道您正在运行 Java 应用程序(基于下面的 cmets)而不是 GWT 应用程序,您可能需要使用 GWT 的 HTTP 客户端以外的东西发出 HTTP 请求。您需要删除上述依赖项并查看this question 的答案以了解如何执行此操作...

如果您需要在 GWT 和 Java PlayN 目标中发出 HTTP 请求,您可能需要抽象核心模块中所需的 HTTP 客户端接口,并在 java 和 GWT 中提供适当的具体实现模块。我描述了使用 Gin 和 Guice 在 this answer here 中注入 AsyncService 对象的 java 和 GWT 特定实例,如果需要,可以使用类似的方法来注入每个平台所需的适当 HTTP 客户端实例......

【讨论】:

  • 现在它可以编译,但我得到一个运行时异常:线程“main”中的异常 java.lang.NoClassDefFoundError: com/google/gwt/http/client/RequestException
  • 抱歉 -- 删除 &lt;scope&gt;provided&lt;/scope&gt; 行或将其更改为 &lt;scope&gt;runtime&lt;/scope&gt; 并查看它是否有效...
  • 现在我得到:线程“main”中的异常 java.lang.UnsatisfiedLinkError: com.google.gwt.xhr.client.XMLHttpRequest.create()Lcom/google/gwt/xhr/client/XMLHttpRequest ;
  • 顺便说一句,我正在尝试将项目作为 Java 应用程序运行。也许这里不支持 playN 和 GWT 之间的混合?
  • 嘿,是的——你需要用别的东西进行 HTTP 调用。我打赌 XMLHttpRequest.create() 正在进行 JSNI 调用,这在 Java 中是行不通的。您可能想查看 stackoverflow.com/a/1359700/302804 以了解在 Java 应用程序中进行调用的一些见解...
猜你喜欢
  • 1970-01-01
  • 2019-10-10
  • 2021-12-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-10
  • 1970-01-01
  • 2021-12-05
  • 2018-09-04
相关资源
最近更新 更多