{"id":923,"date":"2016-09-26T15:01:28","date_gmt":"2016-09-26T07:01:28","guid":{"rendered":"http:\/\/www.zyuns.com\/?p=923"},"modified":"2016-09-26T15:01:28","modified_gmt":"2016-09-26T07:01:28","slug":"android-okhttp%e4%be%8b%e5%ad%90","status":"publish","type":"post","link":"https:\/\/www.siediyer.cn\/?p=923","title":{"rendered":"android OkHttp\u4f8b\u5b50"},"content":{"rendered":"<p><strong>OKHttp\u662f\u4e00\u6b3e\u9ad8\u6548\u7684HTTP\u5ba2\u6237\u7aef\uff0c<\/strong><strong>\u652f\u6301\u8fde\u63a5\u540c\u4e00\u5730\u5740\u7684\u94fe\u63a5\u5171\u4eab\u540c\u4e00\u4e2asocket\uff0c\u901a\u8fc7\u8fde\u63a5\u6c60\u6765\u51cf\u5c0f\u54cd\u5e94\u5ef6\u8fdf\uff0c\u8fd8\u6709\u900f\u660e\u7684GZIP\u538b\u7f29\uff0c\u8bf7\u6c42\u7f13\u5b58\u7b49\u4f18\u52bf\u3002<\/strong><\/p>\n<p><strong>1. GET <\/strong><br \/>\n\u5728OKHttp\uff0c\u6bcf\u6b21\u7f51\u7edc\u8bf7\u6c42\u5c31\u662f\u4e00\u4e2aRequest\uff0c\u6211\u4eec\u5728Request\u91cc\u586b\u5199\u6211\u4eec\u9700\u8981\u7684url\uff0cheader\u7b49\u5176\u4ed6\u53c2\u6570\uff0c\u518d\u901a\u8fc7Request\u6784\u9020\u51faCall\uff0cCall\u5185\u90e8\u53bb\u8bf7\u6c42\u53c2\u6570\uff0c\u5f97\u5230\u56de\u590d\uff0c\u5e76\u5c06\u7ed3\u679c\u544a\u8bc9\u8c03\u7528\u8005\u3002<\/p>\n<pre class=\"lang:default decode:true \">package com.jackchan.test.okhttptest;\r\n\r\nimport android.os.Bundle;\r\nimport android.support.v7.app.ActionBarActivity;\r\nimport android.util.Log;\r\n\r\nimport com.squareup.okhttp.Cache;\r\nimport com.squareup.okhttp.Callback;\r\nimport com.squareup.okhttp.OkHttpClient;\r\nimport com.squareup.okhttp.Request;\r\nimport com.squareup.okhttp.Response;\r\n\r\nimport java.io.File;\r\nimport java.io.IOException;\r\n\r\n\r\npublic class TestActivity extends ActionBarActivity {\r\n\r\n    private final static String TAG = \"TestActivity\";\r\n\r\n    private final OkHttpClient client = new OkHttpClient();\r\n\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_test);\r\n        new Thread(new Runnable() {\r\n            @Override\r\n            public void run() {\r\n                try {\r\n                    execute();\r\n                } catch (Exception e) {\r\n                    e.printStackTrace();\r\n                }\r\n            }\r\n        }).start();\r\n    }\r\n\r\n    public void execute() throws Exception {\r\n        Request request = new Request.Builder()\r\n                .url(\"http:\/\/publicobject.com\/helloworld.txt\")\r\n                .build();\r\n        Response response = client.newCall(request).execute();\r\n        if(response.isSuccessful()){\r\n            System.out.println(response.code());\r\n            System.out.println(response.body().string());\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>\u6211\u4eec\u901a\u8fc7Request.Builder\u4f20\u5165url\uff0c\u7136\u540e\u76f4\u63a5execute\u6267\u884c\u5f97\u5230Response\uff0c\u901a\u8fc7Response\u53ef\u4ee5\u5f97\u5230code,message\u7b49\u4fe1\u606f\u3002<\/p>\n<p>\u8fd9\u4e2a\u662f\u901a\u8fc7\u540c\u6b65\u7684\u65b9\u5f0f\u53bb\u64cd\u4f5c\u7f51\u7edc\u8bf7\u6c42\uff0c\u800candroid\u672c\u8eab\u662f\u4e0d\u5141\u8bb8\u5728UI\u7ebf\u7a0b\u505a\u7f51\u7edc\u8bf7\u6c42\u64cd\u4f5c\u7684\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u81ea\u5df1\u5f00\u542f\u4e00\u4e2a\u7ebf\u7a0b\u3002<\/p>\n<p>\u5f53\u7136\uff0c<strong>OKHttp\u4e5f\u652f\u6301\u5f02\u6b65\u7ebf\u7a0b\u5e76\u4e14\u6709\u56de\u8c03\u8fd4\u56de<\/strong>\uff0c\u6709\u4e86\u4e0a\u9762\u540c\u6b65\u7684\u57fa\u7840\uff0c\u5f02\u6b65\u53ea\u8981\u7a0d\u52a0\u6539\u52a8\u5373\u53ef\u3002<\/p>\n<pre class=\"lang:default decode:true \">private void enqueue(){\r\n        Request request = new Request.Builder()\r\n                .url(\"http:\/\/publicobject.com\/helloworld.txt\")\r\n                .build();\r\n        client.newCall(request).enqueue(new Callback() {\r\n            @Override\r\n            public void onFailure(Request request, IOException e) {\r\n\r\n            }\r\n            \/\/ \u6210\u529f\u65f6\u54cd\u5e94\u7684\u56de\u8c03\u51fd\u6570\r\n            @Override\r\n            public void onResponse(Response response) throws IOException {\r\n                \/\/NOT UI Thread\r\n                if(response.isSuccessful()){\r\n                    System.out.println(response.code());\r\n                    System.out.println(response.body().string());\r\n                }\r\n            }\r\n        });\r\n    }\r\n<\/pre>\n<p>\u5c31\u662f<strong>\u5728\u540c\u6b65\u7684\u57fa\u7840\u4e0a\u8bb2execute\u6539\u6210enqueue<\/strong>\uff0c\u5e76\u4e14\u4f20\u5165\u56de\u8c03\u63a5\u53e3\uff0c\u4f46\u63a5\u53e3\u56de\u8c03\u56de\u6765\u7684\u4ee3\u7801\u662f\u5728\u975eUI\u7ebf\u7a0b\u7684\uff0c<strong>\u56e0\u6b64\u5982\u679c\u6709\u66f4\u65b0UI\u7684\u64cd\u4f5c\u8bb0\u5f97\u7528Handler\u6216\u8005\u5176\u4ed6\u65b9\u5f0f\u3002<\/strong><\/p>\n<p><strong>2\u3001POST<\/strong><\/p>\n<p>\u8bf4\u5b8cGET\u8be5\u4ecb\u7ecd\u4e9b\u5982\u4f55\u4f7f\u7528POST\uff0c<strong>POST\u60c5\u51b5\u4e0b\u6211\u4eec\u4e00\u822c\u9700\u8981\u4f20\u5165\u53c2\u6570\uff0c\u751a\u81f3\u4e00\u4e9bheader\uff0c\u4f20\u5165\u53c2\u6570\u6216\u8005header<\/strong><\/p>\n<p>\u6bd4\u5982\u4f20\u5165header<\/p>\n<pre class=\"lang:default decode:true \">Request request = new Request.Builder() \r\n.url(\"https:\/\/api.github.com\/repos\/square\/okhttp\/issues\") \r\n.header(\"User-Agent\", \"OkHttp Headers.java\") \r\n.addHeader(\"Accept\", \"application\/json; q=0.5\") \r\n.addHeader(\"Accept\", \"application\/vnd.github.v3+json\") \r\n.build();<\/pre>\n<p>\u53ef\u4ee5\u770b\u51fa\u6765\uff0c<strong>\u4f20\u5165header\u6216\u8005post\u53c2\u6570\u90fd\u662f\u4f20\u5230Request\u91cc\u9762<\/strong>\uff0c\u56e0\u6b64\u6700\u540e\u7684\u8c03\u7528\u65b9\u5f0f\u4e5f\u548cGET\u65b9\u5f0f\u4e00\u6837<\/p>\n<pre class=\"lang:default decode:true \">Response response = client.newCall(request).execute();\r\n    if (response.isSuccessful()) {\r\n        return response.body().string();\r\n    } else {\r\n        throw new IOException(\"Unexpected code \" + response);\r\n    }\r\n<\/pre>\n<p>\u8fd9\u4e2a\u4ee3\u7801\u662f\u540c\u6b65\u7f51\u7edc\u8bf7\u6c42\uff0c<strong>\u5f02\u6b65\u5c31\u6539\u6210enqueue\u5c31\u884c\u4e86\u3002<\/strong><\/p>\n<p><strong>\u8bf7\u6c42\u7f13\u5b58<\/strong><\/p>\n<p>\u5728\u7f51\u7edc\u8bf7\u6c42\u4e2d\uff0c<strong>\u7f13\u5b58\u6280\u672f\u662f\u4e00\u9879\u5e94\u7528\u6bd4\u8f83\u5e7f\u6cdb\u7684\u6280\u672f\uff0c\u9700\u8981\u5bf9\u8bf7\u6c42\u8fc7\u7684\u7f51\u7edc\u8d44\u6e90\u8fdb\u884c\u7f13\u5b58\uff0c\u800cokhttp\u4e5f\u652f\u6301\u8fd9\u4e00\u6280\u672f<\/strong>\uff0c\u4e5f\u4f7f\u7528\u5341\u5206\u65b9\u4fbf\uff0c\u524d\u6587\u4e2d\u7ecf\u5e38\u51fa\u73b0\u7684<strong>OkHttpClient<\/strong>\u8fd9\u4e2a\u65f6\u5019\u5c31\u8981\u6d3e\u9001\u7528\u573a\u4e86\u3002\u770b\u4e0b\u9762\u4ee3\u7801<\/p>\n<pre class=\"lang:default decode:true \">package com.jackchan.test.okhttptest;\r\n\r\nimport android.os.Bundle;\r\nimport android.support.v7.app.ActionBarActivity;\r\nimport android.util.Log;\r\n\r\nimport com.squareup.okhttp.Cache;\r\nimport com.squareup.okhttp.CacheControl;\r\nimport com.squareup.okhttp.Call;\r\nimport com.squareup.okhttp.Callback;\r\nimport com.squareup.okhttp.OkHttpClient;\r\nimport com.squareup.okhttp.Request;\r\nimport com.squareup.okhttp.Response;\r\n\r\nimport java.io.File;\r\nimport java.io.IOException;\r\n\r\n\r\npublic class TestActivity extends ActionBarActivity {\r\n\r\n    private final static String TAG = \"TestActivity\";\r\n    \/\/ \u8bf7\u6c42\u8d44\u6e90\u5ba2\u6237\u7aef\r\n    private final OkHttpClient client = new OkHttpClient();\r\n\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_test);\r\n        File sdcache = getExternalCacheDir();\r\n        int cacheSize = 10 * 1024 * 1024; \/\/ 10 MiB\r\n        \/\/ \u5bf9\u8d44\u6e90\u8fdb\u884c\u7f13\u5b58\r\n        client.setCache(new Cache(sdcache.getAbsoluteFile(), cacheSize));\r\n        \/\/ \u5f00\u542f\u4e00\u4e2a\u7ebf\u7a0b\r\n        new Thread(new Runnable() {\r\n            @Override\r\n            public void run() {\r\n                try {\r\n                    execute();\r\n                } catch (Exception e) {\r\n                    e.printStackTrace();\r\n                }\r\n            }\r\n        }).start();\r\n    }\r\n\r\n    public void execute() throws Exception {\r\n        Request request = new Request.Builder()\r\n                .url(\"http:\/\/publicobject.com\/helloworld.txt\")\r\n                .build();\r\n\r\n        Response response1 = client.newCall(request).execute();\r\n        if (!response1.isSuccessful()) throw new IOException(\"Unexpected code \" + response1);\r\n\r\n        String response1Body = response1.body().string();\r\n        System.out.println(\"Response 1 response:          \" + response1);\r\n        System.out.println(\"Response 1 cache response:    \" + response1.cacheResponse());\r\n        System.out.println(\"Response 1 network response:  \" + response1.networkResponse());\r\n\r\n        Response response2 = client.newCall(request).execute();\r\n        if (!response2.isSuccessful()) throw new IOException(\"Unexpected code \" + response2);\r\n\r\n        String response2Body = response2.body().string();\r\n        System.out.println(\"Response 2 response:          \" + response2);\r\n        System.out.println(\"Response 2 cache response:    \" + response2.cacheResponse());\r\n        System.out.println(\"Response 2 network response:  \" + response2.networkResponse());\r\n\r\n        System.out.println(\"Response 2 equals Response 1? \" + response1Body.equals(response2Body));\r\n\r\n    }\r\n}\r\n<\/pre>\n<p>okhttpclient\u6709\u70b9\u50cfApplication\u7684\u6982\u5ff5\uff0c\u7edf\u7b79\u7740\u6574\u4e2aokhttp\u7684\u5927\u529f\u80fd\uff0c\u901a\u8fc7\u5b83\u8bbe\u7f6e\u7f13\u5b58\u76ee\u5f55\u3002<\/p>\n<p>response1 \u7684\u7ed3\u679c\u5728networkresponse\uff0c\u4ee3\u8868\u662f\u4ece\u7f51\u7edc\u8bf7\u6c42\u52a0\u8f7d\u8fc7\u6765\u7684\uff0c\u800cresponse2\u7684networkresponse \u5c31\u4e3anull\uff0c\u800ccacheresponse\u6709\u6570\u636e\uff0c\u56e0\u4e3a\u6211\u8bbe\u7f6e\u4e86\u7f13\u5b58\u56e0\u6b64\u7b2c\u4e8c\u6b21\u8bf7\u6c42\u65f6\u53d1\u73b0\u7f13\u5b58\u91cc\u6709\u5c31\u4e0d\u518d\u53bb\u8d70\u7f51\u7edc\u8bf7\u6c42\u4e86\u3002<br \/>\n\u4f46\u6709\u65f6\u5019\u5373\u4f7f\u5728\u6709\u7f13\u5b58\u7684\u60c5\u51b5\u4e0b\u6211\u4eec\u4f9d\u7136\u9700\u8981\u53bb\u540e\u53f0\u8bf7\u6c42\u6700\u65b0\u7684\u8d44\u6e90\uff08\u6bd4\u5982\u8d44\u6e90\u66f4\u65b0\u4e86\uff09\u8fd9\u4e2a\u65f6\u5019\u53ef\u4ee5\u4f7f\u7528\u5f3a\u5236\u8d70\u7f51\u7edc\u6765\u8981\u6c42\u5fc5\u987b\u8bf7\u6c42\u7f51\u7edc\u6570\u636e<\/p>\n<pre class=\"lang:default decode:true \">public void execute() throws Exception {\r\n        Request request = new Request.Builder()\r\n                .url(\"http:\/\/publicobject.com\/helloworld.txt\")\r\n                .build();\r\n\r\n        Response response1 = client.newCall(request).execute();\r\n        if (!response1.isSuccessful()) throw new IOException(\"Unexpected code \" + response1);\r\n\r\n        String response1Body = response1.body().string();\r\n        System.out.println(\"Response 1 response:          \" + response1);\r\n        System.out.println(\"Response 1 cache response:    \" + response1.cacheResponse());\r\n        System.out.println(\"Response 1 network response:  \" + response1.networkResponse());\r\n\r\n        request = request.newBuilder().cacheControl(CacheControl.FORCE_NETWORK).build();\r\n        Response response2 = client.newCall(request).execute();\r\n        if (!response2.isSuccessful()) throw new IOException(\"Unexpected code \" + response2);\r\n\r\n        String response2Body = response2.body().string();\r\n        System.out.println(\"Response 2 response:          \" + response2);\r\n        System.out.println(\"Response 2 cache response:    \" + response2.cacheResponse());\r\n        System.out.println(\"Response 2 network response:  \" + response2.networkResponse());\r\n\r\n        System.out.println(\"Response 2 equals Response 1? \" + response1Body.equals(response2Body));\r\n\r\n    }\r\n<\/pre>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d<br \/>\nresponse2\u5bf9\u5e94\u7684request\u53d8\u6210<\/p>\n<pre class=\"lang:default decode:true \">request = request.newBuilder().cacheControl(CacheControl.FORCE_NETWORK).build();\r\n<\/pre>\n<p><strong>\u53d6\u6d88\u64cd\u4f5c<\/strong><\/p>\n<p>\u7f51\u7edc\u64cd\u4f5c\u4e2d\uff0c\u7ecf\u5e38\u4f1a\u4f7f\u7528\u5230\u5bf9\u8bf7\u6c42\u7684cancel\u64cd\u4f5c\uff0c<strong>okhttp\u7684\u4e5f\u63d0\u4f9b\u4e86\u8fd9\u65b9\u9762\u7684\u63a5\u53e3<\/strong>\uff0ccall\u7684cancel\u64cd\u4f5c\u3002\u4f7f\u7528Call.cancel()\u53ef\u4ee5\u7acb\u5373\u505c\u6b62\u6389\u4e00\u4e2a\u6b63\u5728\u6267\u884c\u7684call\u3002\u5982\u679c\u4e00\u4e2a\u7ebf\u7a0b\u6b63\u5728\u5199\u8bf7\u6c42\u6216\u8005\u8bfb\u54cd\u5e94\uff0c\u5c06\u4f1a\u5f15\u53d1IOException\uff0c\u540c\u65f6\u53ef\u4ee5\u901a\u8fc7Request.Builder.tag(Object tag)\u7ed9\u8bf7\u6c42\u8bbe\u7f6e\u4e00\u4e2a\u6807\u7b7e\uff0c\u5e76\u4f7f\u7528OkHttpClient.cancel(Object tag)\u6765\u53d6\u6d88\u6240\u6709\u5e26\u6709\u8fd9\u4e2atag\u7684call\u3002\u4f46\u5982\u679c\u8be5\u8bf7\u6c42\u5df2\u7ecf\u5728\u505a\u8bfb\u5199\u64cd\u4f5c\u7684\u65f6\u5019\uff0ccancel\u662f\u65e0\u6cd5\u6210\u529f\u7684\uff0c\u4f1a\u629b\u51faIOException\u5f02\u5e38\u3002<\/p>\n<pre class=\"lang:default decode:true \">public void canceltest() throws Exception {\r\n        Request request = new Request.Builder()\r\n                .url(\"http:\/\/httpbin.org\/delay\/2\") \/\/ This URL is served with a 2 second delay.\r\n                .build();\r\n\r\n        final long startNanos = System.nanoTime();\r\n        final Call call = client.newCall(request);\r\n\r\n        \/\/ Schedule a job to cancel the call in 1 second.\r\n        executor.schedule(new Runnable() {\r\n            @Override\r\n            public void run() {\r\n                System.out.printf(\"%.2f Canceling call.%n\", (System.nanoTime() - startNanos) \/ 1e9f);\r\n                call.cancel();\r\n                System.out.printf(\"%.2f Canceled call.%n\", (System.nanoTime() - startNanos) \/ 1e9f);\r\n            }\r\n        }, 1, TimeUnit.SECONDS);\r\n\r\n        try {\r\n            System.out.printf(\"%.2f Executing call.%n\", (System.nanoTime() - startNanos) \/ 1e9f);\r\n            Response response = call.execute();\r\n            System.out.printf(\"call is cancel:\" + call.isCanceled() + \"%n\");\r\n            System.out.printf(\"%.2f Call was expected to fail, but completed: %s%n\",\r\n                    (System.nanoTime() - startNanos) \/ 1e9f, response);\r\n        } catch (IOException e) {\r\n            System.out.printf(\"%.2f Call failed as expected: %s%n\",\r\n                    (System.nanoTime() - startNanos) \/ 1e9f, e);\r\n        }\r\n    }\r\n<\/pre>\n<pre class=\"lang:default decode:true \">package com.test.okhttp;\r\n\r\nimport java.io.File;\r\nimport java.io.FileOutputStream;\r\nimport java.io.IOException;\r\nimport java.io.InputStream;\r\nimport java.util.List;\r\nimport java.util.concurrent.TimeUnit;\r\n\r\nimport org.apache.http.client.utils.URLEncodedUtils;\r\nimport org.apache.http.message.BasicNameValuePair;\r\n\r\nimport com.squareup.okhttp.Call;\r\nimport com.squareup.okhttp.Callback;\r\nimport com.squareup.okhttp.OkHttpClient;\r\nimport com.squareup.okhttp.Request;\r\nimport com.squareup.okhttp.Response;\r\n\r\npublic class OkHttpClientManager {\r\n\r\n\tprivate static final OkHttpClient mOkHttpClient = new OkHttpClient();\r\n\r\n\tstatic {\r\n\r\n\t\tmOkHttpClient.setConnectTimeout(30, TimeUnit.SECONDS);\r\n\r\n\t}\r\n\r\n\t\/**\r\n\t * \r\n\t * \u8be5\u65b9\u6cd5\u4e0d\u4f1a\u5f00\u542f\u5f02\u6b65\u7ebf\u7a0b\u3002\r\n\t * \r\n\t * @param request\r\n\t * \r\n\t * @return\r\n\t * \r\n\t * @throws IOException\r\n\t *\/\r\n\r\n\tpublic static Response execute(Request request) throws IOException {\r\n\r\n\t\treturn mOkHttpClient.newCall(request).execute();\r\n\r\n\t}\r\n\r\n\t\/**\r\n\t * \r\n\t * \u5f00\u542f\u5f02\u6b65\u7ebf\u7a0b\u8bbf\u95ee\u7f51\u7edc\r\n\t * \r\n\t * @param request\r\n\t * \r\n\t * @param responseCallback\r\n\t *\/\r\n\r\n\tpublic static void enqueue(Request request, Callback responseCallback) {\r\n\r\n\t\tmOkHttpClient.newCall(request).enqueue(responseCallback);\r\n\r\n\t}\r\n\r\n\t\/**\r\n\t * \r\n\t * \u5f00\u542f\u5f02\u6b65\u7ebf\u7a0b\u8bbf\u95ee\u7f51\u7edc, \u4e14\u4e0d\u5728\u610f\u8fd4\u56de\u7ed3\u679c\uff08\u5b9e\u73b0\u7a7acallback\uff09\r\n\t * \r\n\t * @param request\r\n\t *\/\r\n\r\n\tpublic static void enqueue(Request request) {\r\n\r\n\t\tmOkHttpClient.newCall(request).enqueue(new Callback() {\r\n\r\n\t\t\t@Override\r\n\t\t\tpublic void onResponse(Response arg0) throws IOException {\r\n\r\n\t\t\t}\r\n\r\n\t\t\t@Override\r\n\t\t\tpublic void onFailure(Request arg0, IOException arg1) {\r\n\r\n\t\t\t}\r\n\r\n\t\t});\r\n\r\n\t}\r\n\r\n\tpublic static String getStringFromServer(String url) throws IOException {\r\n\r\n\t\tRequest request = new Request.Builder().url(url).build();\r\n\r\n\t\tResponse response = execute(request);\r\n\r\n\t\tif (response.isSuccessful()) {\r\n\r\n\t\t\tString responseUrl = response.body().string();\r\n\r\n\t\t\treturn responseUrl;\r\n\r\n\t\t} else {\r\n\r\n\t\t\tthrow new IOException(\"Unexpected code \" + response);\r\n\r\n\t\t}\r\n\r\n\t}\r\n\r\n\tprivate static final String CHARSET_NAME = \"UTF-8\";\r\n\r\n\t\/**\r\n\t * \r\n\t * \u8fd9\u91cc\u4f7f\u7528\u4e86HttpClinet\u7684API\u3002\u53ea\u662f\u4e3a\u4e86\u65b9\u4fbf\r\n\t * \r\n\t * @param params\r\n\t * \r\n\t * @return\r\n\t *\/\r\n\r\n\tpublic static String formatParams(List&lt;BasicNameValuePair&gt; params) {\r\n\r\n\t\treturn URLEncodedUtils.format(params, CHARSET_NAME);\r\n\t}\r\n\r\n\t\/**\r\n\t * \r\n\t * \u4e3aHttpGet \u7684 url \u65b9\u4fbf\u7684\u6dfb\u52a0\u591a\u4e2aname value \u53c2\u6570\u3002\r\n\t * \r\n\t * @param url\r\n\t * \r\n\t * @param params\r\n\t * \r\n\t * @return\r\n\t *\/\r\n\r\n\tpublic static String attachHttpGetParams(String url,\r\n\t\t\tList&lt;BasicNameValuePair&gt; params) {\r\n\t\treturn url + \"?\" + formatParams(params);\r\n\t}\r\n\r\n\t\/**\r\n\t * \r\n\t * \u4e3aHttpGet \u7684 url \u65b9\u4fbf\u7684\u6dfb\u52a01\u4e2aname value \u53c2\u6570\u3002\r\n\t * \r\n\t * @param url\r\n\t * \r\n\t * @param name\r\n\t * \r\n\t * @param value\r\n\t * \r\n\t * @return\r\n\t *\/\r\n\r\n\tpublic static String attachHttpGetParam(String url, String name,\r\n\t\t\tString value) {\r\n\t\treturn url + \"?\" + name + \"=\" + value;\r\n\t}\r\n\t\/**\r\n\t * \u4e0b\u8f7d\u5185\u5bb9\r\n\t * @param url\r\n\t * @param destFileDir\r\n\t *\/\r\n\tpublic static void downloadAsyn(final String url, final String destFileDir) {\r\n\t\tfinal Request request = new Request.Builder().url(url).build();\r\n\t\tfinal Call call = mOkHttpClient.newCall(request);\r\n\t\tcall.enqueue(new Callback() {\r\n\t\t\t@Override\r\n\t\t\tpublic void onFailure(final Request request, final IOException e) {\r\n\r\n\t\t\t}\r\n\r\n\t\t\t@Override\r\n\t\t\tpublic void onResponse(Response response) {\r\n\t\t\t\tSystem.out.println(\"Download..........\");\r\n\t\t\t\tInputStream is = null;\r\n\t\t\t\tbyte[] buf = new byte[2048];\r\n\t\t\t\tint len = 0;\r\n\t\t\t\tFileOutputStream fos = null;\r\n\t\t\t\ttry {\r\n\t\t\t\t\tis = response.body().byteStream();\r\n\t\t\t\t\tFile file = new File(destFileDir, getFileName(url));\r\n\t\t\t\t\tfos = new FileOutputStream(file);\r\n\t\t\t\t\twhile ((len = is.read(buf)) != -1) {\r\n\t\t\t\t\t\tfos.write(buf, 0, len);\r\n\t\t\t\t\t}\r\n\t\t\t\t\tfos.flush();\r\n\t\t\t\t} catch (IOException e) {\r\n\t\t\t\t} finally {\r\n\t\t\t\t\ttry {\r\n\t\t\t\t\t\tif (is != null)\r\n\t\t\t\t\t\t\tis.close();\r\n\t\t\t\t\t} catch (IOException e) {\r\n\t\t\t\t\t}\r\n\t\t\t\t\ttry {\r\n\t\t\t\t\t\tif (fos != null)\r\n\t\t\t\t\t\t\tfos.close();\r\n\t\t\t\t\t} catch (IOException e) {\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\r\n\t\t\t}\r\n\t\t});\r\n\t}\r\n\r\n\tprivate static String getFileName(String path) {\r\n\t\tint separatorIndex = path.lastIndexOf(\"\/\");\r\n\t\treturn (separatorIndex &lt; 0) ? path : path.substring(separatorIndex + 1,\r\n\t\t\t\tpath.length());\r\n\t}\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>OKHttp\u662f\u4e00\u6b3e\u9ad8\u6548\u7684HTTP\u5ba2\u6237\u7aef\uff0c\u652f\u6301\u8fde\u63a5\u540c\u4e00\u5730\u5740\u7684\u94fe\u63a5\u5171\u4eab\u540c\u4e00\u4e2asocket\uff0c\u901a\u8fc7\u8fde\u63a5\u6c60\u6765\u51cf\u5c0f\u54cd\u5e94\u5ef6\u8fdf [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[52,51],"class_list":["post-923","post","type-post","status-publish","format-standard","hentry","category-android","tag-android","tag-okhttp"],"_links":{"self":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/923","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=923"}],"version-history":[{"count":1,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/923\/revisions"}],"predecessor-version":[{"id":924,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/923\/revisions\/924"}],"wp:attachment":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=923"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=923"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=923"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}