我正在玩这个从 twitter 获取数据的 Google Web 工具包/GSON 示例。
一切都编译得很好,但是当我作为 Web 应用程序调试时,一切都加载得很好,但是当我单击“搜索”按钮时,出现以下错误:
严重:javax.servlet.ServletContext 日志:调度传入的 RPC 调用 com.google.gwt.user.server.rpc.UnexpectedException 时出现异常:服务方法'public abstract java.util.List com.google.gwt.twittersearch.client.TwitterService .searchTweets(java.lang.String) throws java.io.IOException,java.lang.IllegalArgumentException' throws an unexpected exception: java.lang.RuntimeException: Unable to invoke no-args constructor for class com.google.gwt.twittersearch。 server.TwitterServiceImpl$SearchResponse。向 Gson 注册此类型的 InstanceCreator 可能会解决此问题。在 com.google.gwt.user.server.rpc.RPC.encodeResponseForFailure(RPC.java:385) 在 com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:588) 在 com.google .gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:755) 的 HttpConnection$RequestHandler.content(HttpConnection.java:938) org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218) org.mortbay .jetty.HttpConnection.handle(HttpConnection.java:404) 在 org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409) 在 org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582 ) 原因:java.lang.RuntimeException:无法为类 com.google.gwt.twittersearch.server.TwitterServiceImpl$SearchResponse 调用无参数构造函数。向 Gson 注册此类型的 InstanceCreator 可能会解决此问题。在 com.google.gson.internal.ConstructorConstructor$8.construct(ConstructorConstructor.java:167) 在 com.google.gson.internal.bind。
这是 TwitterServiceImpl 代码:
package com.google.gwt.twittersearch.server;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.List;
import com.google.gwt.twittersearch.client.Tweet;
import com.google.gwt.twittersearch.client.TwitterService;
import com.google.gson.Gson;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
@SuppressWarnings("serial")
public class TwitterServiceImpl extends RemoteServiceServlet implements
TwitterService {
@Override
public List<Tweet> searchTweets(String query) throws IllegalArgumentException, IOException {
query = query.trim();
if (query.isEmpty()) {
throw new IllegalArgumentException("No search query specified.");
}
// see: https://dev.twitter.com/docs/api/1/get/search
String q = URLEncoder.encode(query, "UTF-8");
URL url = new URL("http://search.twitter.com/search.json?q=" + q);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream response = null;
try {
response = connection.getInputStream();
return parseSearchResponse(response);
} finally {
if (response != null) {
response.close();
}
}
}
@Override
public String getPrivacyPolicy() throws IOException {
// see: https://dev.twitter.com/docs/api/1/get/legal/privacy
URL url = new URL("https://api.twitter.com/1/legal/privacy.json");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream response = null;
try {
response = connection.getInputStream();
return parsePolicyResponse(response);
} finally {
if (response != null) {
response.close();
}
}
}
/**
* Parses the privacy policy response returned from the Twitter API.
* @param response the response
* @return the privacy policy
* @throws IOException if there was a problem reading the response
*/
private String parsePolicyResponse(InputStream response) throws IOException {
Reader reader = new InputStreamReader(response);
PrivacyPolicyResponse privacyPolicyResponse = new Gson().fromJson(reader, PrivacyPolicyResponse.class);
return privacyPolicyResponse.privacy;
}
/**
* Parses the search response returned from the Twitter API.
* @param response the response
* @return the search results
* @throws IOException if there was a problem reading the response
*/
private List<Tweet> parseSearchResponse(InputStream response) throws IOException {
Reader reader = new InputStreamReader(response);
SearchResponse searchResponse = new Gson().fromJson(reader, SearchResponse.class);
return searchResponse.results;
}
private class PrivacyPolicyResponse {
public String privacy;
}
private class SearchResponse {
public List<Tweet> results;
}
}
这是入口点代码:
package com.google.gwt.twittersearch.client;
import java.util.List;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
/**
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class TwitterSearch implements EntryPoint {
private Button privacyPolicyButton;
private Button searchButton;
private TextBox searchQueryTextBox;
private Panel resultsPanel;
private Label errorLabel;
private Image loadingImage;
private final TwitterServiceAsync service = GWT.create(TwitterService.class);
/**
* This is the entry point method.
*/
public void onModuleLoad() {
createWidgets();
layoutWidgets();
}
private void createWidgets() {
searchQueryTextBox = new TextBox();
searchButton = new Button("Search");
searchButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
setLoading(true);
String query = searchQueryTextBox.getText();
service.searchTweets(query, new AsyncCallback<List<Tweet>>() {
@Override
public void onFailure(Throwable caught) {
errorLabel.setText(caught.getMessage());
errorLabel.setVisible(true);
setLoading(false);
}
@Override
public void onSuccess(List<Tweet> result) {
resultsPanel.clear();
for (Tweet tweet : result) {
SafeHtmlBuilder builder = new SafeHtmlBuilder();
builder.appendHtmlConstant("<b>User: </b>");
builder.appendEscaped(tweet.getFrom_user());
builder.appendHtmlConstant("<br /><b>Created: </b>");
builder.appendEscaped(tweet.getCreated_at());
builder.appendHtmlConstant("<br /><b>Tweet: </b>");
builder.appendEscaped(tweet.getText());
builder.appendHtmlConstant("<br /><br />");
resultsPanel.add(new HTML(builder.toSafeHtml()));
}
setLoading(false);
}
});
}
});
privacyPolicyButton = new Button("Privacy Policy");
privacyPolicyButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
setLoading(true);
service.getPrivacyPolicy(new AsyncCallback<String>() {
@Override
public void onFailure(Throwable caught) {
errorLabel.setText(caught.getMessage());
errorLabel.setVisible(true);
setLoading(false);
}
@Override
public void onSuccess(String result) {
resultsPanel.clear();
// convert newlines to <br />
SafeHtmlBuilder builder = new SafeHtmlBuilder();
builder.appendEscapedLines(result);
resultsPanel.add(new HTML(builder.toSafeHtml()));
setLoading(false);
}
});
}
});
resultsPanel = new VerticalPanel();
errorLabel = new Label();
errorLabel.addStyleName("errorLabel");
errorLabel.setVisible(false);
//image from http://loadinfo.net/
loadingImage = new Image("loading.gif");
loadingImage.setVisible(false);
}
private void layoutWidgets() {
Panel panel = new VerticalPanel();
panel.add(errorLabel);
Panel horizPanel = new HorizontalPanel();
horizPanel.add(searchQueryTextBox);
horizPanel.add(searchButton);
horizPanel.add(privacyPolicyButton);
horizPanel.add(loadingImage);
panel.add(horizPanel);
panel.add(resultsPanel);
RootPanel.get().add(panel);
}
/**
* Updates the UI for when a RPC call is made.
* @param loading true if an RPC call is being sent, false if not
*/
private void setLoading(boolean loading) {
if (loading) {
errorLabel.setVisible(false);
}
searchQueryTextBox.setEnabled(!loading);
searchButton.setEnabled(!loading);
privacyPolicyButton.setEnabled(!loading);
loadingImage.setVisible(loading);
}
}
这是推文代码:
package com.google.gwt.twittersearch.client;
import java.io.Serializable;
@SuppressWarnings("serial")
public class Tweet implements Serializable{
private String id;
private String from_user;
private String created_at;
private String text;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFrom_user() {
return from_user;
}
public void setFrom_user(String from_user) {
this.from_user = from_user;
}
public String getCreated_at() {
return created_at;
}
public void setCreated_at(String created_at) {
this.created_at = created_at;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
任何对此错误的见解或我可能从这里去的地方将不胜感激!
谢谢,brl