Google 语言检测 api 回复错误代码 406

发布于 2024-09-24 18:56:12 字数 1298 浏览 8 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

此生挚爱伱 2024-10-01 18:56:12

据猜测,str 上传入的任何内容都包含在 URL 中无效的字符,因为错误代码 406Not 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 code 406 is Not 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.

孤寂小茶 2024-10-01 18:56:12

在以下链接找到答案:

Java 中的 HTTP URL 地址编码

必须修改代码如下:

URI uri = new URI("http","ajax.googleapis.com","/ajax/services/language/detect","v=1.0&q="+str,null);       
URL url = uri.toURL();  

Found the answer at following link:

HTTP URL Address Encoding in Java

Had to modify the code as follows:

URI uri = new URI("http","ajax.googleapis.com","/ajax/services/language/detect","v=1.0&q="+str,null);       
URL url = uri.toURL();  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文