[转载+翻译] Android : Connecting to MySQL using PHP. --by bhargav--
来源:互联网 发布:windows f8内核调试 编辑:程序博客网 时间:2024/06/15 07:40
原文: http://blog.sptechnolab.com/2011/02/10/android/android-connecting-to-mysql-using-php/
原文作者: bhargav
《Android : 用 PHP 连接 MySQL 数据库》
使用php语言的主要原因是因为,她能够提供与数据库的直接交互。在这个教程中,我将展示给大家如何使用PHP和MySQL数据库,从服务器取得数据。在Android操作系统上我们将使用HTTP协议与PHP脚本连接,。为了理解这篇教程, 你应该具有有运行使用PHP和网络服务器的基础知识,。假设我们讨论的是关于 客户-服务器 框架,那么客户端就是android设备,服务器那边就是PHP脚本与MySQL的组合。简而言之,PHP脚本担任一个中间件:
我们将使用 JSON(JavaScript Object Notain)结构。 JSON是一个轻量级的,基于文本的, 利于人们阅读的开放标准的 数据交换格式。
在我们的应用程序里如何使用JSON:
---当android应用程序执行时,将android设备与PHP脚本建立连接。
---PHP脚本从数据库取出数据,编码成JSON格式并发送到android设备。
---现在,android应用程序可以得到这些数据,解析这些数据并显示在android设备上。
一些实现细节:
在MySQL里创建一个数据库 “Deal”。在数据库里创建一个表 “CITY”。这个表由两个字段构成。第一个字段是 CITY_ID(自增,主键 ,数据类型为INT)。第二个是字段是CITY_NAME(数据类型为Varchar(20))。
我们想从表CITY中检索出开头字母为A的城市名,
简单 PHP代码结构是这样的:
--连接数据库
--运行一个SQL查询,WHERE语句块的内容取决于 POST/GET的值。
--输出JSON格式
在 city.php里是实现这个功能的代码例子:
<?phpmysql_connect("host","username","password");mysql_select_db("Deal");$sql=mysql_query("select * from CITY where CITY_NAME like 'A%'");while($row=mysql_fetch_assoc($sql))$output[]=$row;print(json_encode($output));mysql_close();?>
在我们的Android应用程序里,
--用一个HttpPost得到数据
--将响应response转换成字符串
--解析JSON数据,然后使用在你需要的地方
取数据从服务器到我们的andorid应用程序的全部代码 如下所示:
package com.list;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.util.ArrayList;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.NameValuePair;import org.apache.http.client.HttpClient;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.DefaultHttpClient;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import android.app.ListActivity;import android.net.ParseException;import android.os.Bundle;import android.util.Log;import android.widget.Toast;public class city extends ListActivity {JSONArray jArray;String result = null;InputStream is = null;StringBuilder sb=null;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main); ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();//http posttry{ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://10.0.2.2/city.php"); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); }catch(Exception e){ Log.e("log_tag", "Error in http connection"+e.toString()); }//convert response to stringtry{ BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); sb = new StringBuilder(); sb.append(reader.readLine() + "\n"); String line="0"; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); result=sb.toString(); }catch(Exception e){ Log.e("log_tag", "Error converting result "+e.toString()); }//paring dataint ct_id;String ct_name;try{ jArray = new JSONArray(result); JSONObject json_data=null; for(int i=0;i<jArray.length();i++){ json_data = jArray.getJSONObject(i); ct_id=json_data.getInt("CITY_ID"); ct_name=json_data.getString("CITY_NAME"); } } catch(JSONException e1){ Toast.makeText(getBaseContext(), "No City Found" ,Toast.LENGTH_LONG).show(); } catch (ParseException e1) {e1.printStackTrace();}}} }
不要忘了在 AndroidManifest.xml中 插入下面的权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<完>
--------------------------------------------
本文是我第一次翻译和发表文章,有些生硬。欢迎大家批评指正,交流。
- [转载+翻译] Android : Connecting to MySQL using PHP. --by bhargav--
- PHP - Connecting to MySQL
- Connecting JIRA to MySQL
- Connecting to a GPS receiver using Bluetooth
- Connecting Oracle to PostgreSQL using Oracle Gateways
- kettle7.0 Error connecting to database: (using class org.gjt.mm.mysql.Driver)
- Connecting to a MySQL Database
- Error connecting to MySQL server
- Scala connecting to MySQL with JDBC
- Error when connecting to Windows Server 2003 using Remote Desktop
- Connecting to LDAP server using JNDI in Java
- Connecting to TCP Socket from browser using javascript
- how to start and stop android service by using adb
- Connecting to beacons in Android SDK
- 【转载,翻译】使用命名空间实现上下文Using Namespaces to provide context in AS3
- NServiceBus翻译之持久化技术(三):Using RavenDB In NServiceBus – Connecting
- Connecting to MySQL 5.0 from Visual C# 2005
- Warning about SSL connection when connecting to MySQL database
- java socket通信-传输文件图片
- 性能监控之监控SQL语句
- Linux Hard Disk Format Command
- 浅谈Sun JVM中的内存管理及调优
- JVM基础概念总结:数据类型、堆与栈
- [转载+翻译] Android : Connecting to MySQL using PHP. --by bhargav--
- wpf中数据库表的翻页按钮的设计
- JVM GC调整优化过程全揭秘
- Java 之 String 类型
- SQL基础整理
- CSAPP 第六章-存储器层次结构 小结
- 优化 Java 垃圾收集器改进系统性能
- 事务是什么?
- 花朵数的研究