怎么连接android和php mysql数据库

如题所述

如何连接android和php mysql数据库

我们先来看一个简单的Android app例子(这里是一个商品存货清单项目),在Android程序中,我们可以访问(call)PHP脚本来执行简单的CRUD操作(创建,读取,更新,删除)。为了使你对它的体系结构有一个大概的了解,这里先说一下它是怎么工作的。首先你的Android项目访问(call)PHP脚本来执行一条数据操作,我们称它为“创建”。然后PHP脚本连接MySQL数据库来执行这个操作。这样,数据从Android程序流向PHP脚本,最终存储在MySQL数据库中。
好了,让我们来深入的看一下。
请注意:这里提供的代码只是为了使你能简单的连接Android项目和PHP,MySQL。你不能把它作为一个标准或者安全编程实践。在生产环境中,理想情况下你需要避免使用任何可能造成潜在注入漏洞的代码(比如MYSQL注入)。MYSQL注入是一个很大的话题,不可能用单独的一篇文章来说清楚,并且它也不在本文讨论的范围内,所以本文不以讨论。
1. 什么是WAMP Server
WAMP是Windows,Apache,MySQL和PHP,Perl,Python的简称。WAMP是一个一键安装的软件,它为开发PHP,MySQL Web应用程序提供一个环境。安装这款软件你相当于安装了Apache,MySQL和PHP。或者,你也可以使用XAMP。

2. 安装和使用WAMP Server
你可以从http://www.wampserver.com/en/下载WAMP,安装完成之后,可以从开始->所有程序->WampServer->StartWampServer运行该程序。
在浏览器中输入http://localhost/来测试你的服务器是否安装成功。同样的,也可以打开http://localhost/phpmyadmin来检验phpmyadmin是否安装成功。
3. 创建和运行PHP项目
现在,你已经有一个能开发PHP和MYSQL项目的环境了。打开安装WAMP Server的文件夹(在我的电脑中,是C:\wamp\),打开www文件夹,为你的项目创建一个新的文件夹。你必须把项目中所有的文件放到这个文件夹中。
新建一个名为android_connect的文件夹,并新建一个php文件,命名为test.php,尝试输入一些简单的php代码(如下所示)。输入下面的代码后,打开http://localhost/android_connect/test.php,你会在浏览器中看到“Welcome,I am connecting Android to PHP,MySQL”(如果没有正确输入,请检查WAMP配置是否正确)
test.php

4. 打开MainScreenActivity.java为main_screen.xml文件里的两个按钮添加点击事件

MainScreenActivity.java

7. 添加一个新产品(写入)
创建一个新的view和activity来向MySQL数据库添加新产品。
新建一个简单的表单,创建提供输入产品名称,价格和描述的EditText
add_product.xml

8. 新建一个Activity来处理向MySQL数据库插入新产品。
新建名为NewProductActivity.java的文件,输入以下代码。在下面的代码中
首先,从EditText获得用户输入的产品数据,格式化成基本参数格式
然后,向create_product.php发送请求,通过HTTP POST创建一个新的产品
最后,从create_product.php获取json返回值,如果success值为1,新得到的列表中就加入了新增的产品。
NewProductActivity.java

11. JSONParserç±»
我用一个JSONParser类从URL获得JSON格式的数据。这个类支持两种http请求,GET和POST方法从URL获取JSON数据
JSONParser.java

packagecom.example.androidhive; importjava.io.BufferedReader; importjava.io.IOException; importjava.io.InputStream; importjava.io.InputStreamReader; importjava.io.UnsupportedEncodingException; importjava.util.List; importorg.apache.http.HttpEntity; importorg.apache.http.HttpResponse; importorg.apache.http.NameValuePair; importorg.apache.http.client.ClientProtocolException; importorg.apache.http.client.entity.UrlEncodedFormEntity; importorg.apache.http.client.methods.HttpGet; importorg.apache.http.client.methods.HttpPost; importorg.apache.http.client.utils.URLEncodedUtils; importorg.apache.http.impl.client.DefaultHttpClient; importorg.json.JSONException; importorg.json.JSONObject; importandroid.util.Log; publicclassJSONParser { staticInputStream is = null; staticJSONObject jObj = null; staticString json = ""; // constructor publicJSONParser() { } // function get json from url // by making HTTP POST or GET mehtod publicJSONObject makeHttpRequest(String url, String method, List<NameValuePair> params) { // Making HTTP request try{ // check for request method if(method == "POST"){ // request method is POST // defaultHttpClient DefaultHttpClient httpClient = newDefaultHttpClient(); HttpPost httpPost = newHttpPost(url); httpPost.setEntity(newUrlEncodedFormEntity(params)); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); }elseif(method == "GET"){ // request method is GET DefaultHttpClient httpClient = newDefaultHttpClient(); String paramString = URLEncodedUtils.format(params, "utf-8"); url += "?"+ paramString; HttpGet httpGet = newHttpGet(url); HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); } } catch(UnsupportedEncodingException e) { e.printStackTrace(); } catch(ClientProtocolException e) { e.printStackTrace(); } catch(IOException e) { e.printStackTrace(); } try{ BufferedReader reader = newBufferedReader(newInputStreamReader( is, "iso-8859-1"), 8); StringBuilder sb = newStringBuilder(); String line = null; while((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); json = sb.toString(); } catch(Exception e) { Log.e("Buffer Error", "Error converting result "+ e.toString()); } // try parse the string to a JSON object try{ jObj = newJSONObject(json); } catch(JSONException e) { Log.e("JSON Parser", "Error parsing data "+ e.toString()); } // return JSON String returnjObj; } }
到这里,本教程就结束了。
温馨提示:答案为网友推荐,仅供参考