This has really been annoying me and it is quite important for me to find the problem here. The android code below is not unzipping for me.
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet("http://www**************/get.php");
get.addHeader("Accept-Encoding", "gzip");
try {
HttpResponse response = client.execute(get);
InputStream instream = response.getEntity().getContent();
Header contentEncoding = response.getFirstHeader("Content-Encoding");
//Log msg header
Header[] header = response.getAllHeaders();
for(Header i : header) {
Log.d("TEST", i.getName() + ":" + i.getValue() );
}
if (contentEncoding.getValue().equalsIgnoreCase("gzip")) {
Log.d("TEST", "Content is gzipped");
try {
instream = new GZIPInputStream(instream);
processStream(instream);
} finally {
instream.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}
Logcat output:
D/TEST ( 1067): Date:Fri, 03 Jun 2011 05:18:33 GMT
D/TEST ( 1067): Server:Apache
D/TEST ( 1067): X-Powered-By:PHP/5.2.10
D/TEST ( 1067): Content-Encoding:gzip
D/TEST ( 1067): Content-Length:765
D/TEST ( 1067): Connection:close
D/TEST ( 1067): Content-Type:application/json
D/TEST ( 1067): Content is gzipped
W/System.err( 1067): java.io.IOException: Unknown format
W/System.err( 1067): at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:85)
W/System.err( 1067): at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:66)
W/System.err( 1067): at com.mannotron.gallery.GalleryExample$GetImages.doInBackground(GalleryExample.java:69)
W/System.err( 1067): at com.mannotron.gallery.GalleryExample$GetImages.doInBackground(GalleryExample.java:1)
W/System.err( 1067): at android.os.AsyncTask$2.call(AsyncTask.java:185)
W/System.err( 1067): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
W/System.err( 1067): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
W/System.err( 1067): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
W/System.err( 1067): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
W/System.err( 1067): at java.lang.Thread.run(Thread.java:1096)
edit: added php code
Server side php:
//ob_start('ob_gzhandler');
header("HTTP/1.1 200 OK");
header("Content-Type: application/json");
header("Content-Encoding: gzip");
$dbhost = "xxxxxx";
$dbname = "xxxxxx";
$user = "xxxxxx";
$pass = "xxxxxx";
//Open databse connection
$db = new mysqli($dbhost,$user,$pass,$dbname) or die('Error opening db');
$query = "SELECT * FROM Spots; ";
$result = null;
$result = $db->query($query);
$data = array();
while($row = $result->fetch_assoc()) {
$data [] = $row;
}
$spots = '{"spots":'.json_encode($data)."}";
echo gzencode($spots,FORCE_GZIP);
$db->close();
//ob_flush();
?>
I have tried both gz_encode() and outputbuffering with gzip at the server end in php. Can anyone s开发者_开发知识库ee what I'm doing wrong? Thanks for your time.
The stream looks wrong, as it should start with '1f8b'. The reason might be a stray 'a' in your php-file. But the posted samples seem to be all right.
精彩评论