Sau*_*ena 16 java gwt json http-get httpresponse
我试图在我的GWT应用程序中通过servlet获取请求.在编译代码时,我收到了这些错误.
[ERROR] Line 16: No source code is available for type org.apache.http.client.ClientProtocolException; did you forget to inherit a required module?
[ERROR] Line 16: No source code is available for type org.apache.http.ParseException; did you forget to inherit a required module?
[ERROR] Line 16: No source code is available for type org.json.simple.parser.ParseException; did you forget to inherit a required module?
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能消除这些错误?GWT不支持这些类吗?
以下是我正在使用的代码
public String getJSON() throws ClientProtocolException, IOException, ParseException{
HttpClient httpclient = new DefaultHttpClient();
JSONParser parser = new JSONParser();
String url = "some - url - can't disclose";
HttpResponse response = httpclient.execute(new HttpGet(url));
JSONObject json_data = (JSONObject)parser.parse(EntityUtils.toString(response.getEntity()));
JSONArray results = (JSONArray)json_data.get("result");
}
Run Code Online (Sandbox Code Playgroud)
这段代码工作正常如果我在通常的java项目/控制台应用程序上使用它.
Via*_*lov 13
如果您使用Maven,那么您可以这样做.
带参数compileSourcesArtifacts的maven-gwt-plugin将完成所有源代码管理工作,并允许您编译GWT模块.
在要包含的模块中,您必须启用源包的生成.并查看Github上的外部GWT模块示例.
GWT无法将任何Java类编译为JavaScript客户端代码.它只支持几个基类.请参阅GWT JRE仿真参考.
示例pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<dependencies>
<dependency>
<groupId>com.my.group</groupId>
<artifactId>my-artifact</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<!-- ... -->
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>gwt-maven-plugin</artifactId>
<version>2.5.0</version>
<!-- ... -->
<configuration>
<compileSourcesArtifacts>
<compileSourcesArtifact>com.my.group:my-artifact</compileSourcesArtifact>
</compileSourcesArtifacts>
</configuration>
</plugin>
</plugins>
</build>
</project>
Run Code Online (Sandbox Code Playgroud)
在GWT中运行的Java代码被转换为Javascript,因此在JVM上工作的某些类将无法与GWT一起使用.编写HttpClient和相关类是为了在JVM上工作,完全支持打开套接字,这是Web浏览器中不允许的,因此不能使用这些类.
要打开与您正在使用的服务器的连接(根据浏览器Same Origin Policy),请考虑RequestBuilder类,它允许您提供URL和HTTP方法,以及可选的标头,参数,数据等.此类是JavaScript中XmlHttpRequest对象的抽象,通常用于普通JS中的AJAX请求.