如何COM preSS JSON用gzip Rails中为Android? [英] How to compress JSON with gzip in Rails for Android?
问题描述
我从SQLite数据库上运行的Rails 3.2.7和Ruby 1.9.3p194输出JSON数据。
I am running Rails 3.2.7 with Ruby 1.9.3p194 to output JSON data from an SQLite database.
render :json => result.to_json
这是Android的应用程序使用它获取通过 HTTP-GET
加载的JSON文件:
An Android application consumes the JSON file which gets loaded via HTTP-GET
:
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);
平台支持:
我读到的Rails API中提供的gzip支持如下:
ActiveSupport::Gzip.compress(result)
我也想从 HTTP / 1.1,RFC2626,第14.3 的我可以配置HTTP请求头:
I also guess from HTTP/1.1, RFC2626, section 14.3 that I can configure the header of a HTTP request:
httpPost.setHeader("Accept-Encoding", "gzip");
我还发现,包含在 3.5节内容值编码的信息很有趣:
- 所有内容编码值的区分大小写
- 在HTTP / 1.1使用在接受编码(第14.3)和内容编码(第14.11)头字段内容编码值。
- 在互联网编号分配机构(IANA)充当注册表内容编码值符号。最初,注册表包含的
以下标记:
- GZIP 作为在RFC 1952年[25]描述的文件COM pression计划的gzip(GNU的zip)产生的编码格式。
- 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].
这篇文章进一步解释如何处理GZIP EN $ C $镉含量与Android 。
因此,我不知道,我怎么能找到,如果数据已经融为一体$ P $服务器pssed。 为了测试,无论是Rails的输出GZIP,我试图用的卷曲的的建议这里:
Thus, I have no clue, how I can find out, if the data has been compressed by the server. To test, whether Rails outputs gzip, I tried to use curl as suggested here:
$ curl --head -H "Accept-Encoding: gzip" http://localhost:3000/posts.json
输出,但是,不会透露给我,GZIP是否支持或不:
The output, however, does not reveal to me, whether gzip is supported or not:
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
我也试过
- 中的参数=http://curl.haxx.se/docs/manpage.html >的卷曲的 ...
I also tried the
--compressed
parameter of curl ...$ curl --compressed --head -H "Accept-Encoding: gzip" http://localhost:3000/posts.json
它输出相同的头信息作为previous命令。当我运行...
which outputs the same header information as the previous command. When I run ...
$ curl --compressed -H "Accept-Encoding: gzip" http://localhost:3000/posts.json
JSON数据被打印到控制台为可读文本。我看不到,如果COM pression发生。也许,因为卷曲的已经DECOM presses响应?!
the JSON data is printed to the console as readable text. I cannot see if compression has happened. Maybe, because curl already decompresses the response?!
我也尝试了在线测试 HTTP的COM pression测试的提到的<一个HREF =http://webmasters.stackexchange.com/a/543>此处。据证实,JSON内容是不是gzip压缩。第二个网站 GIDZipTest 联的此处承认的负面测试结果。
I also tried the online test HTTP Compression Test mentioned here. It confirmed that the JSON content "is not gzipped". The second website GIDZipTest linked here acknowledged the negative test result.
- 如何输出的gzip COM pressed JSON从Rails的?
- 如何配置HTTP客户端请求的gzip COM pressed数据?
- 是否相同的配置仍然可以工作,当我在Heroku(PostgreSQL系统)运行Rails服务器,...?
当我学会了所有我必须配置为REST服务器
使用机架:: Deflater
。需要明确的是:我不使用的ActiveSupport :: Gzip.com preSS()
在我的code。如果有人有兴趣,这是头看起来当GZIP COM pression启用等。
Lessons learned
As I learned all I have to configure for the REST server is
use Rack::Deflater
. To be clear: I do not useActiveSupport::Gzip.compress()
in my code. In case someone is interested, this is what the header looks like when GZIP compression is enabled.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
现在我知道了神奇的关键字很容易搜索并找到
使用机架:: Deflater $ C篇$ C>
。Now that I know the magic keyword it is easy to search and find articles on
use Rack::Deflater
.推荐答案
也许你应该使用机架:: Deflater? 插入config.ru:
maybe you should use Rack::Deflater? insert in config.ru:
require ::File.expand_path('../config/environment', __FILE__) use Rack::Deflater run MyAppName::Application
和配置HTTP请求头:
and configure your http request headers:
我可以配置HTTP请求头:
I can configure the header of a HTTP request:
httpPost.setHeader(接受编码,GZIP);
httpPost.setHeader("Accept-Encoding", "gzip");
P.S。相同的配置工作在Heroku
P.S. the same configuration works on Heroku
这篇关于如何COM preSS JSON用gzip Rails中为Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!