Java speech API null response

痴心易碎 提交于 2019-12-11 06:53:07

问题


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

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)

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?


回答1:


I change some things from the Recognizer class:

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";

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

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

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;

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());

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



来源:https://stackoverflow.com/questions/27206749/java-speech-api-null-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!