Jsoup和gzipped html内容(Android)

Jsoup和gzipped html内容(Android),android,gzip,jsoup,Android,Gzip,Jsoup,我一整天都在试着让这东西工作,但现在还不行。我在这里查阅了太多的帖子,测试了太多不同的实现,现在我不知道该去哪里看 以下是我的情况,我的服务器上有一个小的php测试文件(gz.php),如下所示: header("Content-Encoding: gzip"); print("\x1f\x8b\x08\x00\x00\x00\x00\x00"); $contents = gzcompress("Is it working?", 9); print($contents); 这是我能做的最简单的

我一整天都在试着让这东西工作,但现在还不行。我在这里查阅了太多的帖子,测试了太多不同的实现,现在我不知道该去哪里看

以下是我的情况,我的服务器上有一个小的php测试文件(gz.php),如下所示:

header("Content-Encoding: gzip");
print("\x1f\x8b\x08\x00\x00\x00\x00\x00");
$contents = gzcompress("Is it working?", 9);
print($contents);
这是我能做的最简单的事情,它可以与任何web浏览器配合使用

现在,我有一个使用Jsoup的Android活动,其中包含以下代码:

URL url = new URL("http://myServerAdress.com/gz.php");
doc = Jsoup.parse(url, 1000);
这会在“Jsoup.parse”行上导致一个空的EOFEException

我到处都读到Jsoup应该解析Gzip内容,而不需要做任何特殊的事情,但是很明显,缺少了一些东西

我尝试过许多其他方法,比如使用Jsoup.connect().get()或InpuStream、GZipInputStream和DataInpuStream。我也尝试过PHP中的gzDeflate()和gzencode()方法,但也没有成功。我甚至试着不在PHP中声明标题编码,并在以后尝试缩减内容…但它非常聪明有效

这一定是我错过的“愚蠢”的东西,但我就是不知道。。。有人有主意吗


(注:我使用的是Jsoup 1.7.0,所以是目前最新的版本)

提问者在评论中指出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);

注释:“jsoup只是为了解析gzip,所以任何Java程序都会遇到这个问题。”EOFException可能是由于CRC不完整造成的。

你能把PHP脚本放在某个地方让我测试一下吗?此外,1.6.1是最新版本。您对Jsoup版本的选择是正确的。。。我的道歉(我知道我有最新版本,上一个版本是1.6.0…。我今天早上在这个页面上找到了解决方案:。看起来是页面上自动写入的crc有问题。普通浏览器可以使用它,但Jsoup不行。所以我用了一种方法来压制它,“瞧”。问题解决了!谢谢你的努力;)酷。jsoup只是一个普通的JavaGZIPInputStream来解析gzip,所以任何Java程序都会遇到这个问题。也许可以用你找到的答案更新你的问题,并将其标记为已回答。