{"id":87,"date":"2015-10-14T09:12:35","date_gmt":"2015-10-14T09:12:35","guid":{"rendered":"https:\/\/boochlin.com\/?p=87"},"modified":"2026-09-19T18:04:23","modified_gmt":"2026-09-19T18:04:23","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>\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<\/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<\/p>\n<p>\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<\/p>\n<p>\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<\/p>\n<p>cdn\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<\/p>\n<p>\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>\u5ee2\u8a71\u7d50\u675f<\/p>\n<h4>volley\u5f37\u6b78\u5f37\uff0c\u4f46\u662f\u5152\u5b50\u9084\u662f\u8981\u81ea\u5df1\u751f<\/h4>\n<p>\u4ee5\u4e0b\u9019\u5169\u500b\u9700\u6c42\u9084\u662f\u8981\u81ea\u5df1\u641e<\/p>\n<h5>1. gzip<\/h5>\n<ol start=\"2\">\n<li>custom response object<\/li>\n<\/ol>\n<p>\u7b2c\u4e00\u9805\u5176\u5be6\u883b\u7c21\u55ae\u5be6\u7528\u7684\uff0c\u5c31\u662f\u964d\u4f4e\u50b3\u8f38\u91cf<\/p>\n<p>\u7b2c\u4e8c\u9805\u5b98\u7db2\u6709\u63d0\u5230<\/p>\n<p><a href=\"https:\/\/developer.android.com\/training\/volley\/request-custom.html\">Implementing a Custom Request<\/a><\/p>\n<p>GsonRequest:<\/p>\n<p>\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<\/p>\n<p>\uff0c\u56e0\u6b64google \u63a8\u51fa<a href=\"https:\/\/github.com\/google\/gson\">gson<\/a>\u628a\u4f7f\u7528\u8005\u89ba\u7684dirty\u7684\u90e8\u4efd\u7d66\u4f5c\u6389<\/p>\n<p>json \u2013&gt; gson parse \u2013&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<\/p>\n<p>\u5149\u662f\u885d\u8005\u56db\u500d\uff0c\u6c7a\u5b9a\u4f86\u7814\u7a76\u770b\u770b\u5566\uff08<a href=\"https:\/\/github.com\/bluelinelabs\/LoganSquare\">LoganSquare<\/a>\uff09<\/p>\n<h4>\u8a73\u7d30code \u4e0a\u83dc\u5566<\/h4>\n<pre><code class=\"hljs language-java\"><span class=\"hljs-keyword\">package<\/span> com.acer.android.volley;\n\n<span class=\"hljs-keyword\">import<\/span> com.acer.gson.Gson;\n<span class=\"hljs-keyword\">import<\/span> com.android.volley.NetworkResponse;\n<span class=\"hljs-keyword\">import<\/span> com.android.volley.ParseError;\n<span class=\"hljs-keyword\">import<\/span> com.android.volley.Request;\n<span class=\"hljs-keyword\">import<\/span> com.android.volley.Response;\n<span class=\"hljs-keyword\">import<\/span> com.android.volley.Response.Listener;\n<span class=\"hljs-keyword\">import<\/span> com.android.volley.toolbox.HttpHeaderParser;\n\n<span class=\"hljs-keyword\">import<\/span> java.io.BufferedInputStream;\n<span class=\"hljs-keyword\">import<\/span> java.io.ByteArrayInputStream;\n<span class=\"hljs-keyword\">import<\/span> java.io.ByteArrayOutputStream;\n<span class=\"hljs-keyword\">import<\/span> java.io.IOException;\n<span class=\"hljs-keyword\">import<\/span> java.io.UnsupportedEncodingException;\n<span class=\"hljs-keyword\">import<\/span> java.util.HashMap;\n<span class=\"hljs-keyword\">import<\/span> java.util.Map;\n<span class=\"hljs-keyword\">import<\/span> java.util.zip.GZIPInputStream;\n\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">abstract<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title class_\">GzipJsonObjectRequest<\/span> <span class=\"hljs-keyword\">extends<\/span> <span class=\"hljs-title class_\">Request<\/span> {\n\n    <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">final<\/span> <span class=\"hljs-type\">Gson<\/span> <span class=\"hljs-variable\">gson<\/span> <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">Gson<\/span>();\n    <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">final<\/span> Class clazz;\n    <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">final<\/span> Map&lt;String, String&gt; headers;\n    <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">final<\/span> Listener listener;\n    <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">final<\/span> <span class=\"hljs-type\">String<\/span> <span class=\"hljs-variable\">GZIP_CONTENT_TYJE<\/span> <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-string\">&quot;application\/gzip;charset=UTF-8&quot;<\/span>;\n    <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">final<\/span> <span class=\"hljs-type\">String<\/span> <span class=\"hljs-variable\">GZIP_ENCODIING_TYPE<\/span> <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-string\">&quot;gzip&quot;<\/span>;\n\n    <span class=\"hljs-type\">public<\/span> <span class=\"hljs-title function_\">GzipJsonObjectRequest<\/span><span class=\"hljs-params\">(String url, Class clazz, Map&lt;String, String&gt; headers,\n                                 Listener listener, Response.ErrorListener errorListener)<\/span> {\n        <span class=\"hljs-built_in\">super<\/span>(Method.GET, url, errorListener);\n        <span class=\"hljs-comment\">\/\/ \u50b3\u5165\u5c0d\u61c9\u7684gson parsing object: clazz<\/span>\n        <span class=\"hljs-built_in\">this<\/span>.clazz = clazz;\n        <span class=\"hljs-built_in\">this<\/span>.headers = headers;\n        <span class=\"hljs-built_in\">this<\/span>.listener = listener;\n    }\n\n    <span class=\"hljs-meta\">@Override<\/span>\n    <span class=\"hljs-keyword\">protected<\/span> <span class=\"hljs-type\">Response<\/span> <span class=\"hljs-title function_\">parseNetworkResponse<\/span><span class=\"hljs-params\">(NetworkResponse response)<\/span> {\n        <span class=\"hljs-keyword\">try<\/span> {\n            <span class=\"hljs-type\">String<\/span> <span class=\"hljs-variable\">jsonString<\/span> <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-literal\">null<\/span>;\n            <span class=\"hljs-type\">String<\/span> <span class=\"hljs-variable\">contentType<\/span> <span class=\"hljs-operator\">=<\/span> response.headers.get(<span class=\"hljs-string\">&quot;Content-Encoding&quot;<\/span>);\n            <span class=\"hljs-comment\">\/\/ \u5982\u679c\u6709\u652f\u63f4gzip\u5247\u5229\u7528GZIPInputStream\u628a\u8cc7\u6599\u7d66\u8b80\u51fa\u4f86<\/span>\n            <span class=\"hljs-keyword\">if<\/span> (contentType.equalsIgnoreCase(GZIP_ENCODIING_TYPE)) {\n                <span class=\"hljs-keyword\">try<\/span> (<span class=\"hljs-type\">GZIPInputStream<\/span> <span class=\"hljs-variable\">zis<\/span> <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">GZIPInputStream<\/span>(<span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">BufferedInputStream<\/span>(<span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">ByteArrayInputStream<\/span>(response.data)));\n                     <span class=\"hljs-type\">ByteArrayOutputStream<\/span> <span class=\"hljs-variable\">baos<\/span> <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">ByteArrayOutputStream<\/span>()) {\n                    <span class=\"hljs-type\">byte<\/span>[] <span class=\"hljs-variable\">buffer<\/span> <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">byte<\/span>[<span class=\"hljs-number\">1024<\/span>];\n                    <span class=\"hljs-type\">int<\/span> count;\n                    <span class=\"hljs-keyword\">while<\/span> ((count = zis.read(buffer)) != -<span class=\"hljs-number\">1<\/span>) {\n                        baos.write(buffer, <span class=\"hljs-number\">0<\/span>, count);\n                    }\n                    <span class=\"hljs-type\">byte<\/span>[] <span class=\"hljs-variable\">bytes<\/span> <span class=\"hljs-operator\">=<\/span> baos.toByteArray();\n                    jsonString = <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">String<\/span>(bytes);\n                }\n            } <span class=\"hljs-keyword\">else<\/span> {\n                jsonString = <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">String<\/span>(response.data, HttpHeaderParser.parseCharset(response.headers));\n            }\n            <span class=\"hljs-comment\">\/\/ \u6700\u5f8cgzip \u62ff\u51fa\u4f86\u7684string \u5728\u5229\u7528gson parsing \u6210\u9700\u8981\u7684 clazz<\/span>\n            <span class=\"hljs-keyword\">return<\/span> Response.success(\n                    gson.fromJson(jsonString, clazz),\n                    HttpHeaderParser.parseCacheHeaders(response));\n        } <span class=\"hljs-keyword\">catch<\/span> (UnsupportedEncodingException e) {\n            <span class=\"hljs-keyword\">return<\/span> Response.error(<span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">ParseError<\/span>(e));\n        } <span class=\"hljs-keyword\">catch<\/span> (IOException e) {\n            <span class=\"hljs-keyword\">return<\/span> Response.error(<span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">ParseError<\/span>(e));\n        }\n    }\n    <span class=\"hljs-meta\">@Override<\/span>\n    <span class=\"hljs-keyword\">protected<\/span> <span class=\"hljs-type\">Map<\/span>&lt;String, String&gt; <span class=\"hljs-title function_\">getParams<\/span><span class=\"hljs-params\">()<\/span> {\n        <span class=\"hljs-comment\">\/\/ \u5728send request \u52a0\u5165 gzip \u8981\u6c42\uff0c\u5982\u679c\u6709\u652f\u63f4\u7684\u8a71<\/span>\n        <span class=\"hljs-type\">HashMap<\/span>&lt;String, String&gt; <span class=\"hljs-variable\">hashMap<\/span> <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">HashMap<\/span>&lt;&gt;();\n        hashMap.put(<span class=\"hljs-string\">&quot;Accept-Encoding&quot;<\/span>, GZIP_ENCODIING_TYPE);\n        <span class=\"hljs-keyword\">return<\/span> hashMap;\n    }\n}<\/code><\/pre>\n<p>reference:<\/p>\n<p><a href=\"https:\/\/github.com\/bluelinelabs\/LoganSquare\">gzipInputStream<\/a><\/p>\n<p><a href=\"https:\/\/github.com\/bluelinelabs\/LoganSquare\">LoganSquare<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u300eVolley is an HTTP library that makes networking for An [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[169],"tags":[208,253,264,310],"_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":5,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/87\/revisions"}],"predecessor-version":[{"id":1582,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/87\/revisions\/1582"}],"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}]}}