androiddelphiindyidhttp

TIdCompressorZLib doesn't work in FireMonkey app compiled for Android


I have a function using TIdCompressorZLib and TIdHTTP, like this:

pegar := tidhttp.create(nil);
compressor := TIdCompressorZLib.create(pegar);
seguro := TIdSSLIOHandlerSocketOpenSSL.create(pegar);
pegar.compressor := compressor;
pegar.Request.UserAgent := UserAgent;
pegar.Request.Accept := '*/*';
pegar.Request.AcceptEncoding := 'gzip, deflate';
pegar.Request.AcceptLanguage := 'pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7';
pegar.HTTPOptions := [hoForceEncodeParams];
pegar.ReadTimeout := 60000;
pegar.ConnectTimeout := 60000;
pegar.HandleRedirects := true;
pegar.IOHandler := seguro;
pegar.CookieManager := cookm;
pagina := pegar.Get(proxima_pagina);

This code works fine when compiled for Windows, but not for Android. On Android, TIdCompressorZLib does not unpack the source, returning unreadable characters.

Is there any way to fix this? I'm using Delphi 10.3.


Solution

  • Get rid of the assignment of the AcceptEncoding property. Let TIdHTTP handle that internally for you based on the actual capabilities of the Compressor. You are explicitly granting permission to the HTTP server to send a compressed response even if the Compressor is not able to handle it (ie, if Compressor.IsReady is False).

    That being said, Indy accesses ZLib on non-Windows platforms using dynamic load libraries. In this case, the libz dylib on 'Nix platforms (Android runs on top of Linux), which is likely not present in your Android project. Compressor.IsReady returns False if Indy can't load the ZLib library at runtime. So, make sure that dylib is present in your deployment. Indy has an IdZLibSetLibPath() function in the IdZLibHeaders unit if you need to specify the location of that file at runtime.