{"id":381,"date":"2023-02-25T08:38:31","date_gmt":"2023-02-25T00:38:31","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=381"},"modified":"2023-04-30T14:39:23","modified_gmt":"2023-04-30T06:39:23","slug":"retrofit2-learning-3-get-request","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/retrofit2-learning-3-get-request\/","title":{"rendered":"Retrofit2\u5b66\u4e60\u4e4b\u4e09\uff1aGET\u8bf7\u6c42"},"content":{"rendered":"<h2>\u7b80\u4ecb<\/h2>\n<p>Retrofit2 GET\u8bf7\u6c42\u7684\u8bf7\u6c42\u53c2\u6570\u652f\u6301Query\u3001RESTful Path\u3001Map\u53ca\u5176\u4efb\u610f\u7ec4\u5408\u5f62\u5f0f\u3002<\/p>\n<p>\u5b98\u65b9\u6587\u6863\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"http:\/\/square.github.io\/retrofit\/\">http:\/\/square.github.io\/retrofit\/<\/a><\/p>\n<p><!-- more --><\/p>\n<h2>GET\u8bf7\u6c42<\/h2>\n<h3>\u521b\u5efa\u4e1a\u52a1\u8bf7\u6c42\u63a5\u53e3<\/h3>\n<pre><code class=\"language-java\">public interface Api {\n\n    \/**\n     * Query\u53c2\u6570\n     * @param user_id\n     * @return\n     *\/\n    @GET(&quot;user\/info&quot;)\n    Call&lt;User&gt; getUserInfoWithQuery(@Query(&quot;id&quot;) int user_id);\n\n    \/**\n     * Path\u53c2\u6570\n     * @param user_id\n     * @return\n     *\/\n    @GET(&quot;user\/{id}&quot;)\n    Call&lt;User&gt; getUserInfoWithPath(@Path(&quot;id&quot;) int user_id);\n\n    \/**\n     * Map\u53c2\u6570\n     * @param params\n     * @return\n     *\/\n    @GET(&quot;user\/info&quot;)\n    Call&lt;User&gt; getUserInfoWithMap(@QueryMap() Map&lt;String, String&gt; params);\n\n}<\/code><\/pre>\n<h3>\u521b\u5efa\u4e00\u4e2aRetrofit\u7684\u5b9e\u4f8b\uff0c\u7136\u540e\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 getRequest(View view) {\n    Retrofit retrofit = new Retrofit.Builder()\n            .baseUrl(SERVER_ADDRESS)\n            .addConverterFactory(GsonConverterFactory.create())\n            .build();\n\n    api = retrofit.create(Api.class);\n\n    Map&lt;String, String&gt; map = new HashMap&lt;&gt;();\n    map.put(&quot;id&quot;, &quot;8&quot;);\n    map.put(&quot;name&quot;, &quot;AndroidiOS.cc&quot;);\n\n    \/\/api.getUserInfoWithQuery(8).enqueue(new Callback&lt;User&gt;() {\n    \/\/api.getUserInfoWithPath(8).enqueue(new Callback&lt;User&gt;() {\n    api.getUserInfoWithMap(map).enqueue(new Callback&lt;User&gt;() {\n        @Override\n        public void onResponse(Call&lt;User&gt; call, Response&lt;User&gt; response) {\n            \/\/\u5728UI\u4e3b\u7ebf\u7a0b\u8fd0\u884c\n            if (response.isSuccessful()) {\n                Log.i(TAG, &quot;\u8fd4\u56de\u6210\u529f&quot;);\n                User user = response.body();\n                if (user != null) {\n                    Toast.makeText(MainActivity.this, &quot;User Id: &quot; + user.getId(), Toast.LENGTH_SHORT).show();\n                }\n            }\n        }\n\n        @Override\n        public void onFailure(Call&lt;User&gt; call, Throwable t) {\n            Log.i(TAG, &quot;\u8bf7\u6c42\u5931\u8d25: &quot; + t.getLocalizedMessage());\n        }\n    });\n}<\/code><\/pre>\n<p>\u4f9d\u6b21\u8c03\u7528 <code>getUserInfoWithQuery<\/code>\u3001<code>getUserInfoWithPath<\/code> \u548c <code>getUserInfoWithMap<\/code>\uff0c\u670d\u52a1\u7aef\u63a5\u6536\u7684\u7684\u8bf7\u6c42\u5934\u5982\u4e0b\uff0c\u53ef\u89c1Map\u53c2\u6570\u53ea\u662fQuery\u53c2\u6570\u7684\u62fc\u63a5\u7ec4\u5408\u3002<\/p>\n<pre><code>&quot;GET \/user\/info?id=8 HTTP\/1.1&quot; 200 -\n&quot;GET \/user\/8 HTTP\/1.1&quot; 200 -\n&quot;GET \/user\/info?name=AppBlog.CN&amp;id=8 HTTP\/1.1&quot; 200 -<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7b80\u4ecb Retrofit2 GET\u8bf7\u6c42\u7684\u8bf7\u6c42\u53c2\u6570\u652f\u6301Query\u3001RESTful Path\u3001Map\u53ca\u5176\u4efb\u610f\u7ec4\u5408\u5f62\u5f0f [&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-381","post","type-post","status-publish","format-standard","hentry","category-retrofit"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/381","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=381"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/381\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}