android连接mysql数据库

参考自:http://www.helloandroid.com/tutorials/connecting-mysql-database

 

使用PHP作为中间介来实现android链接远程数据库。

PHP代码:

<?php

mysql_connect("host","username","password");

mysql_select_db("PeopleData");

  $q=mysql_query("SELECT * FROM people WHERE birthyear>'".$_REQUEST['year']."'"); while($e=mysql_fetch_assoc($q))

        $output[]=$e; 

print(json_encode($output)); 

mysql_close();

?>

 

android代码:

package lzu.ConnectMysql;

 

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.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 org.apache.http.NameValuePair;

import org.apache.http.message.BasicNameValuePair;

 

 

import android.app.Activity;

import android.os.Bundle;

import android.widget.TextView;

 

public class ConnectMysql extends Activity {

    /** Called when the activity is first created. */

private TextView result;

private String content;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

 

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

 

        content = connecting();

 

        result.setText(content);

      //the year data to send

    }

 

    public String connecting(){

    /*存放http请求得到的结果*/

    String result = "";

    String ss = null;

   

    /*将要发送的数据封包 */

    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

        nameValuePairs.add(new BasicNameValuePair("year","1980"));

 

        InputStream is = null;

        //http post

        try{

        /*创建一个HttpClient的一个对象*/

                HttpClient httpclient = new DefaultHttpClient();

 

                /*创建一个HttpPost的对象*/

                HttpPost httppost = new HttpPost("http://202.201.0.245/test.php");

 

                /*设置请求的数据*/

                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

 

                /*创建HttpResponse对象*/

                HttpResponse response = httpclient.execute(httppost);

 

                /*获取这次回应的消息实体*/

                HttpEntity entity = response.getEntity();

 

                /*创建一个指向对象实体的数据流*/

                is = entity.getContent();

        }catch(Exception e){

               System.out.println("Connectiong Error");

        }

        //convert response to string

        try{

                BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);

                StringBuilder sb = new StringBuilder();

                String line = null;

                while ((line = reader.readLine()) != null) {

                        sb.append(line + "/n");

                }

                is.close();

 

                result=sb.toString();

                System.out.println("get = " + result);

        }catch(Exception e){

               System.out.println("Error converting to String");

        }

 

        //parse json data

        try{

        /*从字符串result创建一个JSONArray对象*/

                JSONArray jArray = new JSONArray(result);

 

                for(int i=0;i<jArray.length();i++){

                        JSONObject json_data = jArray.getJSONObject(i);

                        System.out.println("Success");

                        System.out.println("result " + json_data.toString());

                        if( i == 0){

                        ss = json_data.toString();

                        }

                        else{

                        ss += json_data.toString();

                        }

                }

        }

        catch(JSONException e){

               System.out.println("Error parsing json");

        }

        return ss;

    } 

}

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 15
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值