phil-brown/droidQuery

GZIPped JSON data

Closed this issue · 6 comments

Is there a way to detect if a returned JSON stream has been gzipped as a result of a $.ajax call?

In my existing code, I check the response headers to see if gzip is indicated, then run it through standard GZIPInputStream to get to the uncompressed string, and so the JSONObject.

Using $.ajax I couldn't see how I could check to see if it was GZIPped in .success, or how to cast / process params[0] afterwards.

@mrsean2k I am adding code to first allow the dataType "raw", which will set params[0] to be a byte[]. Additionally, in the success function, I am adding a third parameter that you can cast to Header[]. So if you do something like this:

$.ajax(new AjaxOptions().url("http://my.url.com").dataType("raw").success(new Function() {
    @Override
    public void invoke($ d, Object... args) {
        Header[] headers = args[2];
        for (Header h : headers) {
            if (h.getName().equalsIgnoreCase("Content-Encoding")) {
                if (h.getValue.equals("gzip")) {
                    //handle GZIpped data
                }
                else {
                    Object json = $.parseJSON(new String((byte[]) args[0]));
                    //handle this JSONObject or JSONArray
                }
            }
        }
    }
}));

I will close this soon, and you can test it out!

@mrsean2k I made the changes above (not a new release - but it is new code). Does this work for you now, so I can close it?

Thanks for the changes. I'm using Android Studio and a lot of this is unfamiliar territory to me, so it'll take me a while to work out how to pull and build the changes to test; I'll report back as soon as I'm there.

@mrsean2k have you had a chance to test this?

These changes have been included in the latest release. Closing issue.

@mrsean2k the latest update now automatically handles gzip compression, so you no longer have to process the data yourself using the raw dataType.