使用okhttp上传.pcm文件

时间:2017-09-07 14:57:27

标签: audio okhttp3 nuance

我正在尝试使用Nuance将语音转换为文本,因此我尝试发送此请求

curl "https://dictation.nuancemobility.net:443/NMDPAsrCmdServlet/dictation?appId=[INSERT YOUR APP ID]&appKey=[INSERT YOUR 128-BYTE STRING APP KEY]&id=C4461956B60B" -H "Content-Type: audio/x-wav;codec=pcm;bit=16;rate=16000" -H "Accept-Language: ENUS" -H "Transfer-Encoding: chunked" -H "Accept: application/xml" -H "Accept-Topic: Dictation" -k --data-binary @audio_16k16bit.pcm  

需要上传音频文件(.pcm)格式。

我正在使用okhttp3库以下是构建器

RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("file", "audio_16k16bit.pcm", RequestBody.create(MEDIA_TYPE_PNG, "audio_16k16bit"))
                .build();


    httpBuider.addQueryParameter("appId", "NMDPTRIAL_XXXXXXX_XXX_com20161122071457").addQueryParameter("appKey", "fadaed7b801e10d84272c0a75317d8cee13ab86ae902ab322cd6e1219fcbe79aa5d41526f225fe3497bfdbead6b4b9b7ee7122d773cd0a9fa3ebc042b7a7dc5c");
    Request request = new Request.Builder().addHeader("Content-Type","audio/x-wav;codec=pcm;bit=16;rate=16000").addHeader("Accept-Language","eng-GBR").addHeader("Transfer-Encoding","chunked").addHeader("Accept","application/xml").addHeader("Accept-Topic","Dictation").post(requestBody).url(httpBuider.build()).build();

我正在关注日志

  

HTTP错误500

                                                              

访问/ NMDPAsrCmdServlet / dictation时出现问题。原因:                                                             

    Server Error

遗漏了什么?

2 个答案:

答案 0 :(得分:0)

我刚刚使用泽西岛经历过同样的经历。问题是,泽西岛覆盖了内容类型标题(我尝试使用Application_Octet_Stream)。

以下是我最终使用它的请求:

Response myResponse = target.
request().
accept(MediaType.TEXT_PLAIN_TYPE).
header("Accept-Language","DEDE").
header("Accept-Topic", "Dictation").
header("Transfer-Encoding","chunked").
post(Entity.entity(speechStream, "audio/x-wav;codec=pcm;bit=16;rate=16000"));

我建议使用像fiddler这样的东西找出真正发布的内容并将其与curl post进行比较。这就是我最终发现我的请求出了什么问题的方法。

答案 1 :(得分:0)

删除标题"Transfer-Encoding","chunked"。我的错误已通过删除予以解决。