- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用 Ruby 1.9.3p194 运行 Rails 3.2.7,以从 SQLite 数据库输出 JSON 数据。
render :json => result.to_json
Android 应用程序使用通过 HTTP-GET
加载的 JSON 文件:
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(urlPath);
HttpResponse response = httpclient.execute(httpGet);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity, HTTP.UTF_8);
JSONObject jsonObject = new JSONObject(result);
我在 API 中读到 Rails offers gzip support如下:
ActiveSupport::Gzip.compress(result)
我也是从HTTP/1.1, RFC2626, section 14.3猜的我可以配置 HTTP 请求的 header :
httpPost.setHeader("Accept-Encoding", "gzip");
我还发现 section 3.5 "Content Codings" 中包含的信息非常有趣:
- All content-coding values are case-insensitive.
- HTTP/1.1 uses content-coding values in the Accept-Encoding (section 14.3) and Content-Encoding (section 14.11) header fields.
- The Internet Assigned Numbers Authority (IANA) acts as a registry for content-coding value tokens. Initially, the registry contains the following tokens:
- gzip An encoding format produced by the file compression program "gzip" (GNU zip) as described in RFC 1952 [25].
这篇文章进一步解释了how to handle GZIP encoded content with Android .
因此,我不知道如何才能知道数据是否已被服务器压缩。为了测试 Rails 是否输出 gzip,我尝试按照建议使用 curl here :
$ curl --head -H "Accept-Encoding: gzip" http://localhost:3000/posts.json
然而,输出并没有告诉我是否支持 gzip:
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
X-Ua-Compatible: IE=Edge
Etag: "f6f6732c747466f75052f88b1eff393b"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 74ee0562c05adea679deb701f1b8fd88
X-Runtime: 0.004205
Content-Length: 0
Server: WEBrick/1.3.1 (Ruby/1.9.3/2012-04-20)
Date: Thu, 16 Aug 2012 23:33:25 GMT
Connection: Keep-Alive
我还尝试了 curl 的 --compressed
参数...
$ curl --compressed --head -H "Accept-Encoding: gzip" http://localhost:3000/posts.json
输出与上一条命令相同的头信息。当我跑...
$ curl --compressed -H "Accept-Encoding: gzip" http://localhost:3000/posts.json
JSON 数据作为可读文本打印到控制台。我看不到是否发生了压缩。也许是因为 curl 已经解压缩了响应?!
我也试过在线测试HTTP Compression Test提到here .它确认 JSON 内容“未压缩”。第二网站GIDZipTest链接here承认阴性检测结果。
据我所知,我必须为 REST 服务器配置的是使用 Rack::Deflater
。明确一点:我没有在我的代码中使用 ActiveSupport::Gzip.compress()
。如果有人感兴趣,这就是启用 GZIP 压缩时 header 的样子。
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
X-Ua-Compatible: IE=Edge
Etag: "8170a04be41673bf25824256740a9460"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 700b9536f6a20164d31b8528bde423af
X-Runtime: 0.369337
Vary: Accept-Encoding
Content-Encoding: gzip
Server: WEBrick/1.3.1 (Ruby/1.9.3/2012-04-20)
Date: Tue, 21 Aug 2012 12:10:48 GMT
Content-Length: 20
Connection: Keep-Alive
现在我知道了魔法关键字,很容易search and find articles on use Rack::Deflater
.
最佳答案
也许你应该使用 Rack::Deflater?在 config.ru 中插入:
require ::File.expand_path('../config/environment', __FILE__)
use Rack::Deflater
run MyAppName::Application
并配置您的 http 请求 header :
I can configure the header of a HTTP request:
httpPost.setHeader("Accept-Encoding", "gzip");
附言相同的配置适用于 Heroku
关于android - 如何在 Rails for Android 中使用 gzip 压缩 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11997110/
我正在尝试创建有效的 gzip 文件(可以使用标准 Linux gzip 解压缩),其中的数据使用 DEFLATE 算法和静态/预设字典编码。 我已阅读 DEFLATE 的两个规范和 gzip ,而且
我刚刚搜索了gzip和 Deflate ,并发现 Deflate 更好。 GZip or Deflate for HTTP compression Why use deflate instead of
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度的了解。包括尝试的解决方案、为什么它们不起作用以及预期结果
我一直在寻找 Gzip 是否支持多部分文件压缩。 From what I have seen so far it does not, but how come 7z allows multipart
我可以连接两个已经 gzip 的文件(使用 gzip)然后用枪压缩它们吗? 截至今天,我从远程服务器下载 gzip 压缩文件,单独对其进行 gunzip,然后将它们合并。 希望通过合并 gzipped
我正在创建两个 gzip 文件,一个只包含一个 gzip 成员,而第二个包含 2 个 gzip 成员(两个文件连接成一个 gzip 文件)。 当我尝试通过网络服务器下载此文件时,chrome 会很好地
当我对文件夹进行去皮重和 gzip 压缩时,每次我都会得到不同的文件大小。 目录内容不变,没有变化。压缩后的文件大小变化 20 到 100 字节。这是正常行为吗? 我的数据会受到影响吗? 谢谢 最佳答
比方说 file.txt.gz有 2GB,我想看到最后 100 行左右。 zcat
我一直听说对您的网站进行 gzip 压缩是加快交付速度的良好做法。我的网站总体负载非常大,我还应该研究 gzip 吗?我还了解了使用 gzip 的缺点,例如解压缩内容以便浏览器显示所需的时间。这是真的
下载 jQuery 的生产副本时,链接旁边显示该文件为 32K Minified & Gzipped。我得到了 Minified,但是 Gzipped 是什么意思? 它是否被网络服务器压缩,如 Apa
我的网站很高兴根据以下内容进行 Gzip 压缩: http://www.gidnetwork.com/tools/gzip-test.php 但是,当我通过 Yslow 运行它时,我得到了 Gzip
我使用命令 tar 一组文件:tar -czvf file.tar.gz file/ 然后复制到 USB(ext4 格式),我检查了我可以解压它。重装系统后,挂载usb时发生了一些错误,我执行fsck
我有一个提供小部件的网络服务。为了可扩展性,我想在 Amazon S3 上保留 js 文件的 gzip 版本。问题是不能接受 gzip 文件的浏览器将不会被提供。 任何人都知道我在哪里可以找到统计数据
我正在使用 Chrome 和 Firefox 下的 Yslow 工具查看我的开发站点,其中一项建议是我对适当的内容进行 gzip。作为起点,我刚刚在我的 [/] 配置中添加了“tools.gzip.o
我正在开发一个网站,我正在使用 gzip.exe 来预压缩 css 和 js 文件(只有 1 个 css 文件从 4.53 KB 到 1.50 KB,还有一个 js 文件包含 jquery 和一些来自
我们必须使用什么类型的响应监听器来处理 Android Volley 的 gzip 响应? 如果使用 String 监听器,则响应似乎会丢失其编码。 你如何使用 Volley 处理 gzip 响应?
我用 Fiddler调试我的应用程序。每当响应被服务器压缩,而不是解压缩响应时,Fiddler 显示不可读的二进制数据: /* Response to my request (POST) */ HTT
我通常使用tar zcvf压缩并使用tar zxvf解压缩(由于习惯使用gzip)。 我最近购买了一个具有超线程功能的四核 CPU,因此我有 8 个逻辑核心,并且我注意到许多核心在压缩/解压缩期间未使
我正在使用 Google Cloud Storage 控制台上传文件。我没有使用任何命令行工具。 我想在元数据中将 Content-Encoding 设置为 gzip(-z 选项)。 请看下面的截图,
我正在将mysqldump的结果用管道传输到gzip,gzip的速度似乎大大落后 gzip: 34.9MiB 0:01:54 [ 218kiB/s] mysqldump: 735MiB 0:01:5
我是一名优秀的程序员,十分优秀!