{"id":1055,"date":"2015-12-15T22:15:01","date_gmt":"2015-12-15T14:15:01","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=1055"},"modified":"2026-08-18T12:58:45","modified_gmt":"2026-08-18T04:58:45","slug":"android-%e5%90%84%e5%a4%a7%e7%bd%91%e7%bb%9c%e8%af%b7%e6%b1%82%e5%ba%93%e7%9a%84%e6%af%94%e8%be%83%e5%8f%8a%e5%ae%9e%e6%88%98","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=1055","title":{"rendered":"Android \u7f51\u7edc\u8bf7\u6c42\u5e93"},"content":{"rendered":"<section id=\"wenyan\" style=\"font-family: system-ui, 'Apple Color Emoji', 'Segoe UI', 'Segoe UI Symbol', 'Noto Sans', Roboto, sans-serif; line-height: 1.75; font-size: 16px; color: #000000; caret-color: #000000;\" data-provider=\"WenYan\">\n<blockquote style=\"margin: 20px 0px; display: block; font-size: 0.9em; overflow: auto; border-left: 3px solid #ef7060; color: #6a737d; padding: 10px 10px 10px 20px; background: #fff9f9;\">\n<p style=\"margin: 1em 0px;\">\u4e00\u76f4\u60f3\u5bf9Android\u7f51\u7edc\u8bf7\u6c42\u8fdb\u884c\u6574\u7406\uff0c\u521a\u597d\u770b\u5230\u8fd9\u7bc7\u6587\u7ae0\uff0c\u5206\u4eab\u5982\u4e0b\u5e0c\u671b\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9.<\/p>\n<\/blockquote>\n<p><!--more--><\/p>\n<p style=\"margin: 1em 0px;\">\u4e00\u76f4\u60f3\u5bf9Android\u7f51\u7edc\u8bf7\u6c42\u8fdb\u884c\u6574\u7406\uff0c\u521a\u597d\u770b\u5230\u8fd9\u7bc7\u6587\u7ae0\uff0c\u5206\u4eab\u5982\u4e0b\u5e0c\u671b\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9.<\/p>\n<h2 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid #ef7060; display: flex;\"><span style=\"display: inline-block; font-weight: bold; background: #ef7060; color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;\">HttpUrlConnection<\/span><\/h2>\n<p style=\"margin: 1em 0px;\">\u6700\u5f00\u59cb\u5b66android\u7684\u65f6\u5019\u7528\u7684\u7f51\u7edc\u8bf7\u6c42\u662fHttpUrlConnection\uff0c\u5f53\u65f6\u5f88\u591a\u4e1c\u897f\u8fd8\u4e0d\u77e5\u9053\uff0c\u4f46\u662f\u5728android 2.2\u53ca\u4ee5\u4e0b\u7248\u672c\u4e2dHttpUrlConnection\u5b58\u5728\u7740\u4e00\u4e9bbug\uff0c\u6240\u4ee5\u5efa\u8bae\u5728android 2.3\u4ee5\u540e\u4f7f\u7528HttpUrlConnection\uff0c\u4e4b\u524d\u4f7f\u7528HttpClient\u3002<\/p>\n<blockquote style=\"margin: 20px 0px; display: block; font-size: 0.9em; overflow: auto; border-left: 3px solid #ef7060; color: #6a737d; padding: 10px 10px 10px 20px; background: #fff9f9;\">\n<p style=\"margin: 1em 0px;\">\u5728Android 2.2\u7248\u672c\u4e4b\u524d\uff0cHttpClient\u62e5\u6709\u8f83\u5c11\u7684bug\uff0c\u56e0\u6b64\u4f7f\u7528\u5b83\u662f\u6700\u597d\u7684\u9009\u62e9\u3002\u800c\u5728Android 2.3\u7248\u672c\u53ca\u4ee5\u540e\uff0cHttpURLConnection\u5219\u662f\u6700\u4f73\u7684\u9009\u62e9\u3002\u5b83\u7684API\u7b80\u5355\uff0c\u4f53\u79ef\u8f83\u5c0f\uff0c\u56e0\u800c\u975e\u5e38\u9002\u7528\u4e8eAndroid\u9879\u76ee\u3002\u538b\u7f29\u548c\u7f13\u5b58\u673a\u5236\u53ef\u4ee5\u6709\u6548\u5730\u51cf\u5c11\u7f51\u7edc\u8bbf\u95ee\u7684\u6d41\u91cf\uff0c\u5728\u63d0\u5347\u901f\u5ea6\u548c\u7701\u7535\u65b9\u9762\u4e5f\u8d77\u5230\u4e86\u8f83\u5927\u7684\u4f5c\u7528\u3002\u5bf9\u4e8e\u65b0\u7684\u5e94\u7528\u7a0b\u5e8f\u5e94\u8be5\u66f4\u52a0\u504f\u5411\u4e8e\u4f7f\u7528HttpURLConnection\uff0c\u56e0\u4e3a\u5728\u4ee5\u540e\u7684\u5de5\u4f5c\u5f53\u4e2d\u6211\u4eec\u4e5f\u4f1a\u5c06\u66f4\u591a\u7684\u65f6\u95f4\u653e\u5728\u4f18\u5316HttpURLConnection\u4e0a\u9762\u3002<\/p>\n<\/blockquote>\n<h3 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em;\">\u7279\u70b9<\/h3>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section>\u6bd4\u8f83\u8f7b\u4fbf\uff0c\u7075\u6d3b\uff0c\u6613\u4e8e\u6269\u5c55<\/section>\n<\/li>\n<li>\n<section>\u57283.0\u540e\u4ee5\u53ca4.0\u4e2d\u90fd\u8fdb\u884c\u4e86\u6539\u5584\uff0c\u5982\u5bf9HTTPS\u7684\u652f\u6301<\/section>\n<\/li>\n<li>\n<section>\u57284.0\u4e2d\uff0c\u8fd8\u589e\u52a0\u4e86\u5bf9\u7f13\u5b58\u7684\u652f\u6301<\/section>\n<\/li>\n<\/ul>\n<h3 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em;\">\u7528\u6cd5<\/h3>\n<ol style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem;\">\n<li>\n<section>\u9996\u5148\u6211\u4eec\u9700\u8981\u83b7\u53d6\u5230\u4e00\u4e2aHttpURLConnection\u5b9e\u4f8b\uff0c\u4e00\u822c\u9700\u8981new\u51fa\u4e00\u4e2aURL\u5bf9\u8c61\uff0c\u5e76\u4f20\u5165\u76ee\u6807\u7f51\u7edc\u5730\u5740\uff0c\u901a\u8fc7\u8c03\u7528openConnection()\u65b9\u6cd5\u83b7\u5f97HttpURLConnection\u5b9e\u4f8b\u3002<\/section>\n<\/li>\n<li>\n<section>\u5f97\u5230\u8be5\u5b9e\u4f8b\u540e\u3002\u6211\u4eec\u9700\u8981\u8bbe\u7f6e\u4e00\u4e0bhttp\u8bf7\u6c42\u7684\u7684\u65b9\u6cd5\uff0c\u8fd9\u91cc\u6211\u4eec\u4e3b\u8981\u7814\u7a76get\u548cpost\uff0c\u9ed8\u8ba4\u662f\u4f7f\u7528get\u65b9\u6cd5\u3002get\u4e00\u822c\u7528\u4e8e\u4ece\u670d\u52a1\u5668\u83b7\u53d6\u6570\u636e\uff0cpost\u4e00\u822c\u7528\u4e8e\u5411\u670d\u52a1\u5668\u63d0\u4ea4\u6570\u636e\uff0c\u8bbe\u7f6e\u8bf7\u6c42\u65b9\u6cd5\u4f7f\u7528\u51fd\u6570setRequestMethod(\u201cPOST\u201d)\u8fdb\u884c\u8bbe\u7f6e\u3002<\/section>\n<\/li>\n<li>\n<section>\u6b64\u5916\u53ef\u4ee5\u8fdb\u884c\u4e00\u4e9b\u8bf7\u6c42\u7684\u9650\u5236\uff0c\u6bd4\u5982\u8fde\u63a5\u8d85\u65f6\u7684\u65f6\u95f4\u7b49\uff0c\u53ef\u4ee5\u901a\u8fc7setConnectTimeout\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\u3002<\/section>\n<\/li>\n<li>\n<section>\u83b7\u53d6\u670d\u52a1\u5668\u8fd4\u56de\u7684\u8f93\u5165\u6d41\uff0c\u4f7f\u7528getInputStream\u65b9\u6cd5\u83b7\u53d6\u3002<\/section>\n<\/li>\n<li>\n<section>\u8bfb\u53d6\u5185\u5bb9\u5e76\u5904\u7406<\/section>\n<\/li>\n<li>\n<section>\u5173\u95ed\u8fde\u63a5\uff0c\u901a\u8fc7\u8c03\u7528disconnect\u65b9\u6cd5\u5173\u95ed\u5f53\u524d\u7684\u8fde\u63a5\u3002<br \/>\n\u5173\u952e\u4ee3\u7801\u5982\u4e0b<br \/>\n\u4f7f\u7528\u8fc7\u7a0b\u4e2d\u4e0d\u8981\u5fd8\u8bb0\u6dfb\u52a0\u6743\u9650<\/section>\n<\/li>\n<\/ol>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">  &lt;uses-permission android:<span class=\"hljs-property\">name<\/span>=<span class=\"hljs-string\">\"android.permission.INTERNET\"<\/span><\/code><\/pre>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section>GET<\/section>\n<\/li>\n<\/ul>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">   <span class=\"hljs-keyword\">public<\/span> String <span class=\"hljs-title\">get<\/span>(String urlPath) {\n        HttpURLConnection connection = <span class=\"hljs-keyword\">null<\/span>;\n        InputStream <span class=\"hljs-keyword\">is<\/span> = <span class=\"hljs-keyword\">null<\/span>;\n        <span class=\"hljs-keyword\">try<\/span> {\n            URL url = <span class=\"hljs-keyword\">new<\/span> URL(urlPath);\n            <span class=\"hljs-comment\">\/\/\u83b7\u5f97URL\u5bf9\u8c61<\/span>\n            connection = (HttpURLConnection) url.openConnection();\n            <span class=\"hljs-comment\">\/\/\u83b7\u5f97HttpURLConnection\u5bf9\u8c61<\/span>\n            connection.setRequestMethod(<span class=\"hljs-string\">\"GET\"<\/span>);\n            <span class=\"hljs-comment\">\/\/ \u9ed8\u8ba4\u4e3aGET<\/span>\n            connection.setUseCaches(<span class=\"hljs-keyword\">false<\/span>);\n            <span class=\"hljs-comment\">\/\/\u4e0d\u4f7f\u7528\u7f13\u5b58<\/span>\n            connection.setConnectTimeout(<span class=\"hljs-number\">10000<\/span>);\n            <span class=\"hljs-comment\">\/\/\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4<\/span>\n            connection.setReadTimeout(<span class=\"hljs-number\">10000<\/span>);\n            <span class=\"hljs-comment\">\/\/\u8bbe\u7f6e\u8bfb\u53d6\u8d85\u65f6\u65f6\u95f4<\/span>\n            connection.setDoInput(<span class=\"hljs-keyword\">true<\/span>);\n            <span class=\"hljs-comment\">\/\/\u8bbe\u7f6e\u662f\u5426\u4ecehttpUrlConnection\u8bfb\u5165\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u662ftrue;<\/span>\n            <span class=\"hljs-keyword\">if<\/span> (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {\n                <span class=\"hljs-comment\">\/\/\u76f8\u5e94\u7801\u662f\u5426\u4e3a200<\/span>\n                <span class=\"hljs-keyword\">is<\/span> = connection.getInputStream();\n                <span class=\"hljs-comment\">\/\/\u83b7\u5f97\u8f93\u5165\u6d41<\/span>\n                BufferedReader reader = <span class=\"hljs-keyword\">new<\/span> BufferedReader(<span class=\"hljs-keyword\">new<\/span> InputStreamReader(<span class=\"hljs-keyword\">is<\/span>));\n                <span class=\"hljs-comment\">\/\/\u5305\u88c5\u5b57\u8282\u6d41\u4e3a\u5b57\u7b26\u6d41<\/span>\n                StringBuilder response = <span class=\"hljs-keyword\">new<\/span> StringBuilder();\n                String line;\n                <span class=\"hljs-keyword\">while<\/span> ((line = reader.readLine()) != <span class=\"hljs-keyword\">null<\/span>) {\n                    response.append(line);\n                }\n                <span class=\"hljs-keyword\">return<\/span> response.toString();\n            }\n        } <span class=\"hljs-keyword\">catch<\/span> (Exception e) {\n            e.printStackTrace();\n        } <span class=\"hljs-keyword\">finally<\/span> {\n            <span class=\"hljs-keyword\">if<\/span> (connection != <span class=\"hljs-keyword\">null<\/span>) {\n                connection.disconnect();\n                connection = <span class=\"hljs-keyword\">null<\/span>;\n            }\n            <span class=\"hljs-keyword\">if<\/span> (<span class=\"hljs-keyword\">is<\/span> != <span class=\"hljs-keyword\">null<\/span>) {\n                <span class=\"hljs-keyword\">try<\/span> {\n                    <span class=\"hljs-keyword\">is<\/span>.close();\n                    <span class=\"hljs-keyword\">is<\/span> = <span class=\"hljs-keyword\">null<\/span>;\n                } <span class=\"hljs-keyword\">catch<\/span> (IOException e) {\n                    e.printStackTrace();\n                }\n            }\n        }\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">null<\/span>;\n    }<\/code><\/pre>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section>POST<\/section>\n<\/li>\n<\/ul>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\"> <span class=\"hljs-keyword\">private<\/span> String <span class=\"hljs-title\">post<\/span>(String urlPath, Map&lt;String, String&gt; <span class=\"hljs-keyword\">params<\/span>) {\n        <span class=\"hljs-keyword\">if<\/span> (<span class=\"hljs-keyword\">params<\/span> == <span class=\"hljs-keyword\">null<\/span> || <span class=\"hljs-keyword\">params<\/span>.size() == <span class=\"hljs-number\">0<\/span>) {\n            <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">get<\/span>(urlPath);\n        }\n        OutputStream os = <span class=\"hljs-keyword\">null<\/span>;\n        InputStream <span class=\"hljs-keyword\">is<\/span> = <span class=\"hljs-keyword\">null<\/span>;\n        HttpURLConnection connection = <span class=\"hljs-keyword\">null<\/span>;\n        StringBuffer body = getParamString(<span class=\"hljs-keyword\">params<\/span>);\n        <span class=\"hljs-keyword\">byte<\/span>[] data = body.toString().getBytes();\n        <span class=\"hljs-keyword\">try<\/span> {\n            URL url = <span class=\"hljs-keyword\">new<\/span> URL(urlPath);\n            <span class=\"hljs-comment\">\/\/\u83b7\u5f97URL\u5bf9\u8c61<\/span>\n            connection = (HttpURLConnection) url.openConnection();\n            <span class=\"hljs-comment\">\/\/\u83b7\u5f97HttpURLConnection\u5bf9\u8c61<\/span>\n            connection.setRequestMethod(<span class=\"hljs-string\">\"POST\"<\/span>);\n            <span class=\"hljs-comment\">\/\/ \u8bbe\u7f6e\u8bf7\u6c42\u65b9\u6cd5\u4e3apost<\/span>\n            connection.setUseCaches(<span class=\"hljs-keyword\">false<\/span>);\n            <span class=\"hljs-comment\">\/\/\u4e0d\u4f7f\u7528\u7f13\u5b58<\/span>\n            connection.setConnectTimeout(<span class=\"hljs-number\">10000<\/span>);\n            <span class=\"hljs-comment\">\/\/\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4<\/span>\n            connection.setReadTimeout(<span class=\"hljs-number\">10000<\/span>);\n            <span class=\"hljs-comment\">\/\/\u8bbe\u7f6e\u8bfb\u53d6\u8d85\u65f6\u65f6\u95f4<\/span>\n            connection.setDoInput(<span class=\"hljs-keyword\">true<\/span>);\n            <span class=\"hljs-comment\">\/\/\u8bbe\u7f6e\u662f\u5426\u4ecehttpUrlConnection\u8bfb\u5165\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u662ftrue;<\/span>\n            connection.setDoOutput(<span class=\"hljs-keyword\">true<\/span>);\n            <span class=\"hljs-comment\">\/\/\u8bbe\u7f6e\u4e3atrue\u540e\u624d\u80fd\u5199\u5165\u53c2\u6570<\/span>\n            connection.setRequestProperty(<span class=\"hljs-string\">\"Content-Type\"<\/span>, <span class=\"hljs-string\">\"application\/x-www-form-urlencoded\"<\/span>);\n            connection.setRequestProperty(<span class=\"hljs-string\">\"Content-Length\"<\/span>, String.valueOf(data.length));\n            os = connection.getOutputStream();\n            os.write(data);\n            <span class=\"hljs-comment\">\/\/\u5199\u5165\u53c2\u6570<\/span>\n            <span class=\"hljs-keyword\">if<\/span> (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {\n                <span class=\"hljs-comment\">\/\/\u76f8\u5e94\u7801\u662f\u5426\u4e3a200<\/span>\n                <span class=\"hljs-keyword\">is<\/span> = connection.getInputStream();\n                <span class=\"hljs-comment\">\/\/\u83b7\u5f97\u8f93\u5165\u6d41<\/span>\n                BufferedReader reader = <span class=\"hljs-keyword\">new<\/span> BufferedReader(<span class=\"hljs-keyword\">new<\/span> InputStreamReader(<span class=\"hljs-keyword\">is<\/span>));\n                <span class=\"hljs-comment\">\/\/\u5305\u88c5\u5b57\u8282\u6d41\u4e3a\u5b57\u7b26\u6d41<\/span>\n                StringBuilder response = <span class=\"hljs-keyword\">new<\/span> StringBuilder();\n                String line;\n                <span class=\"hljs-keyword\">while<\/span> ((line = reader.readLine()) != <span class=\"hljs-keyword\">null<\/span>) {\n                    response.append(line);\n                }\n                <span class=\"hljs-keyword\">return<\/span> response.toString();\n            }\n        } <span class=\"hljs-keyword\">catch<\/span> (Exception e) {\n            e.printStackTrace();\n        } <span class=\"hljs-keyword\">finally<\/span> {\n            <span class=\"hljs-comment\">\/\/\u5173\u95ed<\/span>\n            <span class=\"hljs-keyword\">if<\/span> (os != <span class=\"hljs-keyword\">null<\/span>) {\n                <span class=\"hljs-keyword\">try<\/span> {\n                    os.close();\n                } <span class=\"hljs-keyword\">catch<\/span> (IOException e) {\n                    e.printStackTrace();\n                }\n            }\n            <span class=\"hljs-keyword\">if<\/span> (<span class=\"hljs-keyword\">is<\/span> != <span class=\"hljs-keyword\">null<\/span>) {\n                <span class=\"hljs-keyword\">try<\/span> {\n                    <span class=\"hljs-keyword\">is<\/span>.close();\n                } <span class=\"hljs-keyword\">catch<\/span> (IOException e) {\n                    e.printStackTrace();\n                }\n            }\n            <span class=\"hljs-keyword\">if<\/span> (connection != <span class=\"hljs-keyword\">null<\/span>) {\n                connection.disconnect();\n                connection = <span class=\"hljs-keyword\">null<\/span>;\n            }\n        }\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">null<\/span>;\n    }\n\n    <span class=\"hljs-keyword\">private<\/span> StringBuffer <span class=\"hljs-title\">getParamString<\/span>(Map&lt;String, String&gt; <span class=\"hljs-keyword\">params<\/span>) {\n        StringBuffer result = <span class=\"hljs-keyword\">new<\/span> StringBuffer();\n        Iterator&lt;Map.Entry&lt;String, String&gt;&gt; iterator = <span class=\"hljs-keyword\">params<\/span>.entrySet().iterator();\n        <span class=\"hljs-keyword\">while<\/span> (iterator.hasNext()) {\n            Map.Entry&lt;String, String&gt; param = iterator.next();\n            String key = param.getKey();\n            String <span class=\"hljs-keyword\">value<\/span> = param.getValue();\n            result.append(key).append(<span class=\"hljs-string\">'='<\/span>).append(<span class=\"hljs-keyword\">value<\/span>);\n            <span class=\"hljs-keyword\">if<\/span> (iterator.hasNext()) {\n                result.append(<span class=\"hljs-string\">'&amp;'<\/span>);\n            }\n        }\n        <span class=\"hljs-keyword\">return<\/span> result;\n    }<\/code><\/pre>\n<blockquote style=\"margin: 20px 0px; display: block; font-size: 0.9em; overflow: auto; border-left: 3px solid #ef7060; color: #6a737d; padding: 10px 10px 10px 20px; background: #fff9f9;\">\n<p style=\"margin: 1em 0px;\">\u4ee5\u4e0a\u4ee3\u7801\u53c2\u8003\u4e86\u90e8\u5206<a href=\"https:\/\/github.com\/openproject\/LessCode\">LessCode\u9879\u76ee<\/a><\/p>\n<\/blockquote>\n<h2 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid #ef7060; display: flex;\"><span style=\"display: inline-block; font-weight: bold; background: #ef7060; color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;\">HttpClient<\/span><\/h2>\n<h3 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em;\">\u7279\u70b9<\/h3>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section>\u9ad8\u6548\u7a33\u5b9a\uff0c\u4f46\u662f\u7ef4\u62a4\u6210\u672c\u9ad8\u6602\uff0c\u6545android \u5f00\u53d1\u56e2\u961f\u4e0d\u613f\u610f\u5728\u7ef4\u62a4\u8be5\u5e93\u800c\u662f\u8f6c\u6295\u66f4\u4e3a\u8f7b\u4fbf\u7684HttpUrlConnection<\/section>\n<\/li>\n<\/ul>\n<h3 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em;\">\u7528\u6cd5<\/h3>\n<ol style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem;\">\n<li>\n<section>HttpClient\u662f\u4e00\u4e2a\u63a5\u53e3\uff0c\u56e0\u6b64\u65e0\u6cd5\u76f4\u63a5\u521b\u5efa\u5b83\u7684\u5b9e\u4f8b\uff0c\u4e00\u822c\u90fd\u662f\u521b\u5efa\u4e00\u4e2aDefaultHttpClient\u5b9e\u4f8b<\/section>\n<\/li>\n<li>\n<section>\u5982\u679c\u8981\u53d1\u8d77Get\u8bf7\u6c42\uff0c\u9700\u8981\u521b\u5efa\u4e00\u4e2aHttpGet\u5bf9\u8c61\uff0c\u5e76\u4f20\u5165\u8bf7\u6c42\u5730\u5740<\/section>\n<\/li>\n<li>\n<section>\u5982\u679c\u8981\u53d1\u8d77Post\u8bf7\u6c42\uff0c\u9700\u8981\u521b\u5efa\u4e00\u4e2aHttpPost\u5bf9\u8c61\u3002\u5e76\u4f20\u5165\u8bf7\u6c42\u5730\u5740\uff0c\u901a\u8fc7setEntity\u51fd\u6570\u8bbe\u7f6e\u8bf7\u6c42\u53c2\u6570<\/section>\n<\/li>\n<li>\n<section>\u8c03\u7528execute\u65b9\u6cd5\uff0c\u4f20\u5165HttpGet\u6216\u8005HttpPost\u5b9e\u4f8b\uff0c\u6267\u884c\u540e\u8fd4\u56deHttpResponse\u5bf9\u8c61\uff0c\u5224\u65ad\u54cd\u5e94\u72b6\u6001\u7801<\/section>\n<\/li>\n<li>\n<section>\u89e3\u6790\u54cd\u5e94\u7ed3\u679c\uff0c\u901a\u8fc7\u8c03\u7528getEntity\u51fd\u6570\u83b7\u5f97\u4e00\u4e2aHttpEntity\u5bf9\u8c61\uff0c\u4e4b\u540e\u53ef\u4ee5\u901a\u8fc7EntityUtils.toString\u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/section>\n<\/li>\n<\/ol>\n<p style=\"margin: 1em 0px;\">\u7531\u4e8e\u5728android2.3\u4e4b\u540e\u5c31\u88abHttpUrlConnection\u53d6\u4ee3\u4e86\uff0c\u8fd9\u91cc\u4e5f\u4e0d\u8fc7\u591a\u4ecb\u7ecd\u4e86\uff0c\u4e0d\u8fc7\u5f53\u521d\u5b66\u4e60\u5b83\u7684\u65f6\u5019\u8fd8\u6ca1\u63a5\u89e6\u5230\u5176\u4ed6\u5e93\uff0c\u5c31\u611f\u89c9\u5b83\u597d\u65b9\u4fbf\uff0c\u4e0b\u9762\u7b80\u5355\u8d34\u51fa\u4f7f\u7528\u65b9\u6cd5<\/p>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section>GET<\/section>\n<\/li>\n<\/ul>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">    <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-built_in\">String<\/span> <span class=\"hljs-keyword\">get<\/span>(<span class=\"hljs-built_in\">String<\/span> url){\n        HttpClient client=<span class=\"hljs-literal\">null<\/span>;\n        HttpGet <span class=\"hljs-built_in\">request<\/span>=<span class=\"hljs-literal\">null<\/span>;\n        try {\n            client=<span class=\"hljs-keyword\">new<\/span> DefaultHttpClient();\n            <span class=\"hljs-built_in\">request<\/span>=<span class=\"hljs-keyword\">new<\/span> HttpGet(url);\n            HttpResponse <span class=\"hljs-built_in\">response<\/span>=client.<span class=\"hljs-keyword\">execute<\/span>(<span class=\"hljs-built_in\">request<\/span>);\n            <span class=\"hljs-keyword\">if<\/span>(<span class=\"hljs-built_in\">response<\/span>.getStatusLine().getStatusCode()== HttpStatus.SC_OK){\n                <span class=\"hljs-built_in\">String<\/span> result=EntityUtils.toString(<span class=\"hljs-built_in\">response<\/span>.getEntity(),<span class=\"hljs-string\">\"UTF-8\"<\/span>);\n                return result;\n            }\n        } catch (IOException e) {\n            e.printStackTrace();\n        }\n        return  <span class=\"hljs-literal\">null<\/span>;\n    }<\/code><\/pre>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section>POST<\/section>\n<\/li>\n<\/ul>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\"> <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-built_in\">String<\/span> post(<span class=\"hljs-built_in\">String<\/span> url,List&lt;NameValuePair&gt; params){\n        HttpClient client=<span class=\"hljs-literal\">null<\/span>;\n        HttpPost <span class=\"hljs-built_in\">request<\/span>=<span class=\"hljs-literal\">null<\/span>;\n        try {\n            client=<span class=\"hljs-keyword\">new<\/span> DefaultHttpClient();\n            <span class=\"hljs-built_in\">request<\/span>=<span class=\"hljs-keyword\">new<\/span> HttpPost(url);\n            <span class=\"hljs-built_in\">request<\/span>.setEntity(<span class=\"hljs-keyword\">new<\/span> UrlEncodedFormEntity(params, HTTP.UTF_8));\n            HttpResponse <span class=\"hljs-built_in\">response<\/span>=client.<span class=\"hljs-keyword\">execute<\/span>(<span class=\"hljs-built_in\">request<\/span>);\n            <span class=\"hljs-keyword\">if<\/span>(<span class=\"hljs-built_in\">response<\/span>.getStatusLine().getStatusCode()== HttpStatus.SC_OK){\n                <span class=\"hljs-built_in\">String<\/span> result=EntityUtils.toString(<span class=\"hljs-built_in\">response<\/span>.getEntity(),<span class=\"hljs-string\">\"UTF-8\"<\/span>);\n                return result;\n            }\n        } catch (IOException e) {\n            e.printStackTrace();\n        }\n        return  <span class=\"hljs-literal\">null<\/span>;\n    }<\/code><\/pre>\n<blockquote style=\"margin: 20px 0px; display: block; font-size: 0.9em; overflow: auto; border-left: 3px solid #ef7060; color: #6a737d; padding: 10px 10px 10px 20px; background: #fff9f9;\">\n<p style=\"margin: 1em 0px;\">\u4ee5\u4e0a\u4ee3\u7801\u53c2\u8003\u4e86\u90ed\u9716\u300a\u7b2c\u4e00\u884c\u4ee3\u7801\u300b\u2014\u2014HttpClient\u90e8\u5206<\/p>\n<\/blockquote>\n<h2 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid #ef7060; display: flex;\"><span style=\"display: inline-block; font-weight: bold; background: #ef7060; color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;\">Android Asynchronous Http Client<\/span><\/h2>\n<p style=\"margin: 1em 0px;\">Android Asynchronous Http Client\u4e00\u770b\u540d\u5b57\u5c31\u77e5\u9053\u5b83\u662f\u57fa\u4e8eHttp Client\u7684\uff0c\u4f46\u662f\u5462\u5728\u5b89\u5353\u4e2dHttp Client\u5df2\u7ecf\u5e9f\u5f03\u4e86\uff0c\u6240\u4ee5\u4e5f\u4e0d\u5efa\u8bae\u4f7f\u7528\u8fd9\u4e2a\u5e93\u4e86\u3002\u7136\u540e\u4ecd\u7136\u6709\u4e00\u4e9b\u53ef\u53d6\u7684\u5185\u5bb9\u503c\u5f97\u5b66\u4e60\uff0c\u6240\u4ee5\u8fd9\u91cc\u4e5f\u4ecb\u7ecd\u4e00\u4e0b\u3002<\/p>\n<h3 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em;\">\u7279\u70b9<\/h3>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section>\u6240\u4ee5\u8bf7\u6c42\u5728\u5b50\u7ebf\u7a0b\u4e2d\u5b8c\u6210\uff0c\u8bf7\u6c42\u56de\u8c03\u5728\u8c03\u7528\u8be5\u8bf7\u6c42\u7684\u7ebf\u7a0b\u4e2d\u5b8c\u6210<\/section>\n<\/li>\n<li>\n<section>\u4f7f\u7528\u7ebf\u7a0b\u6c60<\/section>\n<\/li>\n<li>\n<section>\u4f7f\u7528RequestParams\u7c7b\u5c01\u88c5\u8bf7\u6c42\u53c2\u6570<\/section>\n<\/li>\n<li>\n<section>\u652f\u6301\u6587\u4ef6\u4e0a\u4f20<\/section>\n<\/li>\n<li>\n<section>\u6301\u4e45\u5316cookie\u5230SharedPreferences\uff0c\u4e2a\u4eba\u611f\u89c9\u8fd9\u4e00\u70b9\u4e5f\u662f\u8fd9\u4e2a\u5e93\u7684\u91cd\u8981\u7279\u70b9\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u7684\u5b8c\u6210\u4e00\u4e9b\u6a21\u62df\u767b\u5f55<\/section>\n<\/li>\n<li>\n<section>\u652f\u6301json<\/section>\n<\/li>\n<li>\n<section>\u652f\u6301HTTP Basic Auth<\/section>\n<\/li>\n<\/ul>\n<h3 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em;\">\u7528\u6cd5<\/h3>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section>\u7f16\u5199\u4e00\u4e2a\u9759\u6001\u7684HttpClient<\/section>\n<\/li>\n<\/ul>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\"><span class=\"hljs-keyword\">package<\/span> cn.edu.zafu.http;\n\n<span class=\"hljs-keyword\">import<\/span> com.loopj.android.http.AsyncHttpClient;\n<span class=\"hljs-keyword\">import<\/span> com.loopj.android.http.AsyncHttpResponseHandler;\n<span class=\"hljs-keyword\">import<\/span> com.loopj.android.http.RequestParams;\n\n<span class=\"hljs-javadoc\">\/**\n * Created by lizhangqu on 2015\/5\/7.\n *\/<\/span>\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">TestClient<\/span> {<\/span>\n    <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">final<\/span> String BASE_URL = <span class=\"hljs-string\">\"http:\/\/121.41.119.107\/\"<\/span>;\n\n    <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">static<\/span> AsyncHttpClient client = <span class=\"hljs-keyword\">new<\/span> AsyncHttpClient();\n\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">get<\/span>(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {\n        client.get(getAbsoluteUrl(url), params, responseHandler);\n    }\n\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">post<\/span>(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {\n        client.post(getAbsoluteUrl(url), params, responseHandler);\n    }\n\n    <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">static<\/span> String <span class=\"hljs-title\">getAbsoluteUrl<\/span>(String relativeUrl) {\n        <span class=\"hljs-keyword\">return<\/span> BASE_URL + relativeUrl;\n    }\n}<\/code><\/pre>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section>\u8c03\u7528get\u6216\u8005post\u65b9\u6cd5<\/section>\n<\/li>\n<\/ul>\n<p style=\"margin: 1em 0px;\">\u53c2\u6570\u901a\u8fc7RequestParams\u4f20\u9012\uff0c\u6ca1\u6709\u53c2\u6570\u5219\u4f20\u9012null<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">RequestParams  <span class=\"hljs-keyword\">params<\/span> = <span class=\"hljs-keyword\">new<\/span> RequestParams();\n<span class=\"hljs-keyword\">params<\/span>.put(<span class=\"hljs-string\">\"\"<\/span>,<span class=\"hljs-string\">\"\"<\/span>);<\/code><\/pre>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section>\u5982\u679c\u8981\u4fdd\u5b58cookie\uff0c\u5728\u53d1\u8d77\u8bf7\u6c42\u4e4b\u524d\u8c03\u7528\u4ee5\u4e0b\u4ee3\u7801<\/section>\n<\/li>\n<\/ul>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">PersistentCookieStore myCookieStore = <span class=\"hljs-keyword\">new<\/span> PersistentCookieStore(<span class=\"hljs-keyword\">this<\/span>);\n<span class=\"hljs-keyword\">client<\/span>.setCookieStore(myCookieStore);<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u4e4b\u540e\u8bf7\u6c42\u6240\u5f97\u5230\u7684cookie\u90fd\u4f1a\u81ea\u52a8\u6301\u4e45\u5316<\/p>\n<p style=\"margin: 1em 0px;\">\u5982\u679c\u8981\u81ea\u5df1\u6dfb\u52a0cookie\uff0c\u5219\u8c03\u7528\u4ee5\u4e0b\u4ee3\u7801<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">BasicClientCookie newCookie = new BasicClientCookie(<span class=\"hljs-string\">\"cookiesare\"<\/span>, <span class=\"hljs-string\">\"awesome\"<\/span>)<span class=\"hljs-comment\">;<\/span>\nnewCookie<span class=\"hljs-preprocessor\">.setVersion<\/span>(<span class=\"hljs-number\">1<\/span>)<span class=\"hljs-comment\">;<\/span>\nnewCookie<span class=\"hljs-preprocessor\">.setDomain<\/span>(<span class=\"hljs-string\">\"mydomain.com\"<\/span>)<span class=\"hljs-comment\">;<\/span>\nnewCookie<span class=\"hljs-preprocessor\">.setPath<\/span>(<span class=\"hljs-string\">\"\/\"<\/span>)<span class=\"hljs-comment\">;<\/span>\nmyCookieStore<span class=\"hljs-preprocessor\">.addCookie<\/span>(newCookie)<span class=\"hljs-comment\">;<\/span><\/code><\/pre>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section>\u4f7f\u7528<br \/>\n\u5728\u56de\u8c03\u51fd\u6570\u4e2d\u5904\u7406\u8fd4\u56de\u7ed3\u679c<\/section>\n<\/li>\n<\/ul>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\"><span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">get<\/span>(){\n        TestClient.<span class=\"hljs-keyword\">get<\/span>(<span class=\"hljs-string\">\"test\/index.php\"<\/span>, <span class=\"hljs-keyword\">null<\/span>, <span class=\"hljs-keyword\">new<\/span> AsyncHttpResponseHandler() {\n            @Override\n            <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">onSuccess<\/span>(<span class=\"hljs-keyword\">int<\/span> statusCode, Header[] headers, <span class=\"hljs-keyword\">byte<\/span>[] responseBody) {\n\n            }\n\n            @Override\n            <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">onFailure<\/span>(<span class=\"hljs-keyword\">int<\/span> statusCode, Header[] headers, <span class=\"hljs-keyword\">byte<\/span>[] responseBody, Throwable error) {\n\n            }\n        });\n    }\n    <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">post<\/span>(){\n        RequestParams <span class=\"hljs-keyword\">params<\/span> = <span class=\"hljs-keyword\">new<\/span> RequestParams();\n        <span class=\"hljs-keyword\">params<\/span>.put(<span class=\"hljs-string\">\"user\"<\/span>,<span class=\"hljs-string\">\"asas\"<\/span>);\n        <span class=\"hljs-keyword\">params<\/span>.put(<span class=\"hljs-string\">\"pass\"<\/span>,<span class=\"hljs-string\">\"12121\"<\/span>);\n        <span class=\"hljs-keyword\">params<\/span>.put(<span class=\"hljs-string\">\"time\"<\/span>,<span class=\"hljs-string\">\"1212121\"<\/span>);\n        TestClient.post(<span class=\"hljs-string\">\"test\/login.php\"<\/span>, <span class=\"hljs-keyword\">params<\/span>, <span class=\"hljs-keyword\">new<\/span> AsyncHttpResponseHandler() {\n            @Override\n            <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">onSuccess<\/span>(<span class=\"hljs-keyword\">int<\/span> statusCode, Header[] headers, <span class=\"hljs-keyword\">byte<\/span>[] responseBody) {\n\n            }\n\n            @Override\n            <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">onFailure<\/span>(<span class=\"hljs-keyword\">int<\/span> statusCode, Header[] headers, <span class=\"hljs-keyword\">byte<\/span>[] responseBody, Throwable error) {\n\n            }\n        });\n    }<\/code><\/pre>\n<blockquote style=\"margin: 20px 0px; display: block; font-size: 0.9em; overflow: auto; border-left: 3px solid #ef7060; color: #6a737d; padding: 10px 10px 10px 20px; background: #fff9f9;\">\n<p style=\"margin: 1em 0px;\">\u4ee5\u4e0a\u4ee3\u7801\u53c2\u8003\u4e86<a href=\"http:\/\/loopj.com\/android-async-http\/\">Android Asynchronous Http Client\u5b98\u65b9\u5b9e\u4f8b<\/a><\/p>\n<\/blockquote>\n<h2 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid #ef7060; display: flex;\"><span style=\"display: inline-block; font-weight: bold; background: #ef7060; color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;\">Volley<\/span><\/h2>\n<p style=\"margin: 1em 0px;\">\u65e2\u7136\u5728android2.2\u4e4b\u540e\u4e0d\u5efa\u8bae\u4f7f\u7528Http Client\uff0c\u90a3\u4e48\u6709\u6ca1\u6709\u4e00\u4e2a\u5e93\u662fandroid2.2\u53ca\u4ee5\u4e0b\u7248\u672c\u4f7f\u7528Http Client\uff0c\u800candroid2.3\u53ca\u4ee5\u4e0a\u7248\u672c\u4f7f\u7528HttpUrlConnection\u7684\u5462\uff0c\u7b54\u6848\u662f\u80af\u5b9a\u7684\uff0c\u5c31\u662fVolley,\u5b83\u662fandroid\u5f00\u53d1\u56e2\u961f\u57282013\u5e74Google I\/O\u5927\u4f1a\u4e0a\u63a8\u51fa\u4e86\u4e00\u4e2a\u65b0\u7684\u7f51\u7edc\u901a\u4fe1\u6846\u67b6<\/p>\n<blockquote style=\"margin: 20px 0px; display: block; font-size: 0.9em; overflow: auto; border-left: 3px solid #ef7060; color: #6a737d; padding: 10px 10px 10px 20px; background: #fff9f9;\">\n<p style=\"margin: 1em 0px;\">Volley\u53ef\u4ee5\u8bf4\u662f\u628aAsyncHttpClient\u548cUniversal-Image-Loader\u7684\u4f18\u70b9\u96c6\u4e8e\u4e86\u4e00\u8eab\uff0c\u65e2\u53ef\u4ee5\u50cfAsyncHttpClient\u4e00\u6837\u975e\u5e38\u7b80\u5355\u5730\u8fdb\u884cHTTP\u901a\u4fe1\uff0c\u4e5f\u53ef\u4ee5\u50cfUniversal-Image-Loader\u4e00\u6837\u8f7b\u677e\u52a0\u8f7d\u7f51\u7edc\u4e0a\u7684\u56fe\u7247\u3002\u9664\u4e86\u7b80\u5355\u6613\u7528\u4e4b\u5916\uff0cVolley\u5728\u6027\u80fd\u65b9\u9762\u4e5f\u8fdb\u884c\u4e86\u5927\u5e45\u5ea6\u7684\u8c03\u6574\uff0c\u5b83\u7684\u8bbe\u8ba1\u76ee\u6807\u5c31\u662f\u975e\u5e38\u9002\u5408\u53bb\u8fdb\u884c\u6570\u636e\u91cf\u4e0d\u5927\uff0c\u4f46\u901a\u4fe1\u9891\u7e41\u7684\u7f51\u7edc\u64cd\u4f5c\uff0c\u800c\u5bf9\u4e8e\u5927\u6570\u636e\u91cf\u7684\u7f51\u7edc\u64cd\u4f5c\uff0c\u6bd4\u5982\u8bf4\u4e0b\u8f7d\u6587\u4ef6\u7b49\uff0cVolley\u7684\u8868\u73b0\u5c31\u4f1a\u975e\u5e38\u7cdf\u7cd5<\/p>\n<\/blockquote>\n<h3 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em;\">\u7279\u70b9<\/h3>\n<ol style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem;\">\n<li>\n<section>Volley\u7684\u4f18\u52bf\u5728\u4e8e\u5904\u7406\u5c0f\u6587\u4ef6\u7684http\u8bf7\u6c42\uff1b<\/section>\n<\/li>\n<li>\n<section>\u5728Volley\u4e2d\u4e5f\u662f\u53ef\u4ee5\u4f7f\u7528Okhttp\u4f5c\u4e3a\u4f20\u8f93\u5c42<\/section>\n<\/li>\n<li>\n<section>Volley\u5728\u5904\u7406\u9ad8\u5206\u8fa8\u7387\u7684\u2550\u255d\u2567\u0451\u538b\u7f29\u4e0a\u6709\u5f88\u597d\u7684\u652f\u6301\uff1b<\/section>\n<\/li>\n<li>\n<section>NetworkImageView\u5728GC\u7684\u4f7f\u7528\u6a21\u5f0f\u4e0a\u66f4\u52a0\u4fdd\u5b88\uff0c\u5728\u8bf7\u6c42\u6e05\u7406\u4e0a\u4e5f\u66f4\u52a0\u79ef\u6781\uff0cnetworkimageview\u4ec5\u4ec5\u4f9d\u8d56\u4e8e\u5f3a\u5927\u7684\u5185\u5b58\u5f15\u7528\uff0c\u5e76\u5f53\u4e00\u4e2a\u65b0\u8bf7\u6c42\u662f\u6765\u81eaImageView\u6216ImageView\u79bb\u5f00\u5c4f\u5e55\u65f6 \u4f1a\u6e05\u7406\u6389\u6240\u6709\u7684\u8bf7\u6c42\u6570\u636e\u3002<\/section>\n<\/li>\n<\/ol>\n<h3 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em;\">\u7528\u6cd5<\/h3>\n<ol style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem;\">\n<li>\n<section>\u521b\u5efa\u4e00\u4e2aRequestQueue\u5bf9\u8c61\u3002<\/section>\n<\/li>\n<li>\n<section>\u521b\u5efa\u4e00\u4e2aRequest\u5bf9\u8c61\u3002<\/section>\n<\/li>\n<li>\n<section>\u5c06Request\u5bf9\u8c61\u6dfb\u52a0\u5230RequestQueue\u91cc\u9762\u3002<\/section>\n<\/li>\n<\/ol>\n<p style=\"margin: 1em 0px;\">\u4e0b\u9762\u4e00\u6b65\u4e00\u6b65\u6765\u5b66\u4e60\u5176\u7528\u6cd5<\/p>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section>GET<\/section>\n<\/li>\n<\/ul>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\"> <span class=\"hljs-keyword\">private<\/span> void <span class=\"hljs-keyword\">get<\/span>(){\n        RequestQueue queue= Volley.newRequestQueue(getApplicationContext());\n        <span class=\"hljs-built_in\">String<\/span> url=<span class=\"hljs-string\">\"http:\/\/121.41.119.107\/test\/index.php\"<\/span>;\n        StringRequest <span class=\"hljs-built_in\">request<\/span>=<span class=\"hljs-keyword\">new<\/span> StringRequest(url, <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-built_in\">Response<\/span>.Listener&lt;<span class=\"hljs-built_in\">String<\/span>&gt;() {\n            @Override\n            <span class=\"hljs-keyword\">public<\/span> void onResponse(<span class=\"hljs-built_in\">String<\/span> <span class=\"hljs-built_in\">response<\/span>) {\n                <span class=\"hljs-built_in\">Log<\/span>.d(<span class=\"hljs-string\">\"TAG\"<\/span>,<span class=\"hljs-built_in\">response<\/span>);\n            }\n        }, <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-built_in\">Response<\/span>.ErrorListener() {\n            @Override\n            <span class=\"hljs-keyword\">public<\/span> void onErrorResponse(VolleyError <span class=\"hljs-keyword\">error<\/span>) {\n\n            }\n        });\n        queue.add(<span class=\"hljs-built_in\">request<\/span>);\n    }<\/code><\/pre>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section>POST<br \/>\n\u901a\u8fc7\u6307\u5b9a\u8bf7\u6c42\u65b9\u6cd5\u4e3aRequest.Method.POST\u4f7f\u5176\u6210\u4e3apost\u8bf7\u6c42\uff0c\u7136\u540e\u91cd\u65b0getParams\u65b9\u6cd5\u8bbe\u7f6e\u8bf7\u6c42\u53c2\u6570\u3002\u5f53\u53d1\u51faPOST\u8bf7\u6c42\u7684\u65f6\u5019\uff0cVolley\u4f1a\u5c1d\u8bd5\u8c03\u7528StringRequest\u7684\u7236\u7c7b\u2014\u2014Request\u4e2d\u7684getParams()\u65b9\u6cd5\u6765\u83b7\u53d6POST\u53c2\u6570<\/section>\n<\/li>\n<\/ul>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\"> <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-literal\">void<\/span> post() {\n        RequestQueue <span class=\"hljs-built_in\">queue<\/span> <span class=\"hljs-subst\">=<\/span> Volley<span class=\"hljs-built_in\">.<\/span>newRequestQueue(getApplicationContext());\n        <span class=\"hljs-built_in\">String<\/span> url <span class=\"hljs-subst\">=<\/span> <span class=\"hljs-string\">\"http:\/\/121.41.119.107\/test\/login.php\"<\/span>;\n        StringRequest request <span class=\"hljs-subst\">=<\/span> <span class=\"hljs-literal\">new<\/span> StringRequest(Request<span class=\"hljs-built_in\">.<\/span>Method<span class=\"hljs-built_in\">.<\/span>POST, url, <span class=\"hljs-literal\">new<\/span> Response<span class=\"hljs-built_in\">.<\/span>Listener<span class=\"hljs-subst\">&lt;<\/span><span class=\"hljs-built_in\">String<\/span><span class=\"hljs-subst\">&gt;<\/span>() {\n            @Override\n            <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-literal\">void<\/span> onResponse(<span class=\"hljs-built_in\">String<\/span> response) {\n                <span class=\"hljs-keyword\">Log<\/span><span class=\"hljs-built_in\">.<\/span>d(<span class=\"hljs-string\">\"TAG\"<\/span>, response);\n            }\n        }, <span class=\"hljs-literal\">new<\/span> Response<span class=\"hljs-built_in\">.<\/span>ErrorListener() {\n            @Override\n            <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-literal\">void<\/span> onErrorResponse(VolleyError error) {\n\n            }\n        }) {\n            <span class=\"hljs-comment\">\/\/\u91cd\u5199getParams\u65b9\u6cd5\u8bbe\u7f6e\u53c2\u6570<\/span>\n            @Override\n            <span class=\"hljs-keyword\">protected<\/span> <span class=\"hljs-built_in\">Map<\/span><span class=\"hljs-subst\">&lt;<\/span><span class=\"hljs-built_in\">String<\/span>, <span class=\"hljs-built_in\">String<\/span><span class=\"hljs-subst\">&gt;<\/span> getParams() throws AuthFailureError {\n                <span class=\"hljs-built_in\">Map<\/span><span class=\"hljs-subst\">&lt;<\/span><span class=\"hljs-built_in\">String<\/span>, <span class=\"hljs-built_in\">String<\/span><span class=\"hljs-subst\">&gt;<\/span> <span class=\"hljs-keyword\">params<\/span> <span class=\"hljs-subst\">=<\/span> <span class=\"hljs-literal\">new<\/span> HashMap<span class=\"hljs-subst\">&lt;<\/span><span class=\"hljs-built_in\">String<\/span>, <span class=\"hljs-built_in\">String<\/span><span class=\"hljs-subst\">&gt;<\/span>();\n                <span class=\"hljs-keyword\">params<\/span><span class=\"hljs-built_in\">.<\/span>put(<span class=\"hljs-string\">\"user\"<\/span>, <span class=\"hljs-string\">\"asas\"<\/span>);\n                <span class=\"hljs-keyword\">params<\/span><span class=\"hljs-built_in\">.<\/span>put(<span class=\"hljs-string\">\"pass\"<\/span>, <span class=\"hljs-string\">\"12121\"<\/span>);\n                <span class=\"hljs-keyword\">params<\/span><span class=\"hljs-built_in\">.<\/span>put(<span class=\"hljs-string\">\"time\"<\/span>, <span class=\"hljs-string\">\"1212121\"<\/span>);\n                <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">params<\/span>;\n            }\n        };\n        <span class=\"hljs-built_in\">queue<\/span><span class=\"hljs-built_in\">.<\/span>add(request);\n    }<\/code><\/pre>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section>\u52a0\u8f7d\u56fe\u7247<br \/>\n\u52a0\u8f7d\u2550\u255d\u2567\u0451\u7684\u65b9\u6cd5\u548c\u524d\u9762\u7c7b\u4f3c\uff0c\u53ea\u4e0d\u8fc7\u4e0d\u5728\u662fStringRequest\u800c\u662fImageRequest\u3002<\/section>\n<\/li>\n<\/ul>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\"> <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">getImage<\/span>() {\n        RequestQueue queue = Volley.newRequestQueue(getApplicationContext());\n        String url = <span class=\"hljs-string\">\"https:\/\/www.baidu.com\/img\/bdlogo.png\"<\/span>;\n        <span class=\"hljs-comment\">\/\/\u7b2c\u4e09\u7b2c\u56db\u4e2a\u53c2\u6570\u5206\u522b\u7528\u4e8e\u6307\u5b9a\u5141\u8bb8\u56fe\u7247\u6700\u5927\u7684\u5bbd\u5ea6\u548c\u9ad8\u5ea6\uff0c\u5982\u679c\u6307\u5b9a\u7684\u7f51\u7edc\u56fe\u7247\u7684\u5bbd\u5ea6\u6216\u9ad8\u5ea6\u5927\u4e8e\u8fd9\u91cc\u7684\u6700\u5927\u503c\uff0c\u5219\u4f1a\u5bf9\u56fe\u7247\u8fdb\u884c\u538b\u7f29\uff0c\u6307\u5b9a\u62100\u7684\u8bdd\u5c31\u8868\u793a\u4e0d\u7ba1\u56fe\u7247\u6709\u591a\u5927\uff0c\u90fd\u4e0d\u4f1a\u8fdb\u884c\u538b\u7f29\u3002<\/span>\n        <span class=\"hljs-comment\">\/\/\u7b2c\u4e94\u4e2a\u53c2\u6570\u5c31\u662fImageView\u91cc\u4e2d\u7684\u5c5e\u6027ScaleType<\/span>\n        <span class=\"hljs-comment\">\/\/\u7b2c\u516d\u4e2a\u53c2\u6570\u7528\u4e8e\u6307\u5b9a\u56fe\u7247\u7684\u989c\u8272\u5c5e\u6027<\/span>\n        ImageRequest request = <span class=\"hljs-keyword\">new<\/span> ImageRequest(url, <span class=\"hljs-keyword\">new<\/span> Response.Listener&lt;Bitmap&gt;() {\n            <span class=\"hljs-annotation\">@Override<\/span>\n            <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">onResponse<\/span>(Bitmap response) {\n                ImageView iv= (ImageView) findViewById(R.id.iv);\n                iv.setImageBitmap(response);\n            }\n        }, <span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">0<\/span>, ImageView.ScaleType.CENTER, Bitmap.Config.ARGB_8888, <span class=\"hljs-keyword\">new<\/span> Response.ErrorListener() {\n\n            <span class=\"hljs-annotation\">@Override<\/span>\n            <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">onErrorResponse<\/span>(VolleyError error) {\n\n            }\n        });\n        queue.add(request);\n    }<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u5176\u5b9e\u52a0\u8f7d\u56fe\u7247\u7684\u529f\u80fd\u8fd8\u8fdc\u8fdc\u4e0d\u6b62\u8fd9\u4e9b\uff0c\u4f7f\u7528ImageLoader\u53ef\u4ee5\u5b9e\u73b0\u5bf9\u56fe\u7247\u7684\u7f13\u5b58\uff0c\u8fd8\u53ef\u4ee5\u8fc7\u6ee4\u91cd\u590d\u94fe\u63a5\uff0c\u907f\u514d\u53d1\u9001\u91cd\u590d\u7684\u8bf7\u6c42 ImageLoader\u7684\u4f7f\u7528\u65b9\u6cd5\u6982\u62ec\u4e3a\u4ee5\u4e0b\u51e0\u6b65<\/p>\n<p style=\"margin: 1em 0px;\">1. \u521b\u5efa\u4e00\u4e2aRequestQueue\u5bf9\u8c61\u3002<\/p>\n<p style=\"margin: 1em 0px;\">2. \u521b\u5efa\u4e00\u4e2aImageLoader\u5bf9\u8c61\u3002<\/p>\n<p style=\"margin: 1em 0px;\">3. \u83b7\u53d6\u4e00\u4e2aImageListener\u5bf9\u8c61\u3002<\/p>\n<p style=\"margin: 1em 0px;\">4. \u8c03\u7528ImageLoader\u7684get()\u65b9\u6cd5\u52a0\u8f7d\u7f51\u7edc\u4e0a\u7684\u56fe\u7247\u3002<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\"> <span class=\"hljs-comment\">\/\/\u7ee7\u627fImageCache\uff0c\u4f7f\u7528LruCache\u5b9e\u73b0\u7f13\u5b58<\/span>\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">BitmapCache<\/span> <span class=\"hljs-keyword\">implements<\/span> <span class=\"hljs-title\">ImageLoader<\/span>.<span class=\"hljs-title\">ImageCache<\/span> {<\/span>\n        <span class=\"hljs-keyword\">private<\/span> LruCache&lt;String, Bitmap&gt; mCache;\n        <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-title\">BitmapCache<\/span>() {\n            <span class=\"hljs-keyword\">int<\/span> maxSize = <span class=\"hljs-number\">10<\/span> * <span class=\"hljs-number\">1024<\/span> * <span class=\"hljs-number\">1024<\/span>;\n            mCache = <span class=\"hljs-keyword\">new<\/span> LruCache&lt;String, Bitmap&gt;(maxSize) {\n                <span class=\"hljs-annotation\">@Override<\/span>\n                <span class=\"hljs-keyword\">protected<\/span> <span class=\"hljs-keyword\">int<\/span> <span class=\"hljs-title\">sizeOf<\/span>(String key, Bitmap bitmap) {\n                    <span class=\"hljs-keyword\">return<\/span> bitmap.getRowBytes() * bitmap.getHeight();\n                }\n            };\n        }\n        <span class=\"hljs-annotation\">@Override<\/span>\n        <span class=\"hljs-keyword\">public<\/span> Bitmap <span class=\"hljs-title\">getBitmap<\/span>(String url) {\n            <span class=\"hljs-keyword\">return<\/span> mCache.get(url);\n        }\n        <span class=\"hljs-annotation\">@Override<\/span>\n        <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">putBitmap<\/span>(String url, Bitmap bitmap) {\n            mCache.put(url, bitmap);\n        }\n\n    }\n    <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">getImageByImageLoader<\/span>() {\n        ImageView iv= (ImageView) findViewById(R.id.iv);\n        RequestQueue queue = Volley.newRequestQueue(getApplicationContext());\n        String url = <span class=\"hljs-string\">\"https:\/\/www.baidu.com\/img\/bdlogo.png\"<\/span>;\n        ImageLoader loader=<span class=\"hljs-keyword\">new<\/span> ImageLoader(queue,<span class=\"hljs-keyword\">new<\/span> BitmapCache() );\n        <span class=\"hljs-comment\">\/\/ \u7b2c\u4e00\u4e2a\u53c2\u6570\u6307\u5b9a\u7528\u4e8e\u663e\u793a\u56fe\u7247\u7684ImageView\u63a7\u4ef6<\/span>\n        <span class=\"hljs-comment\">\/\/ \u7b2c\u4e8c\u4e2a\u53c2\u6570\u6307\u5b9a\u52a0\u8f7d\u56fe\u7247\u7684\u8fc7\u7a0b\u4e2d\u663e\u793a\u7684\u56fe\u7247<\/span>\n        <span class=\"hljs-comment\">\/\/ \u7b2c\u4e09\u4e2a\u53c2\u6570\u6307\u5b9a\u52a0\u8f7d\u56fe\u7247\u5931\u8d25\u7684\u60c5\u51b5\u4e0b\u663e\u793a\u7684\u56fe\u7247<\/span>\n        ImageLoader.ImageListener listener=ImageLoader.getImageListener(iv,R.mipmap.ic_launcher,R.mipmap.ic_launcher);\n        <span class=\"hljs-comment\">\/\/ \u8c03\u7528ImageLoader\u7684get()\u65b9\u6cd5\u6765\u52a0\u8f7d\u56fe\u7247<\/span>\n        <span class=\"hljs-comment\">\/\/ \u7b2c\u4e00\u4e2a\u53c2\u6570\u5c31\u662f\u56fe\u7247\u7684URL\u5730\u5740<\/span>\n        <span class=\"hljs-comment\">\/\/ \u7b2c\u4e8c\u4e2a\u53c2\u6570\u5219\u662f\u521a\u521a\u83b7\u53d6\u5230\u7684ImageListener\u5bf9\u8c61<\/span>\n        <span class=\"hljs-comment\">\/\/ \u5982\u679c\u60f3\u5bf9\u56fe\u7247\u7684\u5927\u5c0f\u8fdb\u884c\u9650\u5236\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528get()\u65b9\u6cd5\u7684\u91cd\u8f7d\uff0c\u6307\u5b9a\u56fe\u7247\u5141\u8bb8\u7684\u6700\u5927\u5bbd\u5ea6\u548c\u9ad8\u5ea6\uff0c\u5373\u901a\u8fc7\u7b2c\u4e09\u7b2c\u56db\u4e2a\u53c2\u6570\u6307\u5b9a<\/span>\n        loader.get(url,listener);\n    }<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u6700\u540e\uff0cVolley\u63d0\u4f9b\u4e86\u4e00\u79cd\u81ea\u5b9a\u4e49ImageView\u6765\u52a0\u8f7d\u56fe\u7247\uff0c\u5176\u4f7f\u7528\u65b9\u6cd5\u53ef\u6982\u62ec\u4e3a<\/p>\n<p style=\"margin: 1em 0px;\">1. \u521b\u5efa\u4e00\u4e2aRequestQueue\u5bf9\u8c61\u3002<\/p>\n<p style=\"margin: 1em 0px;\">2. \u521b\u5efa\u4e00\u4e2aImageLoader\u5bf9\u8c61\u3002<\/p>\n<p style=\"margin: 1em 0px;\">3. \u5728\u5e03\u5c40\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4e00\u4e2aNetworkImageView\u63a7\u4ef6\u3002<\/p>\n<p style=\"margin: 1em 0px;\">4. \u5728\u4ee3\u7801\u4e2d\u83b7\u53d6\u8be5\u63a7\u4ef6\u7684\u5b9e\u4f8b\u3002<\/p>\n<p style=\"margin: 1em 0px;\">5. \u8bbe\u7f6e\u8981\u52a0\u8f7d\u7684\u56fe\u7247\u5730\u5740\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u6211\u4eec\u5728\u5e03\u5c40\u4e2d\u7533\u660e\u8be5\u63a7\u4ef6<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">&lt;<span class=\"hljs-keyword\">com<\/span><span class=\"hljs-preprocessor\">.android<\/span><span class=\"hljs-preprocessor\">.volley<\/span><span class=\"hljs-preprocessor\">.toolbox<\/span><span class=\"hljs-preprocessor\">.NetworkImageView<\/span>\n        android:id=<span class=\"hljs-string\">\"@+id\/network_image_view\"<\/span>\n        android:layout_width=<span class=\"hljs-string\">\"wrap_content\"<\/span>\n        android:layout_height=<span class=\"hljs-string\">\"wrap_content\"<\/span>\n        android:layout_centerInParent=<span class=\"hljs-string\">\"true\"<\/span>\n        \/&gt;<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u5728\u7a0b\u5e8f\u4e2d\u5b9e\u73b0\u52a0\u8f7d<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">public void networkImageView(){\n        RequestQueue queue = Volley<span class=\"hljs-preprocessor\">.newRequestQueue<\/span>(getApplicationContext())<span class=\"hljs-comment\">;<\/span>\n        ImageLoader loader=new ImageLoader(queue,new BitmapCache() )<span class=\"hljs-comment\">;<\/span>\n        NetworkImageView niv= (NetworkImageView) findViewById(R<span class=\"hljs-preprocessor\">.id<\/span><span class=\"hljs-preprocessor\">.network<\/span>_image_view)<span class=\"hljs-comment\">;<\/span>\n        niv<span class=\"hljs-preprocessor\">.setDefaultImageResId<\/span>(R<span class=\"hljs-preprocessor\">.mipmap<\/span><span class=\"hljs-preprocessor\">.ic<\/span>_launcher)<span class=\"hljs-comment\">;\/\/\u8bbe\u7f6e\u52a0\u8f7d\u4e2d\u663e\u793a\u7684\u56fe\u7247<\/span>\n        niv<span class=\"hljs-preprocessor\">.setErrorImageResId<\/span>(R<span class=\"hljs-preprocessor\">.mipmap<\/span><span class=\"hljs-preprocessor\">.ic<\/span>_launcher)<span class=\"hljs-comment\">;\/\/\u8bbe\u7f6e\u52a0\u8f7d\u5931\u8d25\u65f6\u663e\u793a\u7684\u56fe\u7247<\/span>\n        niv<span class=\"hljs-preprocessor\">.setImageUrl<\/span>(<span class=\"hljs-string\">\"https:\/\/www.baidu.com\/img\/bdlogo.png\"<\/span>,  loader)<span class=\"hljs-comment\">;\/\/\u8bbe\u7f6e\u76ee\u6807\u56fe\u7247\u7684URL\u5730\u5740<\/span>\n    }<\/code><\/pre>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section>\u81ea\u5b9a\u4e49Request<\/section>\n<\/li>\n<\/ul>\n<p style=\"margin: 1em 0px;\">\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5f80\u5f80\u9700\u8981\u5c06http\u8bf7\u6c42\u4e0ejson\u8fdb\u884c\u96c6\u6210\uff0c\u800cVolley\u6b63\u6070\u6070\u652f\u6301\u8fd9\u6837\u7684\u65b9\u5f0f\uff0c\u4e0d\u8fc7\u9700\u8981\u6211\u4eec\u81ea\u5df1\u81ea\u5b9a\u4e49Request\uff0c\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528google\u7684Gson\u5e93\u8fdb\u884c\u96c6\u6210\u3002<\/p>\n<p style=\"margin: 1em 0px;\">1. \u7ee7\u627fRequest\u7c7b<\/p>\n<p style=\"margin: 1em 0px;\">2. \u91cd\u5199parseNetworkResponse\uff0c\u5b9e\u73b0json\u4e0e\u5b9e\u4f53\u7c7b\u8f6c\u6362\uff0c\u7531\u4e8e\u5b9e\u4f53\u7c7b\u672a\u5b9a\uff0c\u6240\u4ee5\u91c7\u7528\u6cdb\u578b<\/p>\n<p style=\"margin: 1em 0px;\">\u4e0b\u6587\u7528\u5230\u7684json\u5b57\u7b26\u4e32\u5982\u4e0b<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">{\"<span class=\"hljs-attribute\">name<\/span>\":<span class=\"hljs-value\"><span class=\"hljs-string\">\"lizhangqu\"<\/span><\/span>,\"<span class=\"hljs-attribute\">age<\/span>\":<span class=\"hljs-value\"><span class=\"hljs-number\">16<\/span><\/span>}<\/code><\/pre>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\"><span class=\"hljs-keyword\">package<\/span> cn.edu.zafu.http;\n\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.toolbox.HttpHeaderParser;\n<span class=\"hljs-keyword\">import<\/span> com.google.gson.Gson;\n\n<span class=\"hljs-keyword\">import<\/span> java.io.UnsupportedEncodingException;\n\n<span class=\"hljs-javadoc\">\/**\n * Created by lizhangqu on 2015\/5\/7.\n *\/<\/span>\npublic <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">GsonRequest<\/span>&lt;<span class=\"hljs-title\">T<\/span>&gt; <span class=\"hljs-keyword\">extends<\/span> <span class=\"hljs-title\">Request<\/span>&lt;<span class=\"hljs-title\">T<\/span>&gt; {<\/span>\n\n    <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">final<\/span> Response.Listener&lt;T&gt; mListener;\n\n    <span class=\"hljs-keyword\">private<\/span> Gson mGson;\n\n    <span class=\"hljs-keyword\">private<\/span> Class&lt;T&gt; mClass;\n\n    public GsonRequest(int method, String url, Class&lt;T&gt; clazz, Response.Listener&lt;T&gt; listener,\n                       Response.ErrorListener errorListener) {\n        <span class=\"hljs-keyword\">super<\/span>(method, url, errorListener);\n        mGson = <span class=\"hljs-keyword\">new<\/span> Gson();\n        mClass = clazz;\n        mListener = listener;\n    }\n\n    public GsonRequest(String url, Class&lt;T&gt; clazz, Response.Listener&lt;T&gt; listener,\n                       Response.ErrorListener errorListener) {\n        <span class=\"hljs-keyword\">this<\/span>(Method.GET, url, clazz, listener, errorListener);\n    }\n\n    <span class=\"hljs-annotation\">@Override<\/span>\n    <span class=\"hljs-keyword\">protected<\/span> Response&lt;T&gt; parseNetworkResponse(NetworkResponse response) {\n        <span class=\"hljs-keyword\">try<\/span> {\n            String jsonString = <span class=\"hljs-keyword\">new<\/span> String(response.data,\n                    HttpHeaderParser.parseCharset(response.headers));\n            <span class=\"hljs-keyword\">return<\/span> Response.success(mGson.fromJson(jsonString, mClass),\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> ParseError(e));\n        }\n    }\n\n    <span class=\"hljs-annotation\">@Override<\/span>\n    <span class=\"hljs-keyword\">protected<\/span> void deliverResponse(T response) {\n        mListener.onResponse(response);\n    }\n\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u7f16\u5199\u6d4b\u8bd5\u5b9e\u4f53\u7c7b\uff0c\u4e24\u4e2a\u5b57\u6bb5\u4e00\u4e2aname\u4e00\u4e2aage<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\"><span class=\"hljs-keyword\">package<\/span> cn.edu.zafu.http;\n\n<span class=\"hljs-javadoc\">\/**\n * Created by lizhangqu on 2015\/5\/7.\n *\/<\/span>\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Person<\/span> {<\/span>\n    <span class=\"hljs-keyword\">private<\/span> String name;\n    <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">int<\/span> age;\n\n    <span class=\"hljs-keyword\">public<\/span> String <span class=\"hljs-title\">getName<\/span>() {\n        <span class=\"hljs-keyword\">return<\/span> name;\n    }\n\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">setName<\/span>(String name) {\n        <span class=\"hljs-keyword\">this<\/span>.name = name;\n    }\n\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">int<\/span> <span class=\"hljs-title\">getAge<\/span>() {\n        <span class=\"hljs-keyword\">return<\/span> age;\n    }\n\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">setAge<\/span>(<span class=\"hljs-keyword\">int<\/span> age) {\n        <span class=\"hljs-keyword\">this<\/span>.age = age;\n    }\n\n    <span class=\"hljs-annotation\">@Override<\/span>\n    <span class=\"hljs-keyword\">public<\/span> String <span class=\"hljs-title\">toString<\/span>() {\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">\"Person{\"<\/span> +\n                <span class=\"hljs-string\">\"name='\"<\/span> + name + <span class=\"hljs-string\">'\\''<\/span> +\n                <span class=\"hljs-string\">\", age=\"<\/span> + age +\n                <span class=\"hljs-string\">'}'<\/span>;\n    }\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u8c03\u7528\u65b9\u6cd5\u548cStringRequest\u662f\u4e00\u6837\u7684\u3002\u5982\u4e0b\u6240\u793a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">    <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">json<\/span>(){\n        RequestQueue queue = Volley.newRequestQueue(getApplicationContext());\n        String url = <span class=\"hljs-string\">\"http:\/\/121.41.119.107\/test\/index.php\"<\/span>;\n        GsonRequest&lt;Person&gt; request=<span class=\"hljs-keyword\">new<\/span> GsonRequest&lt;Person&gt;(url, Person.class, <span class=\"hljs-keyword\">new<\/span> Response.Listener&lt;Person&gt;() {\n            <span class=\"hljs-annotation\">@Override<\/span>\n            <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">onResponse<\/span>(Person response) {\n                Log.d(<span class=\"hljs-string\">\"TAG\"<\/span>,response.toString());\n            }\n        }, <span class=\"hljs-keyword\">new<\/span> Response.ErrorListener() {\n            <span class=\"hljs-annotation\">@Override<\/span>\n            <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">onErrorResponse<\/span>(VolleyError error) {\n\n            }\n        });\n        queue.add(request);\n    }<\/code><\/pre>\n<blockquote style=\"margin: 20px 0px; display: block; font-size: 0.9em; overflow: auto; border-left: 3px solid #ef7060; color: #6a737d; padding: 10px 10px 10px 20px; background: #fff9f9;\">\n<p style=\"margin: 1em 0px;\">\u4ee5\u4e0a\u4ee3\u7801\u53c2\u8003\u4e86\u90ed\u9716\u4e09\u7bc7Volley\u535a\u5ba2\u6587\u7ae0\uff0c\u5206\u522b\u4e3a<br \/>\n<a href=\"http:\/\/blog.csdn.net\/guolin_blog\/article\/details\/17482095\">Android Volley\u5b8c\u5168\u89e3\u6790(\u4e00)\uff0c\u521d\u8bc6Volley\u7684\u57fa\u672c\u7528\u6cd5<\/a><br \/>\n<a href=\"http:\/\/blog.csdn.net\/guolin_blog\/article\/details\/17482165\">Android Volley\u5b8c\u5168\u89e3\u6790(\u4e8c)\uff0c\u4f7f\u7528Volley\u52a0\u8f7d\u7f51\u7edc\u56fe\u7247<\/a><br \/>\n<a href=\"http:\/\/blog.csdn.net\/guolin_blog\/article\/details\/17612763\">Android Volley\u5b8c\u5168\u89e3\u6790(\u4e09)\uff0c\u5b9a\u5236\u81ea\u5df1\u7684Request<\/a><\/p>\n<\/blockquote>\n<h2 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid #ef7060; display: flex;\"><span style=\"display: inline-block; font-weight: bold; background: #ef7060; color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;\">okHttp<\/span><\/h2>\n<p style=\"margin: 1em 0px;\">okhttp \u662f\u4e00\u4e2a Java \u7684 HTTP+SPDY \u5ba2\u6237\u7aef\u5f00\u53d1\u5305\uff0c\u540c\u65f6\u4e5f\u652f\u6301 Android\u3002\u9700\u8981Android 2.3\u4ee5\u4e0a\u3002<\/p>\n<h3 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em;\">\u7279\u70b9<\/h3>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section>OKHttp\u662fAndroid\u7248Http\u5ba2\u6237\u7aef\u3002\u975e\u5e38\u9ad8\u6548\uff0c\u652f\u6301SPDY\u3001\u8fde\u63a5\u6c60\u3001GZIP\u548c HTTP \u7f13\u5b58\u3002<\/section>\n<\/li>\n<li>\n<section>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cOKHttp\u4f1a\u81ea\u52a8\u5904\u7406\u5e38\u89c1\u7684\u7f51\u7edc\u95ee\u9898\uff0c\u50cf\u4e8c\u6b21\u8fde\u63a5\u3001SSL\u7684\u63e1\u624b\u95ee\u9898\u3002<\/section>\n<\/li>\n<li>\n<section>\u5982\u679c\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\u96c6\u6210\u4e86OKHttp\uff0cRetrofit\u9ed8\u8ba4\u4f1a\u4f7f\u7528OKHttp\u5904\u7406\u5176\u4ed6\u7f51\u7edc\u5c42\u8bf7\u6c42\u3002<\/section>\n<\/li>\n<li>\n<section>\u4eceAndroid4.4\u5f00\u59cbHttpURLConnection\u7684\u5e95\u5c42\u5b9e\u73b0\u91c7\u7528\u7684\u662fokHttp.<\/section>\n<\/li>\n<\/ul>\n<h3 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em;\">\u7528\u6cd5<\/h3>\n<ol style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem;\">\n<li>\n<section>\u65b0\u5efa\u4e00\u4e2aOkHttpClient\u5bf9\u8c61<\/section>\n<\/li>\n<li>\n<section>\u901a\u8fc7Request.Builder\u5bf9\u8c61\u65b0\u5efa\u4e00\u4e2aRequest\u5bf9\u8c61<\/section>\n<\/li>\n<li>\u8fd4\u56de\u6267\u884c\u7ed3\u679c\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>GET<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\"><span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-built_in\">String<\/span> <span class=\"hljs-keyword\">get<\/span>(<span class=\"hljs-built_in\">String<\/span> url) {\n        OkHttpClient client = <span class=\"hljs-keyword\">new<\/span> OkHttpClient();\n        <span class=\"hljs-built_in\">Request<\/span> <span class=\"hljs-built_in\">request<\/span> = <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-built_in\">Request<\/span>.Builder()\n                .url(url)\n                .build();\n        <span class=\"hljs-built_in\">Response<\/span> <span class=\"hljs-built_in\">response<\/span> = <span class=\"hljs-literal\">null<\/span>;\n        try {\n            <span class=\"hljs-built_in\">response<\/span> = client.newCall(<span class=\"hljs-built_in\">request<\/span>).<span class=\"hljs-keyword\">execute<\/span>();\n            return <span class=\"hljs-built_in\">response<\/span>.body().<span class=\"hljs-built_in\">string<\/span>();\n        } catch (IOException e) {\n            e.printStackTrace();\n        }\n        return <span class=\"hljs-literal\">null<\/span>;\n    }<\/code><\/pre>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section>POST<\/section>\n<\/li>\n<\/ul>\n<p style=\"margin: 1em 0px;\">POST\u9700\u8981\u4f7f\u7528RequestBody\u5bf9\u8c61\uff0c\u4e4b\u540e\u518d\u6784\u5efaRequest\u5bf9\u8c61\u65f6\u8c03\u7528post\u51fd\u6570\u5c06\u5176\u4f20\u5165\u5373\u53ef<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\"><span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-built_in\">String<\/span> post(<span class=\"hljs-built_in\">String<\/span> url) {\n        OkHttpClient client = <span class=\"hljs-keyword\">new<\/span> OkHttpClient();\n\n        RequestBody formBody = <span class=\"hljs-keyword\">new<\/span> FormEncodingBuilder()\n                .add(<span class=\"hljs-string\">\"user\"<\/span>, <span class=\"hljs-string\">\"Jurassic Park\"<\/span>)\n                .add(<span class=\"hljs-string\">\"pass\"<\/span>, <span class=\"hljs-string\">\"asasa\"<\/span>)\n                .add(<span class=\"hljs-string\">\"time\"<\/span>, <span class=\"hljs-string\">\"12132\"<\/span>)\n                .build();\n        <span class=\"hljs-built_in\">Request<\/span> <span class=\"hljs-built_in\">request<\/span> = <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-built_in\">Request<\/span>.Builder()\n                .url(url)\n                .post(formBody)\n                .build();\n        <span class=\"hljs-built_in\">Response<\/span> <span class=\"hljs-built_in\">response<\/span> = <span class=\"hljs-literal\">null<\/span>;\n        try {\n            <span class=\"hljs-built_in\">response<\/span> = client.newCall(<span class=\"hljs-built_in\">request<\/span>).<span class=\"hljs-keyword\">execute<\/span>();\n            return <span class=\"hljs-built_in\">response<\/span>.body().<span class=\"hljs-built_in\">string<\/span>();\n        } catch (IOException e) {\n            e.printStackTrace();\n        }\n        return <span class=\"hljs-literal\">null<\/span>;\n    }<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u6b64\u5916\uff0cpost\u7684\u4f7f\u7528\u65b9\u6cd5\u8fd8\u652f\u6301\u6587\u4ef6\u7b49\u64cd\u4f5c\uff0c\u5177\u4f53\u4f7f\u7528\u65b9\u6cd5\u6709\u5174\u8da3\u7684\u53ef\u4ee5\u81ea\u884c\u67e5\u9605<\/p>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section>\u5bf9Gson\u7684\u652f\u6301<\/section>\n<\/li>\n<\/ul>\n<p style=\"margin: 1em 0px;\">okHttp\u8fd8\u81ea\u5e26\u4e86\u5bf9Gson\u7684\u652f\u6301<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">  <span class=\"hljs-keyword\">private<\/span> Person gson(<span class=\"hljs-built_in\">String<\/span> url){\n        OkHttpClient client = <span class=\"hljs-keyword\">new<\/span> OkHttpClient();\n        Gson gson = <span class=\"hljs-keyword\">new<\/span> Gson();\n        <span class=\"hljs-built_in\">Request<\/span> <span class=\"hljs-built_in\">request<\/span> = <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-built_in\">Request<\/span>.Builder()\n                .url(url)\n                .build();\n        <span class=\"hljs-built_in\">Response<\/span> <span class=\"hljs-built_in\">response<\/span> = <span class=\"hljs-literal\">null<\/span>;\n        try {\n            <span class=\"hljs-built_in\">response<\/span> = client.newCall(<span class=\"hljs-built_in\">request<\/span>).<span class=\"hljs-keyword\">execute<\/span>();\n            Person person = gson.fromJson(<span class=\"hljs-built_in\">response<\/span>.body().charStream(), Person.<span class=\"hljs-keyword\">class<\/span>);\n            return person;\n        } catch (IOException e) {\n            e.printStackTrace();\n        }\n        return <span class=\"hljs-literal\">null<\/span>;\n    }<\/code><\/pre>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section>\u5f02\u6b65\u64cd\u4f5c<\/section>\n<\/li>\n<\/ul>\n<p style=\"margin: 1em 0px;\">\u4ee5\u4e0a\u7684\u4e24\u4e2a\u4f8b\u5b50\u5fc5\u987b\u5728\u5b50\u7ebf\u7a0b\u4e2d\u5b8c\u6210\uff0c\u540c\u65f6okHttp\u8fd8\u63d0\u4f9b\u4e86\u5f02\u6b65\u7684\u65b9\u6cd5\u8c03\u7528\uff0c\u901a\u8fc7\u4f7f\u7528\u56de\u8c03\u6765\u8fdb\u884c\u5f02\u6b65\u8c03\u7528\uff0c\u7136\u540eokHttp\u7684\u56de\u8c03\u4f9d\u7136\u4e0d\u5728\u4e3b\u7ebf\u7a0b\u4e2d\uff0c\u56e0\u6b64\u8be5\u56de\u8c03\u4e2d\u4e0d\u80fd\u64cd\u4f5cUI<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\"> <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">getAsync<\/span>(String url) {\n        OkHttpClient client = <span class=\"hljs-keyword\">new<\/span> OkHttpClient();\n        Request request = <span class=\"hljs-keyword\">new<\/span> Request.Builder()\n                .url(url)\n                .build();\n        Response response = <span class=\"hljs-keyword\">null<\/span>;\n\n        client.newCall(request).enqueue(<span class=\"hljs-keyword\">new<\/span> Callback() {\n            <span class=\"hljs-annotation\">@Override<\/span>\n            <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">onFailure<\/span>(Request request, IOException e) {\n\n            }\n\n            <span class=\"hljs-annotation\">@Override<\/span>\n            <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">onResponse<\/span>(Response response) <span class=\"hljs-keyword\">throws<\/span> IOException {\n                String result = response.body().string();\n                Toast.makeText(getApplicationContext(),result,Toast.LENGTH_SHORT).show();\n                <span class=\"hljs-comment\">\/\/\u4e0d\u80fd\u64cd\u4f5cui\uff0c\u56de\u8c03\u4f9d\u7136\u5728\u5b50\u7ebf\u7a0b<\/span>\n                Log.d(<span class=\"hljs-string\">\"TAG\"<\/span>, result);\n            }\n        });\n\n\n    }<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">okHttp\u7684\u4f7f\u7528\u8fd8\u6709\u5f88\u591a\u5185\u5bb9\uff0c\u8fd9\u91cc\u4e5f\u4e0d\u8fc7\u591a\u4ecb\u7ecd\uff0c\u66f4\u591a\u5185\u5bb9\uff0c\u53c2\u8003<a href=\"http:\/\/square.github.io\/okhttp\/\">\u5b98\u65b9\u7f51\u5740<\/a><\/p>\n<h2 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid #ef7060; display: flex;\"><span style=\"display: inline-block; font-weight: bold; background: #ef7060; color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;\">Retrofit<\/span><\/h2>\n<h3 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em;\">\u7279\u70b9<\/h3>\n<ol style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem;\">\n<li>\n<section>\u6027\u80fd\u6700\u597d\uff0c\u5904\u7406\u6700\u5feb<\/section>\n<\/li>\n<li>\n<section>\u4f7f\u7528REST API\u65f6\u975e\u5e38\u65b9\u4fbf\uff1b<\/section>\n<\/li>\n<li>\n<section>\u4f20\u8f93\u5c42\u9ed8\u8ba4\u5c31\u4f7f\u7528OkHttp\uff1b<\/section>\n<\/li>\n<li>\n<section>\u652f\u6301NIO\uff1b<\/section>\n<\/li>\n<li>\n<section>\u62e5\u6709\u51fa\u8272\u7684API\u6587\u6863\u548c\u793e\u533a\u652f\u6301<\/section>\n<\/li>\n<li>\n<section>\u901f\u5ea6\u4e0a\u6bd4volley\u66f4\u5feb\uff1b<\/section>\n<\/li>\n<li>\n<section>\u5982\u679c\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\u96c6\u6210\u4e86OKHttp\uff0cRetrofit\u9ed8\u8ba4\u4f1a\u4f7f\u7528OKHttp\u5904\u7406\u5176\u4ed6\u7f51\u7edc\u5c42\u8bf7\u6c42\u3002<\/section>\n<\/li>\n<li>\n<section>\u9ed8\u8ba4\u4f7f\u7528Gson<\/section>\n<\/li>\n<\/ol>\n<h3 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em;\">\u4f7f\u7528<\/h3>\n<p style=\"margin: 1em 0px;\">Retrofit\u652f\u6301\u540c\u6b65\u548c\u5f02\u6b65\u4e24\u79cd\u65b9\u5f0f\uff0c\u5728\u4f7f\u7528\u65f6\uff0c\u9700\u8981\u5c06\u8bf7\u6c42\u5730\u5740\u8f6c\u6362\u4e3a\u63a5\u53e3\uff0c\u901a\u8fc7\u6ce8\u89e3\u6765\u6307\u5b9a\u8bf7\u6c42\u65b9\u6cd5\uff0c\u8bf7\u6c42\u53c2\u6570\uff0c\u8bf7\u6c42\u5934\uff0c\u8fd4\u56de\u503c\u7b49\u4fe1\u606f\u3002\u8fd8\u662f\u4f7f\u7528\u4e4b\u524d\u7684person\u7684\u90a3\u6bb5json\u503c\uff0cget\u8bf7\u6c42\u5230\u670d\u52a1\u5668\u540e\u4ece\u6570\u636e\u5e93\u67e5\u8be2\u6570\u636e\uff0c\u8fd4\u56de\u503c\u4e3a\u67e5\u8be2\u5230\u7684\u6570\u636e\uff0cpost\u8bf7\u6c42\u5411\u670d\u52a1\u5668\u63d0\u4ea4\u4e00\u6761\u6570\u636e\uff0c\u8fd4\u56de\u503c\u4e3a\u63d0\u4ea4\u7684\u6570\u636e\u3002 \u9996\u5148\u5b8c\u6210\u8bf7\u6c42\u6240\u7528\u7684service\uff0c\u662f\u4e00\u4e2ainterface\uff0c\u5b8c\u5168\u901a\u8fc7\u6ce8\u89e3\u5b8c\u6210\u914d\u7f6e<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\"><span class=\"hljs-keyword\">package<\/span> cn.edu.zafu.http;\n\n<span class=\"hljs-keyword\">import<\/span> retrofit.Callback;\n<span class=\"hljs-keyword\">import<\/span> retrofit.http.Field;\n<span class=\"hljs-keyword\">import<\/span> retrofit.http.FormUrlEncoded;\n<span class=\"hljs-keyword\">import<\/span> retrofit.http.GET;\n<span class=\"hljs-keyword\">import<\/span> retrofit.http.Headers;\n<span class=\"hljs-keyword\">import<\/span> retrofit.http.POST;\n<span class=\"hljs-keyword\">import<\/span> retrofit.http.Path;\n<span class=\"hljs-keyword\">import<\/span> retrofit.http.Query;\n\n<span class=\"hljs-javadoc\">\/**\n * Created by lizhangqu on 2015\/5\/11.\n *\/<\/span>\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">interface<\/span> <span class=\"hljs-title\">PersonService<\/span> {<\/span>\n    <span class=\"hljs-annotation\">@Headers<\/span>({\n            <span class=\"hljs-string\">\"Cache-Control: max-age=640000\"<\/span>,\n            <span class=\"hljs-string\">\"User-Agent: Mozilla\/5.0 (Windows NT 6.1; WOW64; Trident\/7.0; rv:11.0) like Gecko\"<\/span>\n    })\n    <span class=\"hljs-comment\">\/\/\u901a\u8fc7\u6ce8\u89e3\u8bbe\u7f6e\u8bf7\u6c42\u5934<\/span>\n    <span class=\"hljs-annotation\">@GET<\/span>(<span class=\"hljs-string\">\"\/{test}\/rest.php\"<\/span>)\n    <span class=\"hljs-comment\">\/\/\u8bbe\u7f6e\u8bf7\u6c42\u65b9\u6cd5\u4e3aget\uff0c\u76f8\u5bf9\u8def\u5f84\u4e3a\u6ce8\u89e3\u5185\u5185\u5bb9\uff0c\u5176\u4e2d{test}\u4f1a\u88ab@Path\u6ce8\u89e3\u6307\u5b9a\u5185\u5bb9\u66ff\u6362<\/span>\n    Person getPerson(<span class=\"hljs-annotation\">@Path<\/span>(<span class=\"hljs-string\">\"test\"<\/span>) String dir,<span class=\"hljs-annotation\">@Query<\/span>(<span class=\"hljs-string\">\"name\"<\/span>) String name);\n    <span class=\"hljs-comment\">\/\/@Query\u7528\u4e8e\u6307\u5b9a\u53c2\u6570<\/span>\n\n    <span class=\"hljs-annotation\">@FormUrlEncoded<\/span>\n    <span class=\"hljs-comment\">\/\/urlencode<\/span>\n    <span class=\"hljs-annotation\">@POST<\/span>(<span class=\"hljs-string\">\"\/test\/rest1.php\"<\/span>)\n    <span class=\"hljs-comment\">\/\/post\u63d0\u4ea4<\/span>\n    Person updatePerson(<span class=\"hljs-annotation\">@Field<\/span>(<span class=\"hljs-string\">\"name\"<\/span>) String name,<span class=\"hljs-annotation\">@Field<\/span>(<span class=\"hljs-string\">\"age\"<\/span>) <span class=\"hljs-keyword\">int<\/span> age);\n    <span class=\"hljs-comment\">\/\/@Field\u63d0\u4ea4\u7684\u57df<\/span>\n\n\n    <span class=\"hljs-annotation\">@POST<\/span>(<span class=\"hljs-string\">\"\/test\/rest1.php\"<\/span>)\n    <span class=\"hljs-keyword\">void<\/span> updatePerson(<span class=\"hljs-annotation\">@Field<\/span>(<span class=\"hljs-string\">\"name\"<\/span>) String name,<span class=\"hljs-annotation\">@Field<\/span>(<span class=\"hljs-string\">\"age\"<\/span>) <span class=\"hljs-keyword\">int<\/span> age, Callback&lt;Person&gt; callback);\n    <span class=\"hljs-comment\">\/\/\u5f02\u6b65\u56de\u8c03\uff0c\u4e0d\u80fd\u6307\u5b9a\u8fd4\u56de\u503c<\/span>\n}<\/code><\/pre>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section>GET<br \/>\n\u4f7f\u7528\u65f6\uff0c\u901a\u8fc7RestAdapter\u7684\u5b9e\u4f8b\u83b7\u5f97\u4e00\u4e2a\u63a5\u53e3\u7684\u5b9e\u4f8b\uff0c\u5176\u672c\u8d28\u662f\u52a8\u6001\u4ee3\u7406\uff0c\u6ce8\u610f\u542b\u6709\u8fd4\u56de\u503c\u7684\u65b9\u6cd5\u662f\u540c\u6b65\u7684\uff0c\u4e0d\u80fdUI\u7ebf\u7a0b\u4e2d\u8c03\u7528\uff0c\u5e94\u8be5\u5728\u5b50\u7ebf\u7a0b\u4e2d\u5b8c\u6210<\/section>\n<\/li>\n<\/ul>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">RestAdapter restAdapter = new RestAdapter<span class=\"hljs-preprocessor\">.Builder<\/span>()\n                        <span class=\"hljs-preprocessor\">.setEndpoint<\/span>(<span class=\"hljs-string\">\"http:\/\/121.41.119.107\"<\/span>)\n                        <span class=\"hljs-preprocessor\">.build<\/span>()<span class=\"hljs-comment\">;<\/span>\n                PersonService personService=restAdapter<span class=\"hljs-preprocessor\">.create<\/span>(PersonService<span class=\"hljs-preprocessor\">.class<\/span>)<span class=\"hljs-comment\">;<\/span>\n                Person person=personService<span class=\"hljs-preprocessor\">.getPerson<\/span>(<span class=\"hljs-string\">\"test\"<\/span>,<span class=\"hljs-string\">\"zhangsan\"<\/span>)<span class=\"hljs-comment\">;<\/span>\n                Log<span class=\"hljs-preprocessor\">.d<\/span>(<span class=\"hljs-string\">\"TAG\"<\/span>,person<span class=\"hljs-preprocessor\">.toString<\/span>())<span class=\"hljs-comment\">;<\/span><\/code><\/pre>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section>POST<\/section>\n<\/li>\n<\/ul>\n<p style=\"margin: 1em 0px;\">POST\u7684\u8c03\u7528\u540cGet\uff0c\u83b7\u5f97adapter\u540e\u83b7\u5f97\u4e00\u4e2a\u4ee3\u7406\u5bf9\u8c61\uff0c\u7136\u540e\u901a\u8fc7\u8fd9\u4e2a\u4ee3\u7406\u5bf9\u8c61\u8fdb\u884c\u7f51\u7edc\u8bf7\u6c42<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">Person person1=personService<span class=\"hljs-preprocessor\">.updatePerson<\/span>(<span class=\"hljs-string\">\"lizhangqu\"<\/span>, <span class=\"hljs-number\">12<\/span>)<span class=\"hljs-comment\">;<\/span>\nLog<span class=\"hljs-preprocessor\">.d<\/span>(<span class=\"hljs-string\">\"TAG\"<\/span>,person1<span class=\"hljs-preprocessor\">.toString<\/span>())<span class=\"hljs-comment\">;<\/span><\/code><\/pre>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section>\u5f02\u6b65\u8bf7\u6c42<\/section>\n<\/li>\n<\/ul>\n<p style=\"margin: 1em 0px;\">\u5982\u679c\u8981\u4f7f\u7528\u5f02\u6b65\u8bf7\u6c42\uff0c\u9700\u8981\u5c06\u63a5\u53e3\u4e2d\u7684\u65b9\u6cd5\u8fd4\u56de\u503c\u4fee\u6539\u4f1avoid\uff0c\u518d\u52a0\u5165\u56de\u8c03\u53c2\u6570Callback\uff0c\u5c31\u5982PersonService\u4e2d\u7b2c\u4e09\u4e2a\u65b9\u6cd5\u4e00\u6837\uff0c\u8bf7\u6c42\u5b8c\u6210\u540e\u4f1a\u56de\u8c03\u8be5callback\u5bf9\u8c61\u7684success\u6216\u8005fail\u65b9\u6cd5\u3002<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">RestAdapter restAdapter = <span class=\"hljs-keyword\">new<\/span> RestAdapter.Builder()\n                .setEndpoint(<span class=\"hljs-string\">\"http:\/\/121.41.119.107\"<\/span>)\n                .build();\n        PersonService personService=restAdapter.create(PersonService.class);\n        personService.updatePerson(<span class=\"hljs-string\">\"lizhangqu\"<\/span>,<span class=\"hljs-number\">23<\/span>, <span class=\"hljs-keyword\">new<\/span> Callback&lt;Person&gt;() {\n            <span class=\"hljs-annotation\">@Override<\/span>\n            <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">success<\/span>(Person person, Response response) {\n                Log.d(<span class=\"hljs-string\">\"TAG\"<\/span>, person.toString());\n            }\n\n            <span class=\"hljs-annotation\">@Override<\/span>\n            <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">failure<\/span>(RetrofitError error) {\n\n            }\n        });<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">Retrofit\u7684\u4f7f\u7528\u8fd8\u6709\u5f88\u591a\u5185\u5bb9\uff0c\u5269\u4e0b\u7684\u5c31\u7559\u7ed9\u5404\u4f4d\u8bfb\u8005\u81ea\u884c\u53bb\u53d1\u73b0\u4e86\uff0c\u800c\u5176\u5b98\u7f51\u9875\u63d0\u4f9b\u4e86\u53ca\u5176\u8be6\u7ec6\u7684\u8bf4\u660e\u3002\u4e0b\u9762\u63d0\u4f9b\u5b98\u65b9\u7f51\u5740<\/p>\n<blockquote style=\"margin: 20px 0px; display: block; font-size: 0.9em; overflow: auto; border-left: 3px solid #ef7060; color: #6a737d; padding: 10px 10px 10px 20px; background: #fff9f9;\">\n<p style=\"margin: 1em 0px;\"><a href=\"http:\/\/square.github.io\/retrofit\/\">retrofit\u5b98\u7f51\u793a\u4f8b<\/a><\/p>\n<\/blockquote>\n<p style=\"margin: 1em 0px;\">\u8fd9\u4e2a\u5e93\u91cc\u9762\u6709\u5f88\u591a\u7cbe\u534e\u7684\u5185\u5bb9\uff0c\u5efa\u8bae\u5404\u4f4d\u4ed4\u7ec6\u7684\u9605\u8bfb\u4e0b\u5b98\u65b9\u7684\u6587\u6863\u3002<\/p>\n<h2 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid #ef7060; display: flex;\"><span style=\"display: inline-block; font-weight: bold; background: #ef7060; color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;\">RoboSpice<\/span><\/h2>\n<p style=\"margin: 1em 0px;\">\u89c1\u4e4b\u524d\u5199\u7684\u4e00\u7bc7\u535a\u6587<\/p>\n<blockquote style=\"margin: 20px 0px; display: block; font-size: 0.9em; overflow: auto; border-left: 3px solid #ef7060; color: #6a737d; padding: 10px 10px 10px 20px; background: #fff9f9;\">\n<p style=\"margin: 1em 0px;\"><a href=\"http:\/\/blog.csdn.net\/sbsujjbcy\/article\/details\/45070711\">RoboSpice\uff1aandroid\u5f02\u6b65\u7f51\u7edc\u5e93\u7b80\u5355\u7528\u6cd5<\/a><\/p>\n<\/blockquote>\n<h2 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid #ef7060; display: flex;\"><span style=\"display: inline-block; font-weight: bold; background: #ef7060; color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;\">\u603b\u7ed3<\/span><\/h2>\n<p style=\"margin: 1em 0px;\">\u7f51\u7edc\u8bf7\u6c42\u5e93\u591a\u79cd\u591a\u6837\uff0c\u6700\u7ec8\u5176\u672c\u8d28\u601d\u60f3\u662f\u4e00\u81f4\u7684\uff0c\u8981\u5b66\u4f1a\u878d\u6c47\u8d2f\u901a\uff0c\u8fd8\u662f\u8981fucking the source code\u3002\u7531\u4e8e\u672c\u7bc7\u6587\u7ae0\u5df2\u7ecf\u8fc7\u957f\uff0c\u6240\u4ee5\u56fe\u7247\u7684\u7f51\u7edc\u52a0\u8f7d\u51c6\u5907\u53e6\u5f00\u4e00\u7bc7\u535a\u5ba2\u8fdb\u884c\u6574\u7406\u3002<\/p>\n<h2 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid #ef7060; display: flex;\"><span style=\"display: inline-block; font-weight: bold; background: #ef7060; color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;\">\u6e90\u7801\u4e0b\u8f7d<\/span><\/h2>\n<p style=\"margin: 1em 0px;\"><a href=\"http:\/\/download.csdn.net\/detail\/sbsujjbcy\/8685517\">\u6e90\u7801\u4e0b\u8f7d<\/a><\/p>\n<p style=\"margin: 1em 0px;\">\u539f\u6587\u51fa\u5904\uff1a <a href=\"http:\/\/blog.csdn.net\/sbsujjbcy\/article\/details\/45568053\" target=\"_blank\">lizhangqu\uff08@_\u5b89\u5353\u5f1f\uff09<\/a><\/p>\n<blockquote style=\"margin: 20px 0px; display: block; font-size: 0.9em; overflow: auto; border-left: 3px solid #ef7060; color: #6a737d; padding: 10px 10px 10px 20px; background: #fff9f9;\">\n<p style=\"margin: 1em 0px;\"><strong>\u4e00\u53e5\u8bdd\u603b\u7ed3\uff1aAndroid \u7f51\u7edc\u8bf7\u6c42\u4ece HttpURLConnection \/ HttpClient \u6253\u5e95\uff0c\u518d\u6309\u573a\u666f\u9009\u7528 Volley\u3001OkHttp\u3001Retrofit \u6216 RoboSpice\u3002<\/strong><\/p>\n<\/blockquote>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u76f4\u60f3\u5bf9Android\u7f51\u7edc\u8bf7\u6c42\u8fdb\u884c\u6574\u7406\uff0c\u521a\u597d\u770b\u5230\u8fd9\u7bc7\u6587\u7ae0\uff0c\u5206\u4eab\u5982\u4e0b\u5e0c\u671b\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[202],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1055"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1055"}],"version-history":[{"count":3,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1055\/revisions"}],"predecessor-version":[{"id":1348,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1055\/revisions\/1348"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1055"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1055"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1055"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}