当前位置: 代码迷 >> 综合 >> Retrofit2 上传音频文件(binary二进制数据)
  详细解决方案

Retrofit2 上传音频文件(binary二进制数据)

热度:42   发布时间:2024-02-07 19:57:55.0

Retrofit2 上传音频文件(binary二进制数据)

  • 怎么使用Retrofit2(binary二进制数据) 上传音频文件
  • 如果使用需要添加Header
  • gzip出现的问题
  • 关闭gzip

怎么使用Retrofit2(binary二进制数据) 上传音频文件

使用二进制数据上传音频文件直接使用 RequestBody body,
数据Bean自己设置

@POST("/file")
Observable<Bean> asr(@Body RequestBody body);

主要使用
RequestBody.create(MediaType.parse(“wav”), file)

public Observable asr(File file) {Retrofit retrofit = new Retrofit.Builder().client(mOkHttpClient).baseUrl(ServerUrlConfig.HTTP_VOICE_URL).addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build();ServiceApi apiService = retrofit.create(ServiceApi.class);return apiService.asr(RequestBody.create(MediaType.parse("wav"), file));}

调用

Observable observable = VoiceApiFactory.getInstance(mContext).asr(file);observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new MySubscriber() {@Overridepublic void onCompleted() {super.onCompleted();}@Overridepublic void onError(Throwable e) {super.onError(e);}@Overridepublic void onNext(Object object) {Bean bean = (Bean) object;super.onNext(object);}});

如果使用需要添加Header

@POST("/file")
Observable<Bean> asr(@Header("Content-Type") String header,@Header("Content-Type2") String header2,@Body RequestBody body);

Header参数 @Header(“Content-Type”) String header
多个Header参数 @Header(“Content-Type”) String header,@Header(“Content-Type2”) String header2

public Observable asr(File file) {Retrofit retrofit = new Retrofit.Builder().client(mOkHttpClient).baseUrl(ServerUrlConfig.HTTP_VOICE_URL).addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build();ServiceApi apiService = retrofit.create(ServiceApi.class);return apiService.asr(RequestBody.create("audio/x-raw;rate=16000",MediaType.parse("wav"), file));}

Header单个参数加多组数据audio/x-raw;rate=16000
注意使用 ;分割

Observable observable = VoiceApiFactory.getInstance(mContext).asr(file);observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new MySubscriber() {@Overridepublic void onCompleted() {super.onCompleted();}@Overridepublic void onError(Throwable e) {super.onError(e);}@Overridepublic void onNext(Object object) {Bean bean = (Bean) object;super.onNext(object);}});

gzip出现的问题

一般情况下使用该方法可以正常上传数据。
在自己项目中使用该方法调用后,后台曾一直无法获取数据。在测试使用postman接口调用正常。
使用Android studio进行抓包
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
抓包后,发现一般http都自动开启gzip,而后台确认后没有开启gzip
需要把gzip关闭

关闭gzip

关闭gzip,需要关闭OkHttpClient中默认添加的gzip Header
如果使用网络拦截器

 private void initOkHttpClient(Context context) {Cache cache = new Cache(new File(FileUtil.getCacheDir(context), "HttpCache"), MAXSIZE);HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);mOkHttpClient = new OkHttpClient.Builder().cache(cache).retryOnConnectionFailure(true).addInterceptor(interceptor).addNetworkInterceptor(cacheInterceptor).connectTimeout(TIMEOUT, TimeUnit.SECONDS).readTimeout(TIMEOUT, TimeUnit.SECONDS).writeTimeout(TIMEOUT, TimeUnit.SECONDS).build();}

修改cacheInterceptor
使用removeHeader(“Content-Encoding”)关闭gzip

public Interceptor cacheInterceptor = new Interceptor() {@Overridepublic Response intercept(Chain chain) throws IOException {Request request = chain.request();if (!NetworkUtil.isNetworkConnected()) {request = request.newBuilder().cacheControl(CacheControl.FORCE_CACHE).header("Content-Type", "application/json").build();} else if (NetworkUtil.isNetworkConnected()) {request = request.newBuilder().cacheControl(CacheControl.FORCE_NETWORK).header("Content-Type", "application/json").removeHeader("Content-Encoding").build();}Logcat.d(TAG, "intercept: url:  " + request.url());Response response = chain.proceed(request);if (NetworkUtil.isNetworkConnected()) {response = response.newBuilder().header("Cache-Control", "public, " + CACHE_CONTROL_CACHE).header("Content-Type", "application/json").removeHeader("Pragma").build();}Logcat.d("httplog--> ", response.toString());return response;}};
  相关解决方案