- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在我们的 Android 应用程序中,我正在向我们的 (NGINX) 服务器发送相当大的文件,因此我希望对我的 Retrofit POST 消息使用 gzip。
有许多关于 OkHttp 透明地使用 gzip 或更改 header 以接受 gzip(即在 GET 消息中)的文档。
但是我如何启用此功能以从我的设备发送 gzip http POST 消息?我是否必须编写自定义拦截器或其他东西?或者只是在标题中添加一些东西?
最佳答案
根据以下recipe :gzip 的正确流程应该是这样的:
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new GzipRequestInterceptor())
.build();
/** This interceptor compresses the HTTP request body. Many webservers can't handle this! */
static class GzipRequestInterceptor implements Interceptor {
@Override public Response intercept(Chain chain) throws IOException {
Request originalRequest = chain.request();
if (originalRequest.body() == null || originalRequest.header("Content-Encoding") != null) {
return chain.proceed(originalRequest);
}
Request compressedRequest = originalRequest.newBuilder()
.header("Content-Encoding", "gzip")
.method(originalRequest.method(), gzip(originalRequest.body()))
.build();
return chain.proceed(compressedRequest);
}
private RequestBody gzip(final RequestBody body) {
return new RequestBody() {
@Override public MediaType contentType() {
return body.contentType();
}
@Override public long contentLength() {
return -1; // We don't know the compressed length in advance!
}
@Override public void writeTo(BufferedSink sink) throws IOException {
BufferedSink gzipSink = Okio.buffer(new GzipSink(sink));
body.writeTo(gzipSink);
gzipSink.close();
}
};
}
}
关于安卓-OKHttp : how to enable gzip for POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47637456/
感觉代码越少越好,所以想“优化”如何同时启用多个项目: button1.enabled = YES; button2.enabled = YES; textField.enabled = YES; .
我正在尝试禁用和禁用存储在 List 中的 Component。当我尝试这样做时,出现以下错误: 'Component' does not contain a definition for 'enab
我正在努力使用执行器启用 Spring Boot 运行状况检查。由于我们的应用程序依赖于 Spring 1.5.21,因此我必须使用执行器 1.5.21 版本。我在他们的online document
我有一个项目结构如下:.Persistence -> .Repo -> .Services -> .Controllers -> MVC3 App。 每一层都有一个带有接口(interface)的相应
解决方法: 在cmakelists.txt中添加设置: set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fms-extensions") 问题解决 下面是cmakel
我对 powershell 知之甚少。 我想知道是否有人可以指出我需要了解什么来解释以下内容: PS C:\Users\username> (Get-Aduser -Filter 'Enabled -
我有一个运行 Ubuntu 16.04.1x64 的 DO droplet,我正在尝试运行 IPFS作为系统服务。我已经按照说明创建了一个用户“connor”并安装了 IPFS here .我将服务存
我尝试制作 Android WebRTC 应用 我在 Android Oreo 和 Pie 上测试过,该应用运行良好。 但是在 Android Lollipop 和 Marshmallow 上,当我调
我有一个简单的网站,其中一个 aspx 页面 ( Test.aspx ) 显示 Roles.Enabled在 Web.config 中设置为 false 的值(属性 roleManager@enabl
在 Spring Boot 中,对于分段上传,我看到许多教程站点都建议具有以下属性之一:spring.http.multipart.enabled=false或者spring.servlet.mult
我有一个 PHP 库,它使用许多正则表达式,其中包括用于多字节字符串的 \P 表达式,例如 ((((?:\P{M}\p{M}*)+?)|(\'[^\']*\')|(\"[^\"]*\"))!)?\$?
如果“错误运行应用程序:Instant Run 需要启用'工具 | Android | 启用 ADB 集成'。” 使用最新的 Gradle 插件和最新的 Android SDK 时出现问题。您可以使用
在尝试了一些选项后,我仍然没有得到满意的结果: 使用的Javascript库是FullpageJS ( http://alvarotrigo.com/fullPage/ ) Fullpage.js 使
默认情况下,Kafka Consumer 会定期提交当前偏移量,除非通过禁用 enable.auto.commit 将其关闭。 .根据文档,您将负责自己提交偏移量。所以当我想要 手册 控制,这似乎是要
我有一个 AWS SAM 应用程序,其函数包含 schedule 事件,并且我希望有条件地启用/禁用根据参数值为事件生成的 EventBridge 规则。 为此,我在 template.yaml 中执
似乎 servlet 的 web.xml 有一个元素叫做 false 可以像这样放在servelt定义中 example com.example.TestServlet 10
我们有 Aerospike 服务器版本 3.8.3(支持 LDT) 我们遇到以下异常 - 2018-08-01 16:11:31,558 1320573 [task-scheduler-3] ERRO
我有一个运行 Windows 10 Pro 的 Windows VM,我正在尝试安装运行 WSL2 的 Docker。 但是,我遇到了下面的错误,我可以确认 虚拟机平台 Windows 功能 已经启用
我开始学习 Javafx,并且偶然发现了这个“奇怪”的功能。我制作了一个带有两个按钮的简单窗口。当我按下其中一个按钮时,会出现一条蓝色笔画并一直保留在那里,直到我按下另一个按钮。除了知道最后按下的是哪
我在使用 C++ 模板时遇到了这段使用 SFINAE 使用 std::enable_if 的代码.这段代码我面临两个问题。 #include #include enum class Type :
我是一名优秀的程序员,十分优秀!