悉远

 找回密码
 注册
搜索
查看: 4387|回复: 2

android通过PHP连接mysql数据库

[复制链接]
发表于 2012-7-20 18:15:01 | 显示全部楼层 |阅读模式
使用PHP作为中间介来实现android链接远程数据库。
可以方便快捷的链接数据库!


PHP代码:
  1.         <?php
  2.                 mysql_connect("host","username","password");
  3.                 mysql_select_db("PeopleData");
  4.                  $q=mysql_query("SELECT * FROM people WHERE birthyear>'".$_REQUEST['year']."'");                while($e=mysql_fetch_assoc($q))        
  5.                 $output[]=$e;         
  6.                 print(json_encode($output));         
  7.                 mysql_close();
  8.         ?>
复制代码
android代码:
  1. package lzu.ConnectMysql;

  2. import java.io.BufferedReader;
  3. import java.io.InputStream;
  4. import java.io.InputStreamReader;
  5. import java.util.ArrayList;

  6. import org.apache.http.HttpEntity;
  7. import org.apache.http.HttpResponse;
  8. import org.apache.http.client.HttpClient;
  9. import org.apache.http.client.entity.UrlEncodedFormEntity;
  10. import org.apache.http.client.methods.HttpPost;
  11. import org.apache.http.impl.client.DefaultHttpClient;
  12. import org.json.JSONArray;
  13. import org.json.JSONException;
  14. import org.json.JSONObject;

  15. import org.apache.http.NameValuePair;
  16. import org.apache.http.message.BasicNameValuePair;


  17. import android.app.Activity;
  18. import android.os.Bundle;
  19. import android.widget.TextView;

  20. public class ConnectMysql extends Activity {
  21.     /** Called when the activity is first created. */
  22. private TextView result;
  23. private String content;
  24.     @Override
  25.     public void onCreate(Bundle savedInstanceState) {
  26.         super.onCreate(savedInstanceState);
  27.         setContentView(R.layout.main);

  28.         result = (TextView)findViewById(R.id.content);

  29.         content = connecting();

  30.         result.setText(content);
  31.       //the year data to send
  32.     }

  33.     public String connecting(){
  34.     /*存放http请求得到的结果*/
  35.     String result = "";
  36.     String ss = null;
  37.    
  38.     /*将要发送的数据封包 */
  39.     ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
  40.         nameValuePairs.add(new BasicNameValuePair("year","1980"));

  41.         InputStream is = null;
  42.         //http post
  43.         try{
  44.         /*创建一个HttpClient的一个对象*/
  45.                 HttpClient httpclient = new DefaultHttpClient();

  46.                 /*创建一个HttpPost的对象*/
  47.                 HttpPost httppost = new HttpPost("http://202.201.0.245/test.php");

  48.                 /*设置请求的数据*/
  49.                 httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

  50.                 /*创建HttpResponse对象*/
  51.                 HttpResponse response = httpclient.execute(httppost);

  52.                 /*获取这次回应的消息实体*/
  53.                 HttpEntity entity = response.getEntity();

  54.                 /*创建一个指向对象实体的数据流*/
  55.                 is = entity.getContent();
  56.         }catch(Exception e){
  57.                System.out.println("Connectiong Error");
  58.         }
  59.         //convert response to string
  60.         try{
  61.                 BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
  62.                 StringBuilder sb = new StringBuilder();
  63.                 String line = null;
  64.                 while ((line = reader.readLine()) != null) {
  65.                         sb.append(line + "/n");
  66.                 }
  67.                 is.close();

  68.                 result=sb.toString();
  69.                 System.out.println("get = " + result);
  70.         }catch(Exception e){
  71.                System.out.println("Error converting to String");
  72.         }

  73.         //parse json data
  74.         try{
  75.         /*从字符串result创建一个JSONArray对象*/
  76.                 JSONArray jArray = new JSONArray(result);

  77.                 for(int i=0;i<jArray.length();i++){
  78.                         JSONObject json_data = jArray.getJSONObject(i);
  79.                         System.out.println("Success");
  80.                         System.out.println("result " + json_data.toString());
  81.                         if( i == 0){
  82.                         ss = json_data.toString();
  83.                         }
  84.                         else{
  85.                         ss += json_data.toString();
  86.                         }
  87.                 }
  88.         }
  89.         catch(JSONException e){
  90.                System.out.println("Error parsing json");
  91.         }
  92.         return ss;
  93.     }
  94. }
复制代码
 发表于 2012-7-21 14:12:38
写的真好,向你学习哈,我也会分享我的心得的!欢迎回访[tthread=joyphp, joyphp]http://app.qlogo.cn/mbloghead/7076d23e98d8957d6bda[/tthread]
发表于 2012-8-6 17:42:49 | 显示全部楼层
我怎么运行了没结果啊,最近学这个,能发给我全部代码么?谢谢,[email protected]
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|QQ客服|联系我们|Archiver|手机版|小黑屋|悉远网络 ( 鄂ICP备09013446号 )

GMT+8, 2024-4-20 11:53 , Processed in 0.041253 second(s), 11 queries , Redis On.

Powered by Discuz! X3.5

Copyright © 2001-2023 Tencent Cloud.

快速回复 返回顶部 返回列表