如何使用Retrofit解决IllegalArgumentException“格式错误的内容类型:......”

时间:2017-12-15 01:05:43

标签: android post header retrofit2 content-type

我已经过测试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
    );

然后我在发送带有此标头的音频文件的请求中遇到错误。我知道原因是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)

1 个答案:

答案 0 :(得分:1)

我刚刚找到了解决这个问题的方法。刚刚添加了Okhttp截取的标题。

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