{"id":1130,"date":"2017-11-25T21:50:00","date_gmt":"2017-11-25T13:50:00","guid":{"rendered":"https:\/\/www.ray650128.com\/wordpress\/?p=1130"},"modified":"2021-08-22T22:37:34","modified_gmt":"2021-08-22T14:37:34","slug":"android%e4%bd%bf%e7%94%a8retrofit%e8%88%87api%e9%80%a3%e7%b7%9a","status":"publish","type":"post","link":"https:\/\/blog.ray650128.com\/?p=1130","title":{"rendered":"[Android]\u4f7f\u7528Retrofit\u8207API\u9023\u7dda"},"content":{"rendered":"\n<p>Retrofit\u662f\u4e00\u6b3e\u5c08\u9580\u7528\u4f86\u8655\u7406\u7db2\u8def\u8acb\u6c42\u7684\u6846\u67b6\uff0c\u7531\u77e5\u540d\u7684OkHttp\u958b\u767c\u5718\u968a<a href=\"https:\/\/square.github.io\/\" data-type=\"URL\" data-id=\"https:\/\/square.github.io\/\" target=\"_blank\" rel=\"noreferrer noopener\">Square<\/a>\u6240\u958b\u767c\u3002<\/p>\n\n\n\n<p>Retrofit\u9075\u5faaRESTful\u7684\u8a2d\u8a08\u98a8\u683c\uff0c\u5177\u6709\u7c21\u55ae\u3001\u6613\u4e0a\u624b\u7684\u7279\u6027\uff0c\u540c\u6642\u4e5f\u652f\u63f4JSON\u3001XML\u7b49\u8cc7\u6599\u683c\u5f0f\u5e8f\u5217\u5316\u3002<\/p>\n\n\n\n<p>Retrofit\u7684\u5e95\u5c64\u57fa\u65bcOkHttp\uff0c\u4e00\u822c\u4f86\u8aaa\u4f7f\u7528Retrofit\u5167\u5efa\u7684OkHttp Client\u5373\u53ef\u3002<\/p>\n\n\n\n<p>\u4f46\u5982\u679c\u6709\u7279\u6b8a\u9700\u6c42\uff0c\u6bd4\u65b9\u8aaa\u81ea\u8a02Header\u6b04\u4f4d\u6216\u662f\u52a0\u5165\u6514\u622a\u5668\u7b49\uff0c\u90a3\u9ebc\u4e5f\u53ef\u4ee5\u81ea\u8a02\u4e00\u500bOkHttp Client\u7269\u4ef6\u7d66Retrofit\u4f7f\u7528\uff0c\u5f48\u6027\u975e\u5e38\u7684\u5927\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4f7f\u7528\u65b9\u5f0f<\/h2>\n\n\n\n<p>1.\u5728\u4f7f\u7528Retrofit\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5728Android Studio\u5c08\u6848\u4e2d\u52a0\u5165Retrofit\u7684\u4f9d\u8cf4\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\" line-numbers\">\/\/ Retrofit \u672c\u9ad4\ncompile 'com.squareup.retrofit2:retrofit:2.1.0'\n\/\/ Retrofit Gson \u8f49\u63db\u5668\ncompile 'com.squareup.retrofit2:converter-gson:2.1.0'<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>2.\u7531\u65bcRetrofit\u9700\u8981internet\u6b0a\u9650\u624d\u80fd\u4f7f\u7528\uff0c\u56e0\u6b64\u8981\u5728Android Manifest.xml\u4e2d\u52a0\u5165\u6b0a\u9650\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"xml\" class=\"language-xml line-numbers\">&lt;uses-permission android:name=\"android.permission.INTERNET\" \/&gt;<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>3.\u5efa\u7acb\u8cc7\u6599\u6a21\u578b\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin line-numbers\">data class Student(\n    val exams: ArrayList&lt;Exam&gt;,\n    val id: Int,\n    val name: String\n)\n\ndata class Exam(\n    val score: Int,\n    val subjectName: String\n)<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>4.\u5efa\u7acbApiService Interface\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">interface ApiService {\n\n    @GET(\"api_demo\/students\")\n    fun getStudents(): Call&lt;ArrayList&lt;Student&gt;&gt;\n\n    @GET(\"api_demo\/students\/{id}\")\n    fun getStudent(@Path(\"id\") id: Int): Call&lt;Student&gt;\n\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>5.\u5efa\u7acbRetrofitClient\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin line-numbers\">object RetrofitClient {\n\n    private val retrofitClient: Retrofit.Builder by lazy {\n        Retrofit.Builder()\n            .baseUrl(\"https:\/\/my-json-server.typicode.com\/ray650128\")\n            .addConverterFactory(GsonConverterFactory.create())\n    }\n\n    val api: ApiService\n        get() = retrofitClient.build().create(ApiService::class.java)\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>6.\u547c\u53ebRetrofitClient.api\u4f86\u8acb\u6c42\u7db2\u8def\u4e0a\u7684\u8cc7\u6599\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin line-numbers\">class MainActivity : AppCompatActivity() {\n    \n    private val TAG = MainActivity::class.java.simpleName\n\n    private var students = ArrayList&lt;Student&gt;()\n\n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n        setContentView(R.layout.activity_main)\n\n        getStudents()\n    }\n\n    fun getStudents() {\n        val call = RetrofitClient.api.getStudents()\n        call.enqueue(object : Callback&lt;ArrayList&lt;Student&gt;&gt; {\n            override fun onResponse(call: Call&lt;ArrayList&lt;Student&gt;&gt;, response: Response&lt;ArrayList&lt;Student&gt;&gt;) {\n                if (response.isSuccessful) {\n                    students  = response.body()\n                    Log.e(TAG, \"${students.size}\")\n                }\n            }\n\n            override fun onFailure(call: Call&lt;ArrayList&lt;Student&gt;&gt;, t: Throwable) {\n                Log.e(TAG, \"error: ${t.message}\")\n            }\n        })\n    }\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>\u4ee5\u4e0a\u5c31\u662fRetrofit\u7684\u7c21\u55ae\u793a\u7bc4\u3002<\/p>\n\n\n\n<p>\u96d6\u7136\u4e0a\u9762\u53ea\u5c55\u793a\u4e86HTTP\u4e2d\u7684GET\u8acb\u6c42\u65b9\u6cd5\uff0c\u4f46\u53ea\u8981\u77ad\u89e3\u4e86\u5982\u4f55\u5be6\u4f5c\u5f8c\u3002<\/p>\n\n\n\n<p>\u5176\u4ed6\u8af8\u5982POST\u3001PUT\u3001PATCH\u3001DELETE\u7b49\u8acb\u6c42\uff0c\u5176\u5be6\u662f\u5927\u540c\u5c0f\u7570\u7684\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u4ee5\u4e0b\u9644\u4e0a\u5e7e\u7bc7\u6587\u7ae0\u4f9b\u53c3\u8003\uff1a<\/p>\n\n\n\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000005638577\" target=\"_blank\" rel=\"noreferrer noopener\">\u6df1\u5165\u6d45\u51fa Retrofit\uff0c\u8fd9\u4e48\u725b\u903c\u7684\u6846\u67b6\u4f60\u4eec\u8fd8\u4e0d\u6765\u770b\u770b\uff1f<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/bng86.gitbooks.io\/android-third-party-\/content\/retrofit.html\" data-type=\"URL\" data-id=\"https:\/\/bng86.gitbooks.io\/android-third-party-\/content\/retrofit.html\" target=\"_blank\" rel=\"noreferrer noopener\">Retrofit \u00b7 Android third-party \u4f7f\u7528\u5fc3\u5f97<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/ithelp.ithome.com.tw\/articles\/10188660\" data-type=\"URL\" data-id=\"https:\/\/ithelp.ithome.com.tw\/articles\/10188660\" target=\"_blank\" rel=\"noreferrer noopener\">iT\u90a6\u5e6b\u5fd9Android\u521d\u5b78\u7b46\u8a18\u7cfb\u5217 \u7b2c 24 \u7bc7&nbsp;Day 24 &#8211; \u4f7f\u7528Retrofit\u8207API\u9023\u7dda<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Retrofit\u662f\u4e00\u6b3e\u5c08\u9580\u7528\u4f86\u8655\u7406\u7db2\u8def\u8acb\u6c42\u7684\u6846\u67b6\uff0c\u7531\u77e5\u540d\u7684OkHttp\u958b\u767c\u5718\u968aSquare\u6240\u958b\u767c\u3002 Retro &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[10,32],"class_list":["post-1130","post","type-post","status-publish","format-standard","hentry","category-2","tag-android-app","tag-32"],"_links":{"self":[{"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/posts\/1130","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1130"}],"version-history":[{"count":5,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/posts\/1130\/revisions"}],"predecessor-version":[{"id":1136,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/posts\/1130\/revisions\/1136"}],"wp:attachment":[{"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}