{"id":182944,"date":"2024-11-10T00:38:35","date_gmt":"2024-11-09T16:38:35","guid":{"rendered":"https:\/\/server.hk\/cnblog\/182944\/"},"modified":"2024-11-10T00:38:36","modified_gmt":"2024-11-09T16:38:36","slug":"%e5%af%a6%e7%8f%beandroid%e8%a8%aa%e5%95%8fmysql%e7%9a%84%e7%b0%a1%e5%96%ae%e6%96%b9%e6%b3%95-android%e7%9b%b4%e6%8e%a5%e8%a8%aa%e5%95%8fmysql%e6%95%b8%e6%93%9a%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/182944\/","title":{"rendered":"\u5be6\u73feAndroid\u8a2a\u554fMySQL\u7684\u7c21\u55ae\u65b9\u6cd5 (android\u76f4\u63a5\u8a2a\u554fmysql\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"%e5%af%a6%e7%8f%beandroid%e8%a8%aa%e5%95%8fmysql%e7%9a%84%e7%b0%a1%e5%96%ae%e6%96%b9%e6%b3%95-xLvoQYRiuU\">\u5be6\u73feAndroid\u8a2a\u554fMySQL\u7684\u7c21\u55ae\u65b9\u6cd5<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u79fb\u52d5\u61c9\u7528\u958b\u767c\u4e2d\uff0cAndroid\u61c9\u7528\u7a0b\u5e8f\u7d93\u5e38\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\u7a0b\u5e8f\u6578\u64da\u3002\u96d6\u7136\u76f4\u63a5\u5f9eAndroid\u8a2d\u5099\u8a2a\u554fMySQL\u6578\u64da\u5eab\u662f\u53ef\u884c\u7684\uff0c\u4f46\u9019\u6a23\u505a\u5b58\u5728\u5b89\u5168\u6027\u548c\u6027\u80fd\u65b9\u9762\u7684\u6311\u6230\u3002\u56e0\u6b64\uff0c\u672c\u6587\u5c07\u63a2\u8a0e\u4e00\u4e9b\u7c21\u55ae\u7684\u65b9\u6cd5\u4f86\u5be6\u73feAndroid\u8a2a\u554fMySQL\u7684\u529f\u80fd\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e4%b8%8d%e7%9b%b4%e6%8e%a5%e8%a8%aa%e5%95%8fmysql%ef%bc%9f-xLvoQYRiuU\">\u70ba\u4ec0\u9ebc\u4e0d\u76f4\u63a5\u8a2a\u554fMySQL\uff1f<\/h2>\n<p>\u76f4\u63a5\u5f9eAndroid\u61c9\u7528\u7a0b\u5e8f\u8a2a\u554fMySQL\u6578\u64da\u5eab\u4e26\u4e0d\u662f\u6700\u4f73\u5be6\u8e10\uff0c\u539f\u56e0\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li><strong>\u5b89\u5168\u6027\uff1a<\/strong>\u76f4\u63a5\u9023\u63a5\u6578\u64da\u5eab\u6703\u66b4\u9732\u6578\u64da\u5eab\u7684\u6191\u8b49\uff0c\u589e\u52a0\u4e86\u6578\u64da\u6d29\u9732\u7684\u98a8\u96aa\u3002<\/li>\n<li><strong>\u6027\u80fd\uff1a<\/strong>\u6bcf\u6b21\u8acb\u6c42\u90fd\u9700\u8981\u5efa\u7acb\u65b0\u7684\u6578\u64da\u5eab\u9023\u63a5\uff0c\u9019\u6703\u5f71\u97ff\u61c9\u7528\u7684\u6027\u80fd\u3002<\/li>\n<li><strong>\u53ef\u64f4\u5c55\u6027\uff1a<\/strong>\u96a8\u8457\u7528\u6236\u6578\u91cf\u7684\u589e\u52a0\uff0c\u76f4\u63a5\u8a2a\u554f\u6578\u64da\u5eab\u7684\u65b9\u5f0f\u53ef\u80fd\u7121\u6cd5\u6eff\u8db3\u9700\u6c42\u3002<\/li>\n<\/ul>\n<h2 id=\"%e6%8e%a8%e8%96%a6%e7%9a%84%e8%a7%a3%e6%b1%ba%e6%96%b9%e6%a1%88-xLvoQYRiuU\">\u63a8\u85a6\u7684\u89e3\u6c7a\u65b9\u6848<\/h2>\n<p>\u70ba\u4e86\u5b89\u5168\u548c\u9ad8\u6548\u5730\u8a2a\u554fMySQL\u6578\u64da\u5eab\uff0c\u901a\u5e38\u5efa\u8b70\u4f7f\u7528\u4e2d\u4ecb\u5c64\uff0c\u4f8b\u5982Web\u670d\u52d9\u6216API\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684\u65b9\u6cd5\uff1a<\/p>\n<h3 id=\"1-%e4%bd%bf%e7%94%a8php%e5%92%8crestful-api-xLvoQYRiuU\">1. \u4f7f\u7528PHP\u548cRESTful API<\/h3>\n<p>\u9019\u662f\u6700\u5e38\u898b\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528PHP\u5275\u5efa\u4e00\u500bRESTful API\uff0c\u7136\u5f8c\u5728Android\u61c9\u7528\u4e2d\u901a\u904eHTTP\u8acb\u6c42\u4f86\u8a2a\u554f\u6578\u64da\u3002<\/p>\n<pre><code>\nconnect_error) {\n    die(\"Connection failed: \" . $conn-&gt;connect_error);\n}\n\n$sql = \"SELECT * FROM users\";\n$result = $conn-&gt;query($sql);\n\nif ($result-&gt;num_rows &gt; 0) {\n    $users = array();\n    while($row = $result-&gt;fetch_assoc()) {\n        $users[] = $row;\n    }\n    echo json_encode($users);\n} else {\n    echo \"0 results\";\n}\n$conn-&gt;close();\n?&gt;\n<\/code><\/pre>\n<p>\u5728Android\u61c9\u7528\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Volley\u6216Retrofit\u5eab\u4f86\u767c\u9001HTTP\u8acb\u6c42\u4e26\u8655\u7406\u97ff\u61c9\u3002<\/p>\n<pre><code>\nString url = \"http:\/\/yourserver.com\/db.php\";\nRequestQueue queue = Volley.newRequestQueue(this);\nStringRequest stringRequest = new StringRequest(Request.Method.GET, url,\n    new Response.Listener() {\n        @Override\n        public void onResponse(String response) {\n            \/\/ \u8655\u7406\u97ff\u61c9\n        }\n    }, new Response.ErrorListener() {\n        @Override\n        public void onErrorResponse(VolleyError error) {\n            \/\/ \u8655\u7406\u932f\u8aa4\n        }\n    });\nqueue.add(stringRequest);\n<\/code><\/pre>\n<h3 id=\"2-%e4%bd%bf%e7%94%a8node-js%e5%92%8cexpress-xLvoQYRiuU\">2. \u4f7f\u7528Node.js\u548cExpress<\/h3>\n<p>\u53e6\u4e00\u7a2e\u6d41\u884c\u7684\u9078\u64c7\u662f\u4f7f\u7528Node.js\u548cExpress\u6846\u67b6\u4f86\u5275\u5efaAPI\u3002\u9019\u7a2e\u65b9\u6cd5\u7684\u512a\u52e2\u5728\u65bc\u5176\u975e\u963b\u585eI\/O\u6a21\u578b\uff0c\u80fd\u5920\u8655\u7406\u5927\u91cf\u7684\u4e26\u767c\u8acb\u6c42\u3002<\/p>\n<pre><code>\nconst express = require('express');\nconst mysql = require('mysql');\nconst app = express();\n\nconst connection = mysql.createConnection({\n    host: 'localhost',\n    user: 'username',\n    password: 'password',\n    database: 'database'\n});\n\napp.get('\/users', (req, res) =&gt; {\n    connection.query('SELECT * FROM users', (error, results) =&gt; {\n        if (error) throw error;\n        res.json(results);\n    });\n});\n\napp.listen(3000, () =&gt; {\n    console.log('Server is running on port 3000');\n});\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-xLvoQYRiuU\">\u7e3d\u7d50<\/h2>\n<p>\u96d6\u7136\u76f4\u63a5\u5f9eAndroid\u8a2a\u554fMySQL\u6578\u64da\u5eab\u662f\u53ef\u884c\u7684\uff0c\u4f46\u7531\u65bc\u5b89\u5168\u6027\u548c\u6027\u80fd\u7684\u8003\u91cf\uff0c\u5efa\u8b70\u4f7f\u7528\u4e2d\u4ecb\u5c64\u5982RESTful API\u4f86\u9032\u884c\u6578\u64da\u4ea4\u4e92\u3002\u7121\u8ad6\u662f\u4f7f\u7528PHP\u9084\u662fNode.js\uff0c\u9019\u4e9b\u65b9\u6cd5\u90fd\u80fd\u6709\u6548\u5730\u5be6\u73feAndroid\u61c9\u7528\u8207MySQL\u6578\u64da\u5eab\u4e4b\u9593\u7684\u901a\u4fe1\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u4f86\u90e8\u7f72\u60a8\u7684\u5f8c\u7aef\u670d\u52d9\uff0cServer.HK\u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u60a8\u7684\u9700\u6c42\u3002\u7121\u8ad6\u662f\u5c0f\u578b\u9805\u76ee\u9084\u662f\u5927\u578b\u61c9\u7528\uff0c\u6211\u5011\u7684 <a href=\"https:\/\/server.hk\">\u96f2\u4f3a\u670d\u5668<\/a> \u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u53ef\u9760\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u7c21\u55ae\u65b9\u6cd5\u5be6\u73feAndroid\u76f4\u63a5\u8a2a\u554fMySQL\u6578\u64da\u5eab\uff0c\u63d0\u5347\u61c9\u7528\u6027\u80fd\u8207\u6578\u64da\u7ba1\u7406\u6548\u7387\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-182944","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/182944","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"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=182944"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/182944\/revisions"}],"predecessor-version":[{"id":182945,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/182944\/revisions\/182945"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=182944"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=182944"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=182944"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}