2. 新建在test数据库下新建一个teacher表,表的内容如下:
3. 在服务器机器上的phpnow安装目录E:\PHPnow-1.5.5\htdocs下新建一个test.php文件,文件内容如下:
<?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<NameValuePair>
nameValuePairs = new
ArrayList<NameValuePair>();
//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
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas./apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click" />
<EditText
android:id="@+id/editView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HI "
android:textSize="30dip" />
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0"
encoding="utf-8"?>
<manifest
xmlns:android="http://schemas./apk/res/android"
package="com.knight.android.test"
android:versionCode="1"
android:versionName="1.0" >
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
>
<activity
android:name=".AndroidTestActivity"
android:label="@string/app_name" >
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
</application>
<!--
授权访问网络 -->
<uses-permission
android:name="android.permission.INTERNET"/>
</manifest>
5. 运行结果如下图:
点击click以后,Android会向服务器发送一个Http
Get请求,服务器从mysql中读取数据后,传送给Android客户端,客户端编码数据包,然后返回如下结果:
注意:
(1)AndroidManifest.xml中不能出现<uses-sdk
android:minSdkVersion="15"
/>这种属性,否则Android客户端无法连接到远程服务器
(2)如果在本机搭建mysql和php环境,以上程序(AndroidTestActivity.java)中红色部分应更改为:
HttpGet httpget = new
HttpGet("http://10.0.2.2/test.php");
127.0.0.1表示手机的本机ip,因为程序最终是在手机上跑的
(3)如果读者自定义的工程,需要修改一下几个地方:
- 第一个是 AndroidTestActivity.java 程序里面的package名称package
com.knight.android.test;这个根据读者自己定义的包要做出相应的修改(绿色部分)
- 第二个是修改 AndroidManifest.xml里面第三行的package=" com.knight.android.test",要保持绿色部分和第一条中的绿色部分相对应
- 第三点是修改AndroidManifest.xml里面activity下面的 android:name=".
AndroidTestActivity",将绿色部分修改为
AndroidTestActivity.java的红色部分(也就是类名)
|