我正在尝试使用 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 代码中却失败了。
有什么我做错了吗?
提前致谢
阿什什