线程" main"中的例外情况com.google.api.gax.rpc.DeadlineExceededException:io.grpc.StatusRuntimeException:DEADLINE_EXCEEDED

时间:2018-05-15 13:18:24

标签: cloud speech-to-text google-cloud-speech

我在公司代理服务器上连接谷歌云语音到文本API时遇到此问题:

  

io.grpc.internal.ManagedChannelImpl $ NameResolverListenerImpl onError   警告:[io.grpc.internal.ManagedChannelImpl-1]无法解决   名称。 status =状态{code = UNAVAILABLE,description = null,   cause = java.net.UnknownHostException:这通常是一个临时错误   在主机名解析期间,意味着本地服务器没有   收到权威服务器的回复   (speech.googleapis.com)。

这是我的代码

/*try {
    Credentials creds = GoogleCredentials.getApplicationDefault();
    System.out.println(" "+creds);
} catch (IOException e2) {
    e2.printStackTrace();
}*/
System.setProperty("http.proxyHost", "X.X.X.X");
System.setProperty("http.proxyPort", "3128");
System.setProperty("https.proxyHost", "x.x.x.x");
System.setProperty("https.proxyPort", "3128");

/*String encoded = new String(Base64.encodeBase64(new String("username:password").getBytes()));
String base64encodedCredentials = "Basic " + encoded;*/

SpeechClient speech = null;
try {
    speech = SpeechClient.create();
} catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
//speech.getRequestFactory().setPrivateHeader("Proxy-Authorization", base64encodedCredentials);

// The path to the audio file to transcribe
String fileName = "D:\\RecordAudio1.flac";//D:\\Voice\\RecordAudio.wav
System.out.println("path "+fileName);

// Reads the audio file into memory
Path path = Paths.get(fileName);
byte[] data = null;
try {
    data = Files.readAllBytes(path);
} catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
ByteString audioBytes = ByteString.copyFrom(data);

// Builds the sync recognize request
RecognitionConfig config = RecognitionConfig.newBuilder()
    .setEncoding(AudioEncoding.FLAC)
    .setSampleRateHertz(16000)
    .setLanguageCode("en-US")
    .build();
RecognitionAudio audio = RecognitionAudio.newBuilder()
    .setContent(audioBytes)
    //.setUri(("gs://cloud-samples-tests/speech/brooklyn.flac"))
    .build();

// Performs speech recognition on the audio file
RecognizeResponse response = speech.recognize(config, audio);
List<SpeechRecognitionResult> results = response.getResultsList();
System.out.println("came to line 69 "+results.size());
try
{
  for (SpeechRecognitionResult result: results) {
    // There can be several alternative transcripts for a given chunk of speech. Just use the
    // first (most likely) one here.
    SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
    System.out.printf("Transcription: %s%n", alternative.getTranscript());
  }
    speech.close();
} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

我尝试在系统变量中设置GRPC_PROXY_EXP="http.proxyHost:port"环境变量,但它仍然会犯同样的错误。

请帮助..

1 个答案:

答案 0 :(得分:0)

您使用Speech API遇到此错误,因为该API试图解决该问题,这意味着本地服务器未收到来自授权服务器(speech.googleapis.com)的响应。 因此,我想说这个问题与客户本地DNS配置有关。