{"id":58393,"date":"2024-10-12T20:21:16","date_gmt":"2024-10-12T12:21:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/58393\/"},"modified":"2024-10-12T20:21:16","modified_gmt":"2024-10-12T12:21:16","slug":"android%e7%9a%84mysql%e6%95%b8%e6%93%9a%e5%ba%ab%e8%a8%aa%e5%95%8f%e6%8a%80%e5%b7%a7-android-%e8%a8%aa%e5%95%8fmysql%e6%95%b8%e6%93%9a%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/58393\/","title":{"rendered":"Android\u7684Mysql\u6578\u64da\u5eab\u8a2a\u554f\u6280\u5de7 (android \u8a2a\u554fmysql\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"android%e7%9a%84mysql%e6%95%b8%e6%93%9a%e5%ba%ab%e8%a8%aa%e5%95%8f%e6%8a%80%e5%b7%a7-rkwUkUQmuE\">Android\u7684MySQL\u6578\u64da\u5eab\u8a2a\u554f\u6280\u5de7<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u79fb\u52d5\u61c9\u7528\u958b\u767c\u4e2d\uff0cAndroid\u5e73\u53f0\u7684\u666e\u53ca\u4f7f\u5f97\u958b\u767c\u8005\u8d8a\u4f86\u8d8a\u9700\u8981\u8207\u5f8c\u7aef\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\u3002MySQL\u4f5c\u70ba\u4e00\u7a2e\u6d41\u884c\u7684\u95dc\u4fc2\u578b\u6578\u64da\u5eab\uff0c\u7d93\u5e38\u88ab\u7528\u65bc\u5b58\u5132\u548c\u7ba1\u7406\u61c9\u7528\u6578\u64da\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5728Android\u61c9\u7528\u4e2d\u8a2a\u554fMySQL\u6578\u64da\u5eab\u7684\u5e7e\u7a2e\u6280\u5de7\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u6709\u6548\u5730\u9032\u884c\u6578\u64da\u64cd\u4f5c\u3002<\/p>\n<h2 id=\"1-%e4%bd%bf%e7%94%a8web%e6%9c%8d%e5%8b%99%e9%80%b2%e8%a1%8c%e6%95%b8%e6%93%9a%e8%a8%aa%e5%95%8f-rkwUkUQmuE\">1. \u4f7f\u7528Web\u670d\u52d9\u9032\u884c\u6578\u64da\u8a2a\u554f<\/h2>\n<p>\u76f4\u63a5\u5728Android\u61c9\u7528\u4e2d\u8a2a\u554fMySQL\u6578\u64da\u5eab\u4e26\u4e0d\u5b89\u5168\uff0c\u56e0\u70ba\u9019\u6a23\u6703\u66b4\u9732\u6578\u64da\u5eab\u7684\u9023\u63a5\u4fe1\u606f\u3002\u76f8\u53cd\uff0c\u5efa\u8b70\u4f7f\u7528Web\u670d\u52d9\u4f5c\u70ba\u4e2d\u4ecb\u3002\u9019\u901a\u5e38\u6d89\u53ca\u5230\u4ee5\u4e0b\u6b65\u9a5f\uff1a<\/p>\n<ul>\n<li>\u5efa\u7acb\u4e00\u500bRESTful API\uff0c\u4f7f\u7528PHP\u3001Node.js\u6216\u5176\u4ed6\u5f8c\u7aef\u6280\u8853\u4f86\u8655\u7406\u6578\u64da\u5eab\u8acb\u6c42\u3002<\/li>\n<li>\u5728API\u4e2d\u7de8\u5beb\u76f8\u61c9\u7684\u6578\u64da\u64cd\u4f5c\u908f\u8f2f\uff0c\u5982\u67e5\u8a62\u3001\u63d2\u5165\u3001\u66f4\u65b0\u548c\u522a\u9664\u3002<\/li>\n<li>\u5728Android\u61c9\u7528\u4e2d\u4f7f\u7528HTTP\u8acb\u6c42\uff08\u5982Retrofit\u6216Volley\uff09\u4f86\u8abf\u7528\u9019\u4e9bAPI\u3002<\/li>\n<\/ul>\n<h3 id=\"%e7%a4%ba%e4%be%8b%ef%bc%9a%e4%bd%bf%e7%94%a8retrofit%e8%a8%aa%e5%95%8fapi-rkwUkUQmuE\">\u793a\u4f8b\uff1a\u4f7f\u7528Retrofit\u8a2a\u554fAPI<\/h3>\n<pre><code>\npublic interface ApiService {\n    @GET(\"api\/getData\")\n    Call&lt;List&gt; getData();\n}\n<\/code><\/pre>\n<p>\u9019\u6bb5\u4ee3\u78bc\u5b9a\u7fa9\u4e86\u4e00\u500bAPI\u63a5\u53e3\uff0c\u901a\u904eGET\u8acb\u6c42\u4f86\u7372\u53d6\u6578\u64da\u3002\u958b\u767c\u8005\u53ef\u4ee5\u6839\u64da\u9700\u8981\u64f4\u5c55\u9019\u500b\u63a5\u53e3\uff0c\u6dfb\u52a0\u66f4\u591a\u7684\u6578\u64da\u64cd\u4f5c\u65b9\u6cd5\u3002<\/p>\n<h2 id=\"2-%e4%bd%bf%e7%94%a8json%e6%a0%bc%e5%bc%8f%e5%82%b3%e8%bc%b8%e6%95%b8%e6%93%9a-rkwUkUQmuE\">2. \u4f7f\u7528JSON\u683c\u5f0f\u50b3\u8f38\u6578\u64da<\/h2>\n<p>\u5728Android\u61c9\u7528\u548cMySQL\u6578\u64da\u5eab\u4e4b\u9593\u50b3\u8f38\u6578\u64da\u6642\uff0c\u4f7f\u7528JSON\u683c\u5f0f\u662f\u4e00\u500b\u5e38\u898b\u7684\u505a\u6cd5\u3002JSON\u683c\u5f0f\u8f15\u91cf\u4e14\u6613\u65bc\u89e3\u6790\uff0c\u9069\u5408\u7528\u65bc\u79fb\u52d5\u61c9\u7528\u7684\u6578\u64da\u4ea4\u4e92\u3002<\/p>\n<h3 id=\"%e7%a4%ba%e4%be%8b%ef%bc%9a%e8%a7%a3%e6%9e%90json%e6%95%b8%e6%93%9a-rkwUkUQmuE\">\u793a\u4f8b\uff1a\u89e3\u6790JSON\u6578\u64da<\/h3>\n<pre><code>\npublic void parseJson(String jsonResponse) {\n    try {\n        JSONArray jsonArray = new JSONArray(jsonResponse);\n        for (int i = 0; i &lt; jsonArray.length(); i++) {\n            JSONObject jsonObject = jsonArray.getJSONObject(i);\n            String dataField = jsonObject.getString(&quot;fieldName&quot;);\n            \/\/ \u8655\u7406\u6578\u64da\n        }\n    } catch (JSONException e) {\n        e.printStackTrace();\n    }\n}\n<\/code><\/pre>\n<p>\u9019\u6bb5\u4ee3\u78bc\u5c55\u793a\u4e86\u5982\u4f55\u89e3\u6790\u5f9eAPI\u7372\u53d6\u7684JSON\u6578\u64da\uff0c\u4e26\u63d0\u53d6\u6240\u9700\u7684\u5b57\u6bb5\u3002<\/p>\n<h2 id=\"3-%e4%bd%bf%e7%94%a8asynctask%e9%80%b2%e8%a1%8c%e7%95%b0%e6%ad%a5%e6%93%8d%e4%bd%9c-rkwUkUQmuE\">3. \u4f7f\u7528AsyncTask\u9032\u884c\u7570\u6b65\u64cd\u4f5c<\/h2>\n<p>\u5728Android\u4e2d\uff0c\u9032\u884c\u7db2\u7d61\u8acb\u6c42\u6642\uff0c\u61c9\u907f\u514d\u5728\u4e3b\u7dda\u7a0b\u4e2d\u57f7\u884c\u8017\u6642\u64cd\u4f5c\u3002\u4f7f\u7528AsyncTask\u53ef\u4ee5\u6709\u6548\u5730\u8655\u7406\u9019\u4e9b\u7570\u6b65\u8acb\u6c42\uff0c\u78ba\u4fdd\u61c9\u7528\u7684\u6d41\u66a2\u6027\u3002<\/p>\n<h3 id=\"%e7%a4%ba%e4%be%8b%ef%bc%9a%e4%bd%bf%e7%94%a8asynctask%e9%80%b2%e8%a1%8c%e7%b6%b2%e7%b5%a1%e8%ab%8b%e6%b1%82-rkwUkUQmuE\">\u793a\u4f8b\uff1a\u4f7f\u7528AsyncTask\u9032\u884c\u7db2\u7d61\u8acb\u6c42<\/h3>\n<pre><code>\nprivate class FetchDataTask extends AsyncTask {\n    @Override\n    protected String doInBackground(Void... voids) {\n        \/\/ \u57f7\u884c\u7db2\u7d61\u8acb\u6c42\n        return jsonResponse;\n    }\n\n    @Override\n    protected void onPostExecute(String result) {\n        parseJson(result);\n    }\n}\n<\/code><\/pre>\n<p>\u9019\u6bb5\u4ee3\u78bc\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528AsyncTask\u4f86\u57f7\u884c\u7db2\u7d61\u8acb\u6c42\uff0c\u4e26\u5728\u8acb\u6c42\u5b8c\u6210\u5f8c\u89e3\u6790\u8fd4\u56de\u7684\u6578\u64da\u3002<\/p>\n<h2 id=\"4-%e5%ae%89%e5%85%a8%e6%80%a7%e8%80%83%e9%87%8f-rkwUkUQmuE\">4. \u5b89\u5168\u6027\u8003\u91cf<\/h2>\n<p>\u5728\u8a2a\u554fMySQL\u6578\u64da\u5eab\u6642\uff0c\u5b89\u5168\u6027\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002\u958b\u767c\u8005\u61c9\u8003\u616e\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528HTTPS\u5354\u8b70\u4f86\u52a0\u5bc6\u6578\u64da\u50b3\u8f38\u3002<\/li>\n<li>\u5728API\u4e2d\u5be6\u65bd\u8eab\u4efd\u9a57\u8b49\u548c\u6388\u6b0a\u6a5f\u5236\u3002<\/li>\n<li>\u5c0d\u7528\u6236\u8f38\u5165\u9032\u884c\u6709\u6548\u7684\u9a57\u8b49\u548c\u904e\u6ffe\uff0c\u4ee5\u9632\u6b62SQL\u6ce8\u5165\u653b\u64ca\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-rkwUkUQmuE\">\u7e3d\u7d50<\/h2>\n<p>\u5728Android\u61c9\u7528\u4e2d\u8a2a\u554fMySQL\u6578\u64da\u5eab\u9700\u8981\u8003\u616e\u591a\u65b9\u9762\u7684\u56e0\u7d20\uff0c\u5305\u62ec\u5b89\u5168\u6027\u3001\u6578\u64da\u683c\u5f0f\u548c\u7570\u6b65\u64cd\u4f5c\u7b49\u3002\u901a\u904e\u4f7f\u7528Web\u670d\u52d9\u3001JSON\u683c\u5f0f\u548cAsyncTask\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6709\u6548\u5730\u5be6\u73fe\u6578\u64da\u7684\u8b80\u53d6\u548c\u5beb\u5165\u3002\u82e5\u60a8\u9700\u8981\u7a69\u5b9a\u7684\u5f8c\u7aef\u652f\u6301\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u4f86\u642d\u5efa\u60a8\u7684\u6578\u64da\u5eab\u670d\u52d9\uff0c\u78ba\u4fdd\u61c9\u7528\u7684\u9ad8\u6548\u904b\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u638c\u63e1Android\u8a2a\u554fMySQL\u6578\u64da\u5eab\u7684\u6280\u5de7\uff0c\u63d0\u5347\u61c9\u7528\u6027\u80fd\u8207\u6578\u64da\u7ba1\u7406\u6548\u7387\uff0c\u5be6\u73fe\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-58393","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/58393","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=58393"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/58393\/revisions"}],"predecessor-version":[{"id":58394,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/58393\/revisions\/58394"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=58393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=58393"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=58393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}