我一整天都在尝试让这件事有效,但现在还不行。我在这里检查了很多帖子并测试了很多不同的实现,我们现在不知道在哪里看...
这是我的情况,我的服务器上有一个小的php测试文件(gz.php),如下所示:
header("Content-Encoding: gzip");
print("\x1f\x8b\x08\x00\x00\x00\x00\x00");
$contents = gzcompress("Is it working?", 9);
print($contents);
这是我能做的最简单的事情,它适用于任何网络浏览器。
现在我有一个使用Jsoup的Android活动,其中包含以下代码:
URL url = new URL("http://myServerAdress.com/gz.php");
doc = Jsoup.parse(url, 1000);
这导致“Jsoup.parse”行上出现空EOFException。
我到处都读过Jsoup应该解析gzip压缩的内容而不必做任何特别的事情,但显然有些东西丢失了。
我尝试了许多其他方法,比如使用Jsoup.connect()。get()或InpuStream,GZipInputStream和DataInpuStream。我也尝试过PHP中的gzDeflate()和gzencode()方法,但也没有运气。我甚至尝试过不在PHP中声明头文件编码并尝试稍后对内容进行缩减......但它的效果非常聪明......
它必须是“愚蠢的”我缺少的东西,但我不知道是什么......有人有想法吗?
(ps:我正在使用Jsoup 1.7.0,所以最新版本为现在)
答案 0 :(得分:0)
根据来自here的信息,提问者在评论中指出gzcompress正在编写一个既错误又不完整的CRC,执行代码为:
// Display the header of the gzip file
// Thanks ck@medienkombinat.de!
// Only display this once
echo "\x1f\x8b\x08\x00\x00\x00\x00\x00";
// Figure out the size and CRC of the original for later
$Size = strlen($contents);
$Crc = crc32($contents);
// Compress the data
$contents = gzcompress($contents, 9);
// We can't just output it here, since the CRC is messed up.
// If I try to "echo $contents" at this point, the compressed
// data is sent, but not completely. There are four bytes at
// the end that are a CRC. Three are sent. The last one is
// left in limbo. Also, if we "echo $contents", then the next
// byte we echo will not be sent to the client. I am not sure
// if this is a bug in 4.0.2 or not, but the best way to avoid
// this is to put the correct CRC at the end of the compressed
// data. (The one generated by gzcompress looks WAY wrong.)
// This will stop Opera from crashing, gunzip will work, and
// other browsers won't keep loading indefinately.
//
// Strip off the old CRC (it's there, but it won't be displayed
// all the way -- very odd)
$contents = substr($contents, 0, strlen($contents) - 4);
// Show only the compressed data
echo $contents;
// Output the CRC, then the size of the original
gzip_PrintFourChars($Crc);
gzip_PrintFourChars($Size);
Jonathan Hedley评论说,“jsoup只是uses a normal Java GZIPInputStream来解析gzip,所以你可以用任何Java程序来解决这个问题。” EOFException可能是由于CRC不完整。