Delphi: 如何设置TWebResponse 包含gzip 并在Android App 中读取?

Delphi: How to set TWebResponse to contain gzip and read it in Android App?

我正在为 Android 应用程序构建 Web 服务器。(WebBroker->VC)。

如何将我的响应字符串压缩为 gzip,我试过这个:

 a:=[{.......}];//json data
 srcbuf := BytesOf(a.ToString);
 ZCompress(srcbuf, destbuf, zcDefault);
 Response.Content := '';
 Response.ContentStream := TMemoryStream.Create;
 Response.ContentEncoding := 'deflate';
 Response.ContentType := 'application/json';
 Response.ContentStream.Write(@(destbuf[0]),length(destbuf));
 Response.ContentLength := (length(destbuf));

在我的 android 应用程序中出现错误:

未知格式(幻数...)

我还有用 PHP 编写的服务器端,android 应用程序工作正常,我认为 Delphi System.ZLib 不能像 gzip 一样压缩?

知道如何解决这个问题吗?

我找到了解决方案:

Delphi:

//------------------------------------------------------------------------------
procedure doGZIP(Input, gZipped: TMemoryStream);//helper function
const
  GZIP = 31;//very important because gzip is a linux zip format
var
  CompactadorGZip: TZCompressionStream;
begin
  Input.Position:=0;
  CompactadorGZip:=TZCompressionStream.Create(gZipped, zcMax, GZIP);
  CompactadorGZip.CopyFrom(Input, Input.Size);
  CompactadorGZip.Free;
  gZipped.Position:=0;
end;
//------------------------------------------------------------------------------
.
.
.
strJSON:=a.ToString;//a->TJSONArray
 //init
 Original:=TMemoryStream.Create;
 gZIPStream:=TMemoryStream.Create;
 //copy result to stream
 oString := UTF8String(strJSON);
 len := length(oString);
 Original.WriteBuffer(oString[1], len);
 //make it gzip
 doGZIP(Original,gZIPStream);
 //prepare responsestream and set content encoding and type
 Response.Content := '';
 Response.ContentStream:=gZIPStream;
 Response.ContentEncoding := 'gzip';
 Response.ContentType := 'application/json';
 //clear...
 Original.Free;

Android:

conn.setRequestProperty("Accept-Encoding", "gzip");//do not forget this