{"id":379,"date":"2023-02-25T08:37:03","date_gmt":"2023-02-25T00:37:03","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=379"},"modified":"2023-04-30T14:39:48","modified_gmt":"2023-04-30T06:39:48","slug":"retrofit2-learning-1-helloworld","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/retrofit2-learning-1-helloworld\/","title":{"rendered":"Retrofit2\u5b66\u4e60\u4e4b\u4e00\uff1aHelloWorld"},"content":{"rendered":"<h2>Retrofit\u7b80\u4ecb<\/h2>\n<p>Retrofit\u662fSquare\u516c\u53f8\u5f00\u53d1\u7684\u4e00\u6b3e\u9488\u5bf9Android\u7f51\u7edc\u8bf7\u6c42\u7684\u6846\u67b6\uff0cRetrofit\u4e0eOKhttp\u5171\u540c\u51fa\u81ea\u4e8eSquare\u516c\u53f8\uff0cRetrofit2\u5e95\u5c42\u57fa\u4e8eOkHttp\u5b9e\u73b0\u7684\uff0cOkHttp\u73b0\u5728\u5df2\u7ecf\u5f97\u5230Google\u5b98\u65b9\u8ba4\u53ef\uff0c\u5927\u91cf\u7684app\u90fd\u91c7\u7528OkHttp\u505a\u7f51\u7edc\u8bf7\u6c42\u3002<\/p>\n<p><!-- more --><\/p>\n<p>Retrofit\u628a\u7f51\u7edc\u8bf7\u6c42\u4ea4\u7ed9OKhttp\uff0c\u53ea\u9700\u8981\u901a\u8fc7\u7b80\u5355\u7684\u914d\u7f6e\u5c31\u80fd\u4f7f\u7528retrofit\u5b9e\u73b0\u7f51\u7edc\u8bf7\u6c42\u3002<\/p>\n<h2>\u5b89\u88c5\u4f7f\u7528<\/h2>\n<p>Github\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/square\/retrofit\">https:\/\/github.com\/square\/retrofit<\/a><\/p>\n<p>\u5728app\u5c42\u7ea7\u7684build.gradle\u914d\u7f6e<\/p>\n<pre><code>compile &#039;com.squareup.retrofit2:retrofit:2.1.0&#039;<\/code><\/pre>\n<h2>Hello World<\/h2>\n<h3>\u521b\u5efa\u4e1a\u52a1\u8bf7\u6c42\u63a5\u53e3<\/h3>\n<pre><code class=\"language-java\">public interface Api {\n\n    @GET(&quot;user\/1&quot;)\n    Call&lt;ResponseBody&gt; getUser();\n\n}<\/code><\/pre>\n<h3>\u521b\u5efa\u4e00\u4e2aRetrofit\u7684\u5b9e\u4f8b\uff0c\u5e76\u5229\u7528Retrofit\u5b9e\u4f8b\u521b\u5efa\u63a5\u53e3\u5bf9\u8c61\u548c\u8c03\u7528\u63a5\u53e3\u65b9\u6cd5<\/h3>\n<pre><code class=\"language-java\">public void hello(View view) {\n    Retrofit retrofit = new Retrofit.Builder()\n            .baseUrl(SERVER_ADDRESS)\n            .build();\n\n    Api api = retrofit.create(Api.class);\n\n    Call&lt;ResponseBody&gt; call = api.getUser();\n    call.enqueue(new Callback&lt;ResponseBody&gt;() {\n        @Override\n        public void onResponse(Call&lt;ResponseBody&gt; call, Response&lt;ResponseBody&gt; response) {\n            \/\/\u5728UI\u4e3b\u7ebf\u7a0b\u8fd0\u884c\n            if (response.isSuccessful()) {\n                Log.i(TAG, &quot;\u8fd4\u56de\u6210\u529f&quot;);\n                ResponseBody body = response.body();\n                if (body != null) {\n                    String result = null;\n                    try {\n                        result = body.string();\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                        Picasso.with(MainActivity.this).load(head_url).resize(100, 100).centerCrop().into(mIvAvatar);\n                    } catch (IOException e) {\n                        e.printStackTrace();\n                    } catch (JSONException e) {\n                        e.printStackTrace();\n                    } finally {\n                        body.close();\n                    }\n                }\n            }\n        }\n\n        @Override\n        public void onFailure(Call&lt;ResponseBody&gt; call, Throwable t) {\n            Log.i(TAG, &quot;\u8bf7\u6c42\u5931\u8d25: &quot; + t.getLocalizedMessage());\n        }\n    });\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Retrofit\u7b80\u4ecb Retrofit\u662fSquare\u516c\u53f8\u5f00\u53d1\u7684\u4e00\u6b3e\u9488\u5bf9Android\u7f51\u7edc\u8bf7\u6c42\u7684\u6846\u67b6\uff0cRetro [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[130],"tags":[],"class_list":["post-379","post","type-post","status-publish","format-standard","hentry","category-retrofit"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/379","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=379"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/379\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}