0

I am trying to get compressed data from server. The guy that programmed the server told me, that he uses ZLIB library on his iPhone, and the gzcompress on server. I was trying to find any suitable way to get that data, but it ends up with info "java.io.IOException: unknown format (magic number 9c78)" while creating GZIPInputStream object. Finally I've reached point, where I had data as a String. It was compressed, so I used that answer to decompress: https://stackoverflow.com/a/6963668/419308 . But that code doesn't work. "in.read()" returns -1 at the beginning.

Anyone has any idea why there's -1 ? Or maybe a better way to get the compressed data?

EDIT: I tried adding file to project and reading from that file. in.read() didn't return -1

EDIT2: According to jJ's answer I've tried this code:

HttpGet request = new HttpGet( urlTeam );
HttpResponse response = new DefaultHttpClient().execute( request );
HttpEntity entity = response.getEntity();

InputStream stream = AndroidHttpClient.getUngzippedContent( entity );

InputStreamReader reader = new InputStreamReader( stream );
BufferedReader buffer = new BufferedReader( reader );

StringBuilder sb = new StringBuilder();
sb.delete( 0, sb.length() );
String input;
while ( ( input = buffer.readLine() ) != null )
{
     sb.append( input );
}

But the answer is still compressed (or unreadable)

Community
  • 1
  • 1
Seraphis
  • 1,026
  • 2
  • 14
  • 30
  • -1 is returned if there is nothing to read – pengibot May 17 '12 at 09:16
  • So that code is not correct? I'm sure the passed String was not empty – Seraphis May 17 '12 at 09:19
  • from the api "the actual number of bytes read, or -1 if the end of the compressed input stream is reached" – pengibot May 17 '12 at 09:24
  • have you tried reading in your own zipped file to make sure it is not server side where something is going wrong. – pengibot May 17 '12 at 09:25
  • I tried to, but after adding file to assets or res/raw, I couldn't compile project because of errors (Unable to add '/xxx.json.gz': Zip add failed, ERROR: unable to process assets while packaging '\bin\resources.ap_', and ERROR: packaging of '\bin\resources.ap_' failed). I'll try to find a solution for that and check that file – Seraphis May 17 '12 at 09:30
  • I updated question. File was readable. – Seraphis May 17 '12 at 09:55

1 Answers1

0

on android you should use either HttpURLConnection which handles decompression (and http encoding headers) for you from GingerBread on or AndroidHttpClient (for older android versions) that has helper methods like getUngzippedContent and modifyRequestToAcceptGzipResponse.

This is a good summary Android HTTP clients

jJ'
  • 3,038
  • 32
  • 25
  • I updated question with code containing AndroidHttpClient, which still brings me nothing.. Maybe there's something wrong? – Seraphis May 17 '12 at 10:59
  • 1
    I used getUngzippedContent() method from class AndroidHttpClient. Problem is that it won't work on lower Androids, but then I will download uncompressed data :-) Thanks for your guides, jJ :-) – Seraphis May 17 '12 at 14:38