Google 语言检测 api 回复错误代码 406
我正在尝试使用 Google 语言检测 API,现在我正在使用 Google 文档上提供的示例,如下所示:
public static String googleLangDetection(String str) throws IOException, JSONException{
String urlStr = "http://ajax.googleapis.com/ajax/services/language/detect?v=1.0&q=";
// String urlStr = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=Paris%20Hilton";
URL url = new URL(urlStr+str);
URLConnection connection = url.openConnection();
// connection.addRequestProperty("Referer","http://www.hpeprint.com");
String line;
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while((line = reader.readLine()) != null) {
builder.append(line);
}
JSONObject json = new JSONObject(builder.toString());
for (Iterator iterator = json.keys(); iterator.hasNext();) {
String type = (String) iterator.next();
System.out.println(type);
}
return json.getString("language");
}
但我收到 http 错误代码“406”。
我无法理解问题是什么?正如下面的谷歌搜索查询(评论)它工作正常。
当我在 Firefox 或 IE 中运行时,生成的语言检测 url 本身工作正常,但在我的 java 代码中失败。
我做错了什么吗?
预先感
谢阿什什
I am trying to use Google language detection API, Right now I am using the sample available on Google documentation as follows:
public static String googleLangDetection(String str) throws IOException, JSONException{
String urlStr = "http://ajax.googleapis.com/ajax/services/language/detect?v=1.0&q=";
// String urlStr = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=Paris%20Hilton";
URL url = new URL(urlStr+str);
URLConnection connection = url.openConnection();
// connection.addRequestProperty("Referer","http://www.hpeprint.com");
String line;
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while((line = reader.readLine()) != null) {
builder.append(line);
}
JSONObject json = new JSONObject(builder.toString());
for (Iterator iterator = json.keys(); iterator.hasNext();) {
String type = (String) iterator.next();
System.out.println(type);
}
return json.getString("language");
}
But I am getting http error code '406'.
I am unable to understand what the problem is? As the google search query(commented) below it is working fine.
The resultant language detection url itself is working fine when I run it in firefox or IE but it's failing in my java code.
Is there something I am doing wrong?
Thanks in advance
Ashish
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据猜测,
str
上传入的任何内容都包含在 URL 中无效的字符,因为错误代码406
为Not Acceptable
,当存在内容编码问题时看起来会返回。经过快速谷歌搜索后,您似乎需要通过 java.net.URLEncoder 类,然后将其附加到 URL。
As a guess, whatever is being passed in on
str
has characters that are invalid in a URL, as the error code406
isNot Acceptable
, and looks to be returned when there is a content encoding issue.After a quick google, it looks like you need to run your
str
through the java.net.URLEncoder class, then append it to the URL.在以下链接找到答案:
Java 中的 HTTP URL 地址编码
必须修改代码如下:
Found the answer at following link:
HTTP URL Address Encoding in Java
Had to modify the code as follows: