我正在尝试编写 JAVA 代码以从谷歌获得答案。我编写的代码仅适用于英语,但当我尝试添加一些希伯来语字符时,我得到的答案与我在 Google 网站上编写时不同。
这是代码:
String address = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=";
String charset = "UTF-8";
URL url;
try
{
url = new URL(address + URLEncoder.encode(artistAndSong + site, charset));
//The url seems right, and it's the same as the url that appear on the web
Reader reader = new InputStreamReader(url.openStream(), charset);
GoogleResults results = new Gson().fromJson(reader, GoogleResults.class);
res =(results.getResponseData().getResults().get(0).getUrl());
//This is the part where I see that the answers are not the same
}
class GoogleResults{
private ResponseData responseData;
public ResponseData getResponseData() { return responseData; }
public void setResponseData(ResponseData responseData) { this.responseData = responseData; }
public String toString() { return "ResponseData[" + responseData + "]"; }
static class ResponseData {
private List<Result> results;
public List<Result> getResults() { return results; }
public void setResults(List<Result> results) { this.results = results; }
public String toString() { return "Results[" + results + "]"; }
}
static class Result {
private String url;
private String title;
public String getUrl() { return url; }
public String getTitle() { return title; }
public void setUrl(String url) { this.url = url; }
public void setTitle(String title) { this.title = title; }
public String toString() { return "Result[url:" + url +",title:" + title + "]"; }
}
}
有人有想法吗?我已经尝试解决了几天...(另外,我尝试使用 unicode,但似乎没有解决它..)
谢谢!