gpt4 book ai didi

android - 如何使用 Retrofit 解决 IllegalArgumentException "Malformed content type: ......"

转载 作者:行者123 更新时间:2023-11-29 01:05:06 24 4
gpt4 key购买 nike

我已经测试了speech recognition API by Microsoft用于从短音频文件中获取字符串。此 API 不需要格式良好的媒体类型(这真的不是格式良好的吗?仅适用于 Retrofit 或 Okhttp?)。

Content-Type: audio/wav; codec=audio/pcm; samplerate=16000

所以我将它设置为 Retrofit 界面,如下所示。

 @Multipart
@POST("/speech/recognition/{recognitionMode}/cognitiveservices/v1")
Observable<Recognition> getRecgnition(
@Header("Ocp-Apim-Subscription-Key") String subscriptionKey,
@Header("Content-Type") String contentType,
@Path("recognitionMode") String recognitionMode,
@Query("language") String language,
@Query("format") String format,
@Part("file")RequestBody file
);

然后我在发送带有此 header 的音频文件的请求中遇到了错误。我知道原因是因为 MediaType#parse 将返回 null 因为 Matcher#lookingAt 将返回 false .

好的,我明白了。但我不知道如何忽略这个异常。你能给我一些建议来避免这个错误吗?

java.lang.IllegalArgumentException: Malformed content type: audio/wav;codec=audio/pcm; samplerate=16000
at retrofit2.RequestBuilder.addHeader(RequestBuilder.java:81)
at retrofit2.ParameterHandler$Header.apply(ParameterHandler.java:79)
at retrofit2.ServiceMethod.toRequest(ServiceMethod.java:111)
at retrofit2.OkHttpCall.createRawCall(OkHttpCall.java:184)
at retrofit2.OkHttpCall.execute(OkHttpCall.java:168)
at retrofit2.adapter.rxjava.CallExecuteOnSubscribe.call(CallExecuteOnSubscribe.java:40)
at retrofit2.adapter.rxjava.CallExecuteOnSubscribe.call(CallExecuteOnSubscribe.java:24)
at retrofit2.adapter.rxjava.BodyOnSubscribe.call(BodyOnSubscribe.java:36)
at retrofit2.adapter.rxjava.BodyOnSubscribe.call(BodyOnSubscribe.java:28)
at rx.Observable.unsafeSubscribe(Observable.java:10256)
at rx.internal.operators.OperatorSubscribeOn$SubscribeOnSubscriber.call(OperatorSubscribeOn.java:100)
at rx.internal.schedulers.CachedThreadScheduler$EventLoopWorker$1.call(CachedThreadScheduler.java:230)
at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:272)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:762)

最佳答案

我刚刚找到了这个问题的解决方案。刚刚使用 Okhttp 拦截器添加了 header 。

private class SpeechRecognitionIntercepter implements Interceptor {

@Override
public Response intercept(Chain chain) throws IOException {
Headers.Builder builder = new Headers.Builder();
builder.add("Ocp-Apim-Subscription-Key", "here-is-your-own-subscription-key");
builder.add("Accept", "application/json");
builder.add("Content-Type", "audio/wav; codec=audio/pcm; samplerate=16000");
return chain.proceed(
chain.request().newBuilder()
.headers(builder.build())
.build()
);
}
}

然后添加这个拦截器。

new OkHttpClient.Builder().addInterceptor(new SpeechRecognitionIntercepter())

关于android - 如何使用 Retrofit 解决 IllegalArgumentException "Malformed content type: ......",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47824404/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com