应该使用什么标头将 GZIP 压缩的 JSON 从 Android 客户端发送到服务器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11414987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What header should be used for sending GZIP compressed JSON from Android Client to Server?
提问by Gaurav Agarwal
This question is extension to the question here. I am using the code herereproduced below to GZIP compress a JSONObject
.
这个问题是这里问题的延伸。我使用的代码在这里转载如下,以GZIP压缩JSONObject
。
String foo = "value";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream gzos = null;
try {
gzos = new GZIPOutputStream(baos);
gzos.write(foo.getBytes("UTF-8"));
} finally {
if (gzos != null) try { gzos.close(); } catch (IOException ignore) {};
}
byte[] fooGzippedBytes = baos.toByteArray();
I am using a DefaultHttpClient
to send this compressed JSONObject to server(the code is in my control).
我正在使用 aDefaultHttpClient
将这个压缩的 JSONObject 发送到服务器(代码在我的控制之下)。
My Question
我的问题
What header should I use in my request
? I am using request.setHeader("Content-type", "application/json");
for sending JSON to server?
我应该在我的request
? 我request.setHeader("Content-type", "application/json");
用于将 JSON 发送到服务器?
回答by Michael Hampton
To inform the server that you are sending gzip-encoded data, send the Content-Encodingheader, not Accept-Encoding.
要通知服务器您正在发送 gzip 编码的数据,请发送Content-Encoding标头,而不是Accept-Encoding。
回答by Audrius
Thisanswer shows you that you need to set a header indicating that you are sending data compressed:
这个答案表明您需要设置一个标头,表明您正在发送压缩数据:
HttpUriRequest request = new HttpGet(url);
request.addHeader("Content-Encoding", "gzip");
// ...
httpClient.execute(request);
The answer also shows how to deal with the incoming compressed data.
答案还显示了如何处理传入的压缩数据。
回答by Jagadeesan M
Content-Type:application/json
- tells what type of content data in the http callContent-Encoding: gzip
- tells what compression being used.
Content-Type:application/json
- 说明 http 调用中的内容数据类型Content-Encoding: gzip
- 说明正在使用什么压缩。
application/json
or text/xml
or other type can be compressed as gzip and sending to receiver and with Content-Type
header only, receiver will identify the incoming data is of type json/xml/text
and then convert back to object of type json/xml/text.
application/json
或text/xml
或其他类型可以压缩为 gzip 并发送到接收器并且Content-Type
仅带有标头,接收器将识别传入数据的类型json/xml/text
,然后转换回 json/xml/text 类型的对象。
With Content-Encoding
header only receiver will identify the incoming data is getting gzip compressed. Then receiver required to decompress the incoming data and use it.
Content-Encoding
仅使用标头接收器将识别传入数据正在被 gzip 压缩。然后接收器需要解压缩传入的数据并使用它。