【问题标题】:Java speech API null responseJava 语音 API 空响应
【发布时间】:2014-11-29 20:27:42
【问题描述】:

我正在使用位于 https://github.com/lkuza2/java-speech-api 的 java 语音识别 API - Jarvis

但是,当我运行我的应用程序时,我收到一个错误:服务器返回 HTTP 响应代码:400 for URL:https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=en-US&maxresults=1(这是此 api 用于从 Google 获取响应的 URL)

我还创建了前面文章中提到的 API 密钥并尝试使用 URL(这是第 2 版 API):www.google.com/speech-api/v2/recognize?output=json&lang=en-US&key =MYKey"。但在这种情况下,我得到了 Google 的空响应。

谁能告诉我如何解决这个问题?

【问题讨论】:

  • 400 是一个错误的请求。确保您发送的内容对于 API 而言是正确的。
  • @Makoto 这是 API 使用的 URL。我还尝试使用 V2 URL (google.com/speech-api/v2/…)。我可以看到来自我的 Google Developer Console 的 Google Speech API 有 31 个请求。但我得到一个空响应。我的问题是“这个 API 真的有效吗?”
  • 顺便说一句,我正在使用link中列出的示例
  • 我从未使用过 jarvis,但我确实直接使用了谷歌语音 URL。 V1 几个月前已经停止工作,但如果您提供 API 密钥并正确解析结果,V2 的工作就像一个魅力。

标签: java api speech-recognition speech-to-text


【解决方案1】:

我改变了识别器类的一些东西:

我将 GOOGLE_RECOGNIZER_URL 常量更改为:

    private static final String GOOGLE_RECOGNIZER_URL = "https://www.google.com/speech-api/v2/recognize?output=json&lang=en-us&key=YOUR_KEY";

然后我改变了这个方法,因为响应数据有2行

   private String rawRequest(File inputFile, int maxResults, int sampleRate) throws IOException

第一行(读取和发送的那一行)为空(我真的不知道为什么),第二行有识别出的语音响应。为此你必须阅读第二行(不知道是否有更好的方法):

    String response = br.readLine();
    response = br.readLine();
    br.close();
    return response;

然后我改变这个方法,我认为它使用了v1 URL响应或其他东西,因为这个方法在json响应中寻找话语并且没有话语键。

    private void parseResponse(String rawResponse, GoogleResponse googleResponse)
    if (rawResponse == null)
        return;

    JSONObject jsonObject = new JSONObject(rawResponse);
    JSONArray jsonArray= (JSONArray) jsonObject.get("result");
    JSONArray jsonArrayResult = (JSONArray) jsonArray.getJSONObject(0).get("alternative");
    googleResponse.setResponse(jsonArrayResult.getJSONObject(0).get("transcript").toString());
    googleResponse.setConfidence(jsonArrayResult.getJSONObject(0).get("confidence").toString());

我是 json 库的新手,所以它可能是一种更好、更短的方法,但这对我有用!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多