{"id":367,"date":"2023-02-25T08:27:01","date_gmt":"2023-02-25T00:27:01","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=367"},"modified":"2023-04-30T14:41:32","modified_gmt":"2023-04-30T06:41:32","slug":"okhttp3-learning-3-get-request","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/okhttp3-learning-3-get-request\/","title":{"rendered":"OKHttp3\u5b66\u4e60\u4e4b\u4e09\uff1aGET\u8bf7\u6c42"},"content":{"rendered":"<p>\u9700\u6c42\uff1a\u5b9e\u73b0\u7528\u6237\u4fe1\u606f\u83b7\u53d6\u5e76\u5728UI\u754c\u9762\u4e0a\u663e\u793a<\/p>\n<p>\u5728app\u6a21\u5757\u7684build.gradle\u914d\u7f6e<\/p>\n<p><!-- more --><\/p>\n<pre><code>compile &#039;com.squareup.okhttp3:okhttp:3.4.2&#039;\ncompile &#039;com.squareup.picasso:picasso:2.5.2&#039;<\/code><\/pre>\n<p>\u5b9e\u73b0\u6e90\u7801<\/p>\n<pre><code class=\"language-java\">public void getRequest(View view) {\n    OkHttpClient client = new OkHttpClient();\n    String url = SERVER_ADDRESS + &quot;\/user\/info?id=1&quot;;\n    Request request = new Request.Builder().url(url).get().build();\n\n    client.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            if (response.isSuccessful()) {\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                    showUserInfo(result);\n                }\n            }\n        }\n    });\n}\n\nprivate void showUserInfo(final String result) {\n    \/\/\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u5b9e\u73b0UI\u66f4\u65b0\n    runOnUiThread(new Runnable() {\n        @Override\n        public void run() {\n            try {\n                JSONObject json = new JSONObject(result);\n                String id = json.optString(&quot;id&quot;);\n                String username = json.optString(&quot;username&quot;);\n                String head_url = json.optString(&quot;head_url&quot;);\n                mTvNickName.setText(username);\n                \/\/\u5229\u7528Picasso\u56fe\u7247\u52a0\u8f7d\u6846\u67b6\u5b9e\u73b0\u5934\u50cf\u5f02\u6b65\u52a0\u8f7d\n                Picasso.with(MainActivity.this).load(head_url).resize(100, 100).centerCrop().into(mIvAvatar);\n            } catch (JSONException e) {\n                e.printStackTrace();\n            }\n        }\n    });\n}<\/code><\/pre>\n<p>\u670d\u52a1\u5668\u8fd4\u56de\u6570\u636e<\/p>\n<pre><code>{\n    &quot;head_url&quot;: &quot;http:\/\/www.appblog.cn\/images\/avatar.png&quot;, \n    &quot;id&quot;: &quot;1&quot;, \n    &quot;username&quot;: &quot;Joe.Ye&quot;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9700\u6c42\uff1a\u5b9e\u73b0\u7528\u6237\u4fe1\u606f\u83b7\u53d6\u5e76\u5728UI\u754c\u9762\u4e0a\u663e\u793a \u5728app\u6a21\u5757\u7684build.gradle\u914d\u7f6e compile &#038;#03 [&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-367","post","type-post","status-publish","format-standard","hentry","category-okhttp"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/367","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=367"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/367\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=367"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=367"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=367"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}