{"id":87,"date":"2015-10-14T09:12:35","date_gmt":"2015-10-14T09:12:35","guid":{"rendered":"https:\/\/boochlin.com\/?p=87"},"modified":"2016-07-13T14:34:32","modified_gmt":"2016-07-13T14:34:32","slug":"android-volley-customize-request-gzip-and-gson","status":"publish","type":"post","link":"https:\/\/boochlin.com\/?p=87","title":{"rendered":"Android volley customize request : gzip and gson."},"content":{"rendered":"<p><span style=\"color: #0000ff;\">\u300eVolley is an HTTP library that makes networking for Android apps easier and most importantly, faster. Volley is available through the open AOSP repository.\u300f<\/span><\/p>\n<p>volley\u4e5f\u5df2\u7d93\u7528\u4e86\u597d\u4e45\u4e00\u9663\u5b50\uff0c\u4e5f\u6709\u60f3\u904e\u4f86\u5beb\u4e00\u4e9b\u4f7f\u7528\u5fc3\u5f97<br \/>\n\u4f46\u662f\u4e00\u770b\u5df2\u7d93\u6709\u5f88\u591a\u9ad8\u624b\u90fd\u5beb\u904e\u985e\u4f3c\u7684\u8cc7\u6599\uff0c\u56e0\u6b64\u5c31\u6c7a\u5b9a\u4e0d\u51fa\u4f86\u737b\u919c<\/p>\n<p>\u90a3\u5c31\u7c21\u55ae\u8aaa\u4e00\u4e0b\u70ba\u4f55\u4f7f\u7528 volley \u6700\u70ba\u8655\u7406\u7db2\u8def\u4e0b\u8f09\u7684lib<br \/>\n\u7576\u6642\u5c08\u6848\u5728go\u7684\u6642\u5019\uff0c\u9084\u5728\u7528httpconection\u4f86\u8655\u7406\u5404\u7a2e\u9700\u6c42\uff0c\u53ef\u662f\u9019\u771f\u7684\u6709\u5920\u9ebb\u7169<br \/>\ncdn\u7684\u8f49\u5740\uff0cbitmap\u7684\u8f49\u7f6e\uff0c\u56e0\u6b64\u52d5\u4e86\u627e\u500blib\u4f86\u641e\u7684\u60f3\u6cd5\uff0c\u7562\u7adf\u8f2a\u5b50\u90a3\u67d0\u591a\uff0c\u4f55\u5fc5kobe<\/p>\n<p>\u4e00\u627e\u4e4b\u4e0b\uff0c\u9996\u5148\u770b\u5230\u7684\u662fUIL\uff0c\u9a5a\u70ba\u5929\u4eba\uff0c\u5728\u627e\u4e00\u4e0b\uff08\u4f55\u6b62\u8f2a\u5b50\uff0c\u9023\u85cd\u4fdd\u57fa\u5c3c\u90fd\u6709\u5566\uff0cUIL,GLIDE,PICCASO\uff09<br \/>\n\u4e0d\u904e\u6700\u5f8c\u6c7a\u5b9a\u662fvolley\u7684\u6700\u6839\u672c\u7406\u7531\u6839\u672c\u5c31\u53ea\u662f google \u5b98\u65b9\u61c9\u8a72\u6703\u4e00\u76f4\u652f\u63f4\u4e0a\u53bb\u5566<\/p>\n<p><del>\u5ee2\u8a71\u7d50\u675f<\/del><\/p>\n<p><!--more--><\/p>\n<h4>volley\u5f37\u6b78\u5f37\uff0c\u4f46\u662f\u5152\u5b50\u9084\u662f\u8981\u81ea\u5df1\u751f<br \/>\n\u4ee5\u4e0b\u9019\u5169\u500b\u9700\u6c42\u9084\u662f\u8981\u81ea\u5df1\u641e<\/h4>\n<h5><span style=\"color: #0000ff;\">1. gzip<\/span><br \/>\n<span style=\"color: #0000ff;\"> 2. custom response object<\/span><\/h5>\n<p>\u7b2c\u4e00\u9805\u5176\u5be6\u883b\u7c21\u55ae\u5be6\u7528\u7684\uff0c\u5c31\u662f\u964d\u4f4e\u50b3\u8f38\u91cf<br \/>\n\u7b2c\u4e8c\u9805\u5b98\u7db2\u6709\u63d0\u5230<br \/>\n<a href=\"https:\/\/developer.android.com\/training\/volley\/request-custom.html\" target=\"_blank\">Implementing a Custom Request<\/a><br \/>\nGsonRequest:<br \/>\n\u76ee\u524d\u6d41\u884c\u7684json \u683c\u5f0f\u5176\u5be6\u883b\u56fa\u5b9a\u7684\u883b\u9069\u5408\u76f4\u63a5\u8f49\u6210 \u5c0d\u61c9\u7684\u7269\u4ef6<br \/>\n\uff0c\u56e0\u6b64google \u63a8\u51fa<a href=\"https:\/\/github.com\/google\/gson\" target=\"_blank\">gson<\/a>\u628a\u4f7f\u7528\u8005\u89ba\u7684dirty\u7684\u90e8\u4efd\u7d66\u4f5c\u6389<br \/>\njson &#8211;&gt; gson parse &#8211;&gt; Object<\/p>\n<p>gson\u7684\u90e8\u4efd\u5c31\u4e0d\u53e6\u5916\u63d0\uff0c\u5ba2\u5b98\u81ea\u5df1\u770b<\/p>\n<p>ps. \u5f8c\u4f86\u627e\u5230\u4e00\u500b\u597d\u50cf\u66f4\u53b2\u5bb3\u7684 json to object \u7684parse lib\uff0c\u770b\u6548\u80fd\u5c45\u7136\u662fgson\u7684\u56db\u500d\uff0c<br \/>\n\u5149\u662f\u885d\u8005\u56db\u500d\uff0c\u6c7a\u5b9a\u4f86\u7814\u7a76\u770b\u770b\u5566\uff08<a href=\"https:\/\/github.com\/bluelinelabs\/LoganSquare\" target=\"_blank\">LoganSquare<\/a>\uff09<\/p>\n<h4><span style=\"color: #0000ff;\">\u8a73\u7d30code \u4e0a\u83dc\u5566<\/span><\/h4>\n<pre>package com.acer.android.volley;\r\n\r\nimport com.acer.gson.Gson;\r\nimport com.android.volley.NetworkResponse;\r\nimport com.android.volley.ParseError;\r\nimport com.android.volley.Request;\r\nimport com.android.volley.Response;\r\nimport com.android.volley.Response.Listener;\r\nimport com.android.volley.toolbox.HttpHeaderParser;\r\n\r\nimport java.io.BufferedInputStream;\r\nimport java.io.ByteArrayInputStream;\r\nimport java.io.ByteArrayOutputStream;\r\nimport java.io.IOException;\r\nimport java.io.UnsupportedEncodingException;\r\nimport java.util.HashMap;\r\nimport java.util.Map;\r\nimport java.util.zip.GZIPInputStream;\r\n\r\npublic abstract class GzipJsonObjectRequest extends Request {\r\n\r\n    private final Gson gson = new Gson();\r\n    private final Class clazz;\r\n    private final Map&lt;String, String&gt; headers;\r\n    private final Listener listener;\r\n    private static final String GZIP_CONTENT_TYJE = \"application\/gzip;charset=UTF-8\";\r\n    private static final String GZIP_ENCODIING_TYPE = \"gzip\";\r\n\r\n    public GzipJsonObjectRequest(String url, Class clazz, Map&lt;String, String&gt; headers,\r\n                                 Listener listener, Response.ErrorListener errorListener) {\r\n        super(Method.GET, url, errorListener);\r\n        \/\/ \u50b3\u5165\u5c0d\u61c9\u7684gson parsing object: clazz\r\n        this.clazz = clazz;\r\n        this.headers = headers;\r\n        this.listener = listener;\r\n    }\r\n\r\n\r\n    @Override\r\n    protected Response parseNetworkResponse(NetworkResponse response) {\r\n        try {\r\n            String jsonString = null;\r\n            String contentType = response.headers.get(\"Content-Encoding\");\r\n            \/\/ \u5982\u679c\u6709\u652f\u63f4gzip\u5247\u5229\u7528GZIPInputStream\u628a\u8cc7\u6599\u7d66\u8b80\u51fa\u4f86\r\n            if (contentType.equalsIgnoreCase(GZIP_ENCODIING_TYPE)) {\r\n                try (GZIPInputStream zis = new GZIPInputStream(new BufferedInputStream(new ByteArrayInputStream(response.data)));\r\n                     ByteArrayOutputStream baos = new ByteArrayOutputStream()) {\r\n                    byte[] buffer = new byte[1024];\r\n                    int count;\r\n                    while ((count = zis.read(buffer)) != -1) {\r\n                        baos.write(buffer, 0, count);\r\n                    }\r\n                    byte[] bytes = baos.toByteArray();\r\n                    jsonString = new String(bytes);\r\n                }\r\n            } else {\r\n                jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));\r\n            }\r\n            \/\/ \u6700\u5f8cgzip \u62ff\u51fa\u4f86\u7684string \u5728\u5229\u7528gson parsing \u6210\u9700\u8981\u7684 clazz\r\n            return Response.success(\r\n                    gson.fromJson(jsonString, clazz),\r\n                    HttpHeaderParser.parseCacheHeaders(response));\r\n        } catch (UnsupportedEncodingException e) {\r\n            return Response.error(new ParseError(e));\r\n        } catch (IOException e) {\r\n            return Response.error(new ParseError(e));\r\n        }\r\n    }\r\n    @Override\r\n    protected Map&lt;String, String&gt; getParams() {\r\n        \/\/ \u5728send request \u52a0\u5165 gzip \u8981\u6c42\uff0c\u5982\u679c\u6709\u652f\u63f4\u7684\u8a71\r\n        HashMap&lt;String, String&gt; hashMap = new HashMap&lt;&gt;();\r\n        hashMap.put(\"Accept-Encoding\", GZIP_ENCODIING_TYPE);\r\n        return hashMap;\r\n    }\r\n}\r\n<\/pre>\n<p>reference:<br \/>\n<a href=\"https:\/\/github.com\/bluelinelabs\/LoganSquare\">gzipInputStream<\/a><br \/>\n<a href=\"https:\/\/github.com\/bluelinelabs\/LoganSquare\" target=\"_blank\">LoganSquare<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u300eVolley is an HTTP library that makes networking for Android apps easier and most importantly, faste&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[50,52],"tags":[21,22,9,20],"_links":{"self":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/87"}],"collection":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=87"}],"version-history":[{"count":1,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/87\/revisions"}],"predecessor-version":[{"id":88,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/87\/revisions\/88"}],"wp:attachment":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=87"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=87"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=87"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}