如何在Rails for Android中使用gzip压缩JSON?

JJD*_*JJD 7 android json gzip httpclient ruby-on-rails-3

我正在使用Ruby 1.9.3p194运行Rails 3.2.7,以从SQLite数据库输出JSON数据.

render :json => result.to_json
Run Code Online (Sandbox Code Playgroud)

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);
Run Code Online (Sandbox Code Playgroud)

平台支持:

我在API中读到Rails提供的gzip支持如下:

ActiveSupport::Gzip.compress(result)
Run Code Online (Sandbox Code Playgroud)

我还从HTTP/1.1,RFC2626,第14.3节中猜测我可以配置HTTP请求的标头:

httpPost.setHeader("Accept-Encoding", "gzip");
Run Code Online (Sandbox Code Playgroud)

我还发现第3.5节"内容编码"中包含的信息非常有趣:

  • 所有内容编码值都不区分大小写.
  • HTTP/1.1使用Accept-Encoding(第14.3节)和Content-Encoding(第14.11节)头字段中的内容编码值.
  • 互联网号码分配机构(IANA)充当内容编码价值代币的注册机构.最初,注册表包含以下标记:
    • gzip由RFC 1952 [25]中描述的文件压缩程序"gzip"(GNU zip)生成的编码格式.

这篇文章进一步解释了如何使用Android处理GZIP编码的内容.

服务器测试:

因此,我不知道如果数据已被服务器压缩,我怎么知道.为了测试,无论是Rails的gzip的输出,我试图用卷曲的建议在这里:

$ curl --head -H "Accept-Encoding: gzip" http://localhost:3000/posts.json
Run Code Online (Sandbox Code Playgroud)

但是,输出并没有告诉我,是否支持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
Run Code Online (Sandbox Code Playgroud)

我也试过curl--compressed参数......

$ curl --compressed --head -H "Accept-Encoding: gzip" http://localhost:3000/posts.json
Run Code Online (Sandbox Code Playgroud)

它输出与前一个命令相同的标题信息.当我跑...

$ curl --compressed -H "Accept-Encoding: gzip" http://localhost:3000/posts.json
Run Code Online (Sandbox Code Playgroud)

JSON数据作为可读文本打印到控制台.我看不出压缩是否已经发生.也许,因为curl已经解压缩了响应?!

在线测试

我也尝试过这里提到的在线测试HTTP压缩测试.它确认JSON内容"不是gzip".这里链接的第二个网站GIDZipTest承认了负面的测试结果.

问题:

  1. 如何从Rails输出gzip压缩的JSON?
  2. 如何配置HTTP客户端以请求gzip压缩数据?
  3. 当我在Heroku(PostgreSQL)上运行Rails服务器时,相同的配置是否仍然有效,......?

得到教训

据我所知,我必须为REST服务器配置use Rack::Deflater.要明确:我不在ActiveSupport::Gzip.compress()我的代码中使用.如果有人感兴趣,这就是启用GZIP压缩时标题的样子.

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
Run Code Online (Sandbox Code Playgroud)

现在我知道了magic关键字,很容易搜索和查找文章use Rack::Deflater.

Dmi*_*dov 7

也许你应该使用Rack :: Deflater?在config.ru中插入:

require ::File.expand_path('../config/environment',  __FILE__)
use Rack::Deflater
run MyAppName::Application
Run Code Online (Sandbox Code Playgroud)

并配置您的http请求标头:

我可以配置HTTP请求的标头:

httpPost.setHeader("Accept-Encoding","gzip");

PS相同的配置适用于Heroku