{"id":365,"date":"2023-02-25T08:20:10","date_gmt":"2023-02-25T00:20:10","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=365"},"modified":"2023-04-30T14:41:59","modified_gmt":"2023-04-30T06:41:59","slug":"okhttp3-learning-1-helloworld","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/okhttp3-learning-1-helloworld\/","title":{"rendered":"OKHttp3\u5b66\u4e60\u4e4b\u4e00\uff1aHelloWorld"},"content":{"rendered":"<h2>\u7b80\u4ecb<\/h2>\n<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\uff0c\u8fd8\u6709\u900f\u660e\u7684GZIP\u538b\u7f29\uff0c\u8bf7\u6c42\u7f13\u5b58\u7b49\u4f18\u52bf\u3002<\/p>\n<p><!-- more --><\/p>\n<h2>\u5b66\u4e60\u8def\u5f84<\/h2>\n<ul>\n<li>\u5b89\u88c5\u4f7f\u7528<\/li>\n<li>HTTP\u534f\u8bae\u7b80\u4ecb<\/li>\n<li>GET\u8bf7\u6c42<\/li>\n<li>POST\u8bf7\u6c42\uff08Form\u8868\u5355\u5f62\u5f0f\uff09\uff1amultipart\/form-data<\/li>\n<li>POST\u8bf7\u6c42\uff08JSON\u53c2\u6570\u5f62\u5f0f\uff09\uff1aapplication\/json<\/li>\n<li>\u6587\u4ef6\u4e0b\u8f7d\uff08\u7b80\u5355\u65b9\u5f0f\uff09<\/li>\n<li>\u6587\u4ef6\u4e0b\u8f7d\uff08\u62e6\u622a\u5668\u65b9\u5f0f\uff09<\/li>\n<li>\u5e26\u53c2\u6570\u6587\u4ef6\u4e0a\u4f20<\/li>\n<li>\u6587\u4ef6\u4e0a\u4f20\uff08\u62e6\u622a\u5668\u83b7\u53d6\u8fdb\u5ea6\uff09<\/li>\n<\/ul>\n<h2>\u5b89\u88c5\u4f7f\u7528<\/h2>\n<p>Github\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/square\/okhttp\">https:\/\/github.com\/square\/okhttp<\/a><\/p>\n<p>\u5728app\u5c42\u7ea7\u7684build.gradle\u914d\u7f6e<\/p>\n<pre><code>compile &#039;com.squareup.okhttp3:okhttp:3.4.2&#039;<\/code><\/pre>\n<h2>Hello World<\/h2>\n<pre><code class=\"language-java\">OkHttpClient client = new OkHttpClient();\nString url = SERVER_ADDRESS;\nRequest request = new Request.Builder().url(url).build();\n\nclient.newCall(request).enqueue(new Callback() {\n    @Override\n    public void onFailure(Call call, IOException e) {\n        Log.i(TAG, &quot;\u8bf7\u6c42\u5931\u8d25: &quot; + e.getLocalizedMessage());\n    }\n\n    @Override\n    public void onResponse(Call call, Response response) throws IOException {\n        ResponseBody body = response.body();\n        if (body != null) {\n            String result = body.string();\n            Log.i(TAG, &quot;\u8fd4\u56de\u6210\u529f: &quot; + result);\n            body.close();\n        }\n    }\n});<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7b80\u4ecb 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[116],"tags":[],"class_list":["post-365","post","type-post","status-publish","format-standard","hentry","category-okhttp"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/365","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=365"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/365\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}