Android通过PHP连接MySQL(读取)

 

1. 通过 MySQL在windows下的配置 中介绍第二种方法,在服务器机器上配置php和mysql环境,譬如我的服务器机器ip为:10.141.249.136

2. 新建在test数据库下新建一个teacher表,表的内容如下:
Android通过PHP连接MySQL(读取)
3. 在服务器机器上的phpnow安装目录 E:\PHPnow-1.5.5\htdocs下新建一个test.php文件,文件内容如下:

$link=mysql_connect("127.0.0.1","root","123456");
mysql_query("SET NAMES utf8");
mysql_select_db("test",$link);
$sql=mysql_query("select * from teacher ",$link);
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print(json_encode($output));
mysql_close();
?>

4. 新建一个Android Java Project
需要修改的是一下三个文件: AndroidTestActivity.java、 main.xml、 AndroidManifest.xml
//AndroidTestActivity.java
package com.knight.android.test;//根据实际的工程需要,修改包的名称

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.methods.HttpGet;

import org.apache.http.impl.client.DefaultHttpClient;

import org.json.JSONArray;

import org.json.JSONException;

import org.json.JSONObject;

import android.app.Activity;

import android.net.ParseException;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

public class AndroidTestActivity extends Activity {

JSONArray jArray;

String result = null;

InputStream is = null;

StringBuilder sb = null;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

Button b1 = (Button) findViewById(R.id.button1);

b1.setOnClickListener(new Button.OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

EditText tv = (EditText) findViewById(R.id.editView);

ArrayList nameValuePairs = newArrayList();

// http get

try {

HttpClient httpclient = new DefaultHttpClient();

HttpGet httpget = new HttpGet(

"http://10.141.249.136/test.php");

HttpResponse response = httpclient.execute(httpget);

HttpEntity entity = response.getEntity();

is = entity.getContent();

} catch (Exception e) {

Log.e("log_tag", "Error in http connection" + e.toString());

}

// convert response to string

try {

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 data

int 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("id");

ct_name = json_data.getString("name");

tv.append(ct_name + " \n");

}

} catch (JSONException e1) {

// Toast.makeText(getBaseContext(), "No City Found"

// ,Toast.LENGTH_LONG).show();

} catch (ParseException e1) {

e1.printStackTrace();

}

}

});

}

}


layout/main.xml
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >
 

你可能感兴趣的:(移动开发(Android))