Java的语音API空响应 [英] Java speech API null response

查看:274
本文介绍了Java的语音API空响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是java的语音识别API - 贾维斯位于 https://github.com/ lkuza2 / Java的语音API

I am using the java speech recognition API - Jarvis located at https://github.com/lkuza2/java-speech-api

然而,当我跑我的应用程序,我得到一个错误:服务器返回的HTTP响应code:400网址:<一href=\"https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=en-US&maxresults=1\" rel=\"nofollow\">https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=en-US&maxresults=1 (这是这个API使用摆脱谷歌回应URL)

However when I run my application, I get an error : Server returned HTTP response code: 400 for URL: https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=en-US&maxresults=1 (This is the URL that this api uses to get response from Google)

我还创建了一个API密钥,如较早的帖子中提到,并试图使用url(这是第2版API):www.google.com/speech-api/v2/recognize?output=json&lang=en -US和放大器;关键=的myKey但在这种情况下,我从谷歌null响应

I also created a API key as mentioned in the earlier posts and tried to use the url (this is version 2 API): www.google.com/speech-api/v2/recognize?output=json&lang=en-US&key=MYKey". But in this case I get a null response from Google.

谁能告诉我怎么解决这个问题?

Can anybody please tell me how to get around this?

推荐答案

我改变从Recognizer类一些事情:

I change some things from the Recognizer class:

我改变GOOGLE_RECOGNIZER_URL常量:

I change the GOOGLE_RECOGNIZER_URL constant to:

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

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

Then I changed this method because the response data have 2 lines

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

的第一行(即读取和发送的一个)为空(ⅰdon¡t真正知道为什么)和第二线具有识别出的语音的响应。为此,你必须阅读第二行(不知道是否有更好的方式):

The first line (the one that is read and sent) is null (i don¡t really know why) and the second line has the response of the speech recognized. For this you must read the second line (don't know if there is a nicer way):

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

然后我改变这个方法,我认为它用的是V1 URL响应或东西,因为这种方法会在JSON响应话语再没有发话键。

Then I change this method, I think it was using the v1 URL response or something because this method looks for utterance in the json response and there is none utterance key.

    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库所以它可能是一个更好的和更短的方式,但这个工作对我来说!

I'm new with the json library so it might be a better and shorter way but this worked for me!

这篇关于Java的语音API空响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆