{"id":7736,"date":"2015-10-19T01:17:10","date_gmt":"2015-10-19T01:17:10","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/10\/19\/retrofit-okhttp-retrieve-gzipinputstream-open-source-projects-square-okhttp\/"},"modified":"2015-10-19T01:17:10","modified_gmt":"2015-10-19T01:17:10","slug":"retrofit-okhttp-retrieve-gzipinputstream-open-source-projects-square-okhttp","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/10\/19\/retrofit-okhttp-retrieve-gzipinputstream-open-source-projects-square-okhttp\/","title":{"rendered":"retrofit + okhttp : Retrieve GZIPInputStream-open source projects square\/okhttp"},"content":{"rendered":"<p>After running into a similar issue (in my case, without adding any Accept-Encoding header, it would occasionally fail to un-gzip the response, leaving also the Content-Encoding: gzip header in it, crashing the JSON parser), and with no clear way around this, I manually enabled gzip for Retrofit by creating the delegated Client implementation below. It works great, except that you probably should not use it for very large (e.g. &gt; 250KB) responses, as they are first copied into a byte array.<\/p>\n<pre><code>public class GzippedClient implements Client {\n\n    private Client wrappedClient;\n\n    public GzippedClient(Client wrappedClient) {\n        this.wrappedClient = wrappedClient;\n    }\n\n    @Override\n    public Response execute(Request request) throws IOException {\n        Response response = wrappedClient.execute(request);\n\n        boolean gzipped = false;\n        for (Header h : response.getHeaders()) {\n            if (h.getName() != null &amp;&amp; h.getName().toLowerCase().equals(\"content-encoding\") &amp;&amp; h.getValue() != null &amp;&amp; h.getValue().toLowerCase().equals(\"gzip\")) {\n                gzipped = true;\n                break;\n            }\n        }\n\n        Response r = null;\n        if (gzipped) {\n            InputStream is = null;\n            ByteArrayOutputStream bos = null;\n\n            try {\n                is = new BufferedInputStream(new GZIPInputStream(response.getBody().in()));\n                bos = new ByteArrayOutputStream();\n\n                int b;\n                while ((b = is.read()) != -1) {\n                    bos.write(b);\n                }\n\n                TypedByteArray body = new TypedByteArray(response.getBody().mimeType(), bos.toByteArray());\n                r = new Response(response.getUrl(), response.getStatus(), response.getReason(), response.getHeaders(), body);\n            } finally {\n                if (is != null) {\n                    is.close();\n                }\n                if (bos != null) {\n                    bos.close();\n                }\n            }\n        } else {\n            r = response;\n        }\n        return r;\n    }\n\n}\n<\/code><\/pre>\n<p>You will also have to add an Accept-Encoding header to your requests, e.g. by using a RequestInterceptor<\/p>\n<pre><code>requestFacade.addHeader(\"Accept-Encoding\", \"gzip\");\n<\/code><\/pre>\n<p>Finally, you have to wrap your existing Client into this new GzippedClient, like so:<\/p>\n<pre><code>restBuilder.setClient(new GzippedClient(new OkClient(okHttpClient)));\n<\/code><\/pre>\n<p>That&#8217;s it. Now your data will be gzipped.<\/p>\n<p>EDIT: It seems that in OkHttp version 1.5.1, a bug (https:\/\/github.com\/square\/okhttp\/pull\/632) seems to have been fixed related to the transparent gzipping which may (or may not) have been the source of my initial issue. If so, the occasional failure to un-gzip may no longer occur, though it happened rarely enough that I cannot confirm this yet. Either way, if you want to rely on your own, rather than the transparent adding\/removing of headers and gzipping, then the solution described will work.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>After running into a similar issue (in my case, without adding any Accept-Encoding header, it would occasionally fail to un-gzip the response, leaving also the Content-Encoding: gzip header in it, crashing the JSON parser), and with no clear way around this, I manually enabled gzip for Retrofit by creating the delegated Client implementation below. It [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7736","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7736","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7736"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7736\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7736"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7736"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7736"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}