{"id":58562,"date":"2024-10-12T20:49:17","date_gmt":"2024-10-12T12:49:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/58562\/"},"modified":"2024-10-12T20:49:17","modified_gmt":"2024-10-12T12:49:17","slug":"android%e7%b6%b2%e7%b5%a1%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e5%af%a6%e7%8f%be%e5%92%8c%e5%84%aa%e5%8c%96-android%e7%b6%b2%e7%b5%a1%e6%95%b8%e6%93%9a%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/58562\/","title":{"rendered":"Android\u7db2\u7d61\u6578\u64da\u5eab\u7684\u5be6\u73fe\u548c\u512a\u5316 (android\u7db2\u7d61\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"android%e7%b6%b2%e7%b5%a1%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e5%af%a6%e7%8f%be%e5%92%8c%e5%84%aa%e5%8c%96-iKnXQjytqt\">Android\u7db2\u7d61\u6578\u64da\u5eab\u7684\u5be6\u73fe\u548c\u512a\u5316<\/h1>\n<p>\u96a8\u8457\u79fb\u52d5\u61c9\u7528\u7684\u666e\u53ca\uff0cAndroid\u5e73\u53f0\u4e0a\u7684\u7db2\u7d61\u6578\u64da\u5eab\u6210\u70ba\u4e86\u958b\u767c\u8005\u5fc5\u9808\u638c\u63e1\u7684\u91cd\u8981\u6280\u8853\u4e4b\u4e00\u3002\u7db2\u7d61\u6578\u64da\u5eab\u4e0d\u50c5\u80fd\u5920\u5b58\u5132\u5927\u91cf\u6578\u64da\uff0c\u9084\u80fd\u5920\u5be6\u73fe\u6578\u64da\u7684\u5373\u6642\u66f4\u65b0\u548c\u5171\u4eab\u3002\u672c\u6587\u5c07\u63a2\u8a0eAndroid\u7db2\u7d61\u6578\u64da\u5eab\u7684\u5be6\u73fe\u65b9\u6cd5\u53ca\u5176\u512a\u5316\u7b56\u7565\u3002<\/p>\n<h2 id=\"%e4%b8%80%e3%80%81android%e7%b6%b2%e7%b5%a1%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5-iKnXQjytqt\">\u4e00\u3001Android\u7db2\u7d61\u6578\u64da\u5eab\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>\u7db2\u7d61\u6578\u64da\u5eab\u662f\u6307\u901a\u904e\u7db2\u7d61\u9023\u63a5\u7684\u6578\u64da\u5eab\u7cfb\u7d71\uff0c\u901a\u5e38\u4f7f\u7528\u5ba2\u6236\u7aef-\u670d\u52d9\u5668\u67b6\u69cb\u3002Android\u61c9\u7528\u53ef\u4ee5\u901a\u904eHTTP\u8acb\u6c42\u8207\u5f8c\u7aef\u670d\u52d9\u5668\u9032\u884c\u6578\u64da\u4ea4\u4e92\uff0c\u9019\u4e9b\u670d\u52d9\u5668\u901a\u5e38\u4f7f\u7528MySQL\u3001PostgreSQL\u7b49\u95dc\u4fc2\u578b\u6578\u64da\u5eab\uff0c\u6216MongoDB\u7b49\u975e\u95dc\u4fc2\u578b\u6578\u64da\u5eab\u3002<\/p>\n<h2 id=\"%e4%ba%8c%e3%80%81%e5%af%a6%e7%8f%beandroid%e7%b6%b2%e7%b5%a1%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e6%ad%a5%e9%a9%9f-iKnXQjytqt\">\u4e8c\u3001\u5be6\u73feAndroid\u7db2\u7d61\u6578\u64da\u5eab\u7684\u6b65\u9a5f<\/h2>\n<h3 id=\"1-%e8%a8%ad%e8%a8%88%e5%be%8c%e7%ab%afapi-iKnXQjytqt\">1. \u8a2d\u8a08\u5f8c\u7aefAPI<\/h3>\n<p>\u5728\u5be6\u73fe\u7db2\u7d61\u6578\u64da\u5eab\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u8a2d\u8a08\u5f8c\u7aefAPI\u3002\u9019\u4e9bAPI\u5c07\u8ca0\u8cac\u8655\u7406\u4f86\u81eaAndroid\u61c9\u7528\u7684\u8acb\u6c42\uff0c\u4e26\u8fd4\u56de\u76f8\u61c9\u7684\u6578\u64da\u3002\u5e38\u898b\u7684\u6280\u8853\u6709RESTful API\u548cGraphQL\u3002<\/p>\n<pre><code>GET \/api\/users\nPOST \/api\/users\nPUT \/api\/users\/{id}\nDELETE \/api\/users\/{id}\n<\/code><\/pre>\n<h3 id=\"2-%e4%bd%bf%e7%94%a8http%e5%ba%ab%e9%80%b2%e8%a1%8c%e8%ab%8b%e6%b1%82-iKnXQjytqt\">2. \u4f7f\u7528HTTP\u5eab\u9032\u884c\u8acb\u6c42<\/h3>\n<p>\u5728Android\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u7a2eHTTP\u5eab\u4f86\u767c\u9001\u8acb\u6c42\uff0c\u4f8b\u5982Retrofit\u3001OkHttp\u7b49\u3002\u9019\u4e9b\u5eab\u7c21\u5316\u4e86\u7db2\u7d61\u8acb\u6c42\u7684\u904e\u7a0b\uff0c\u4e26\u63d0\u4f9b\u4e86\u7570\u6b65\u8655\u7406\u7684\u529f\u80fd\u3002<\/p>\n<pre><code>Retrofit retrofit = new Retrofit.Builder()\n        .baseUrl(\"https:\/\/api.example.com\/\")\n        .addConverterFactory(GsonConverterFactory.create())\n        .build();\n\nUserService service = retrofit.create(UserService.class);\nCall&lt;List&gt; call = service.getUsers();\ncall.enqueue(new Callback&lt;List&gt;() {\n    @Override\n    public void onResponse(Call&lt;List&gt; call, Response&lt;List&gt; response) {\n        \/\/ \u8655\u7406\u6210\u529f\u7684\u97ff\u61c9\n    }\n\n    @Override\n    public void onFailure(Call&lt;List&gt; call, Throwable t) {\n        \/\/ \u8655\u7406\u5931\u6557\u7684\u97ff\u61c9\n    }\n});\n<\/code><\/pre>\n<h3 id=\"3-%e6%95%b8%e6%93%9a%e5%ad%98%e5%84%b2%e8%88%87%e7%ae%a1%e7%90%86-iKnXQjytqt\">3. \u6578\u64da\u5b58\u5132\u8207\u7ba1\u7406<\/h3>\n<p>\u5728\u7372\u53d6\u6578\u64da\u5f8c\uff0c\u958b\u767c\u8005\u9700\u8981\u8003\u616e\u5982\u4f55\u5728\u672c\u5730\u5b58\u5132\u9019\u4e9b\u6578\u64da\u3002Android\u63d0\u4f9b\u4e86SQLite\u6578\u64da\u5eab\u548cRoom\u6301\u4e45\u5316\u5eab\u4f86\u5e6b\u52a9\u958b\u767c\u8005\u7ba1\u7406\u672c\u5730\u6578\u64da\u3002<\/p>\n<pre><code>@Entity\npublic class User {\n    @PrimaryKey\n    public int id;\n    public String name;\n}\n\n@Dao\npublic interface UserDao {\n    @Insert\n    void insert(User user);\n\n    @Query(\"SELECT * FROM user\")\n    List getAllUsers();\n}\n<\/code><\/pre>\n<h2 id=\"%e4%b8%89%e3%80%81%e7%b6%b2%e7%b5%a1%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e5%84%aa%e5%8c%96%e7%ad%96%e7%95%a5-iKnXQjytqt\">\u4e09\u3001\u7db2\u7d61\u6578\u64da\u5eab\u7684\u512a\u5316\u7b56\u7565<\/h2>\n<h3 id=\"1-%e6%95%b8%e6%93%9a%e7%b7%a9%e5%ad%98-iKnXQjytqt\">1. \u6578\u64da\u7de9\u5b58<\/h3>\n<p>\u70ba\u4e86\u63d0\u9ad8\u61c9\u7528\u7684\u6027\u80fd\uff0c\u53ef\u4ee5\u8003\u616e\u4f7f\u7528\u6578\u64da\u7de9\u5b58\u6280\u8853\u3002\u901a\u904e\u5728\u672c\u5730\u5b58\u5132\u6700\u8fd1\u4f7f\u7528\u7684\u6578\u64da\uff0c\u53ef\u4ee5\u6e1b\u5c11\u5c0d\u7db2\u7d61\u7684\u8acb\u6c42\u6b21\u6578\uff0c\u5f9e\u800c\u63d0\u9ad8\u61c9\u7528\u7684\u97ff\u61c9\u901f\u5ea6\u3002<\/p>\n<h3 id=\"2-%e6%89%b9%e9%87%8f%e8%ab%8b%e6%b1%82-iKnXQjytqt\">2. \u6279\u91cf\u8acb\u6c42<\/h3>\n<p>\u5728\u9700\u8981\u7372\u53d6\u5927\u91cf\u6578\u64da\u6642\uff0c\u53ef\u4ee5\u8003\u616e\u4f7f\u7528\u6279\u91cf\u8acb\u6c42\u7684\u65b9\u5f0f\uff0c\u9019\u6a23\u53ef\u4ee5\u6e1b\u5c11\u7db2\u7d61\u5ef6\u9072\uff0c\u63d0\u9ad8\u6578\u64da\u7372\u53d6\u7684\u6548\u7387\u3002<\/p>\n<h3 id=\"3-%e9%8c%af%e8%aa%a4%e8%99%95%e7%90%86%e8%88%87%e9%87%8d%e8%a9%a6%e6%a9%9f%e5%88%b6-iKnXQjytqt\">3. \u932f\u8aa4\u8655\u7406\u8207\u91cd\u8a66\u6a5f\u5236<\/h3>\n<p>\u5728\u7db2\u7d61\u8acb\u6c42\u4e2d\uff0c\u932f\u8aa4\u662f\u4e0d\u53ef\u907f\u514d\u7684\u3002\u958b\u767c\u8005\u61c9\u8a72\u5be6\u73fe\u932f\u8aa4\u8655\u7406\u548c\u91cd\u8a66\u6a5f\u5236\uff0c\u4ee5\u78ba\u4fdd\u61c9\u7528\u5728\u7db2\u7d61\u4e0d\u7a69\u5b9a\u7684\u60c5\u6cc1\u4e0b\u4ecd\u80fd\u6b63\u5e38\u904b\u884c\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-iKnXQjytqt\">\u7e3d\u7d50<\/h2>\n<p>Android\u7db2\u7d61\u6578\u64da\u5eab\u7684\u5be6\u73fe\u548c\u512a\u5316\u662f\u4e00\u500b\u8907\u96dc\u4f46\u91cd\u8981\u7684\u904e\u7a0b\u3002\u901a\u904e\u5408\u7406\u8a2d\u8a08\u5f8c\u7aefAPI\u3001\u4f7f\u7528\u5408\u9069\u7684HTTP\u5eab\u3001\u6709\u6548\u7ba1\u7406\u6578\u64da\u5b58\u5132\uff0c\u4ee5\u53ca\u63a1\u53d6\u512a\u5316\u7b56\u7565\uff0c\u53ef\u4ee5\u986f\u8457\u63d0\u5347\u61c9\u7528\u7684\u6027\u80fd\u548c\u7528\u6236\u9ad4\u9a57\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u6578\u64da\u8655\u7406\u7684\u61c9\u7528\uff0c\u9078\u64c7\u5408\u9069\u7684\u670d\u52d9\u5668\u67b6\u69cb\uff0c\u5982<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u6216<a href=\"https:\/\/server.hk\">\u96f2\u4f3a\u670d\u5668<\/a>\uff0c\u4e5f\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002\u5e0c\u671b\u672c\u6587\u80fd\u70ba\u958b\u767c\u8005\u63d0\u4f9b\u6709\u50f9\u503c\u7684\u53c3\u8003\u3002 <\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22Android\u7db2\u7d61\u6578\u64da\u5eab\u7684\u5be6\u73fe\u8207\u512a\u5316\u6280\u8853\uff0c\u63d0\u5347\u6578\u64da\u5b58\u53d6\u6548\u7387\uff0c\u589e\u5f37\u61c9\u7528\u6027\u80fd\uff0c\u5be6\u73fe\u66f4\u6d41\u66a2\u7684\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-58562","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/58562","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=58562"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/58562\/revisions"}],"predecessor-version":[{"id":58563,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/58562\/revisions\/58563"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=58562"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=58562"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=58562"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}