已通过HttpClient获取到了HttpResponse,此时已关闭自动解压缩功能,需要进行手动解压br格式
时间: 2023-12-14 17:39:44 浏览: 21
可以使用第三方库Brotli来手动解压缩br格式。具体操作步骤如下:
1. 引入Brotli库
在项目的build.gradle文件中添加以下依赖:
```
implementation 'org.meteogroup.jbrotli:jbrotli:0.7'
```
2. 解压缩
通过Brotli库提供的BrotliInputStream类,将HttpResponse中的br格式数据解压缩成字节数组。
```
import org.meteogroup.jbrotli.*;
HttpResponse response = ...;
InputStream in = response.getEntity().getContent();
byte[] decompressedData = null;
try {
BrotliInputStream brotliInputStream = new BrotliInputStream(in);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int read;
while ((read = brotliInputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, read);
}
decompressedData = byteArrayOutputStream.toByteArray();
} catch (IOException e) {
e.printStackTrace();
} finally {
in.close();
}
// 使用解压缩后的数据进行后续操作
```
注意:需要确保HttpResponse中的Content-Encoding头部信息为"br",否则不需要进行解压缩操作。