环境配置:
windows:
Wamp下载地址:www.wampserver.com
https://github.com/teddysun/lamp
linux:
apt-get -y install wget screen git
git clone https://github.com/teddysun/lamp.git
cd lamp
chmod 755 *.sh
screen -S lamp
./lamp.sh
2, php后台源码
conn.php
$dbhost="120.78.121.226:3306";
$dbuser="root";
$dbpassword="tripod";
$dbname="test_db";
//最新的mysql需要用mysqli开关查询接口
$conn=mysqli_connect($dbhost,$dbuser,$dbpassword) or die("connect error");
$db_selected = mysqli_select_db($conn,"$dbname") or die("Could not select hugf_db
");
//设置字符编码,方便中文显示
mysqli_query("set names 'UTF-8'");
?>
index.php (查询用户名和密码用于登录)
include("conn.php");
//获取提交用户名
$username=str_replace("","",$_POST['name']);
//数据库查询语句,查询指定用户名数据
$sql="select * from login where name='$username'";
$query=mysqli_query($conn,$sql);
$rows = mysqli_fetch_array($query);
if(is_array($rows)){
if($_POST['pwd']==$rows['password']){
//如果密码和提交密码一致反回验证成功信息
echo "login succeed";
}else{
echo "error";
}
}
mysqli_close($conn);
?>
index.php (返回JSON数据,把数据库的所用用户名和密码用JSON的形式返回查询终端)
include("conn.php");
$username=str_replace("","",$_POST['name']);
//查询数据库表login 把回所有的表数据
$sql="select * from login ";
$query=mysqli_query($conn,$sql);
//新建一个数据
$jarr = array();
while($rows = mysqli_fetch_array($query)){
//把数据库信息放入数组
array_push($jarr,$rows);
}
//把数据转换成JSON信息并返回终端
echo $str=json_encode($jarr);
mysqli_close($conn);
?>
android终端代码:
权限:
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
LoginActivity.java
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.util.EntityUtils;
class ThreadPhp extends Thread {
@Override
public void run() {
Log.i("huguifeng","huguifeng--------2-------------LoginHandler");
String userName = "huguifeng";
String password = "hugf123";
//连接到服务器的地址,我监听的是8080端口
String connectURL="http://120.78.121.226/";
//填入用户名密码和连接地址
boolean isLoginSucceed = gotoLogin(userName, password,connectURL);
//判断返回值是否为true,若是的话就跳到主页。
Log.i("huguifeng","huguifeng-----------------isLoginSucceed="+isLoginSucceed);
if(isLoginSucceed){
// Toast.makeText(FullscreenActivity.this, "登入成功", Toast.LENGTH_LONG).show();
}else{
// Toast.makeText(FullscreenActivity.this, "登入错误", Toast.LENGTH_LONG).show();
}
}
}
private boolean gotoLogin(String userName, String password,String connectUrl) {
String result = null; //用来取得返回的String;
boolean isLoginSucceed = false;
//发送post请求
HttpPost httpRequest = new HttpPost(connectUrl);
//Post运作传送变数必须用NameValuePair[]阵列储存
List params = new ArrayList();
params.add(new BasicNameValuePair("name",userName));
params.add(new BasicNameValuePair("pwd",password));
try{
//发出HTTP请求
httpRequest.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
Log.i("huguifeng","huguifeng-------------httpRequest");
//取得HTTP response
HttpResponse httpResponse=new DefaultHttpClient().execute(httpRequest);
//若状态码为200则请求成功,取到返回数据
Log.i("huguifeng","huguifeng-------------httpResponse.getStatusLine().getStatusCode()="+httpResponse.getStatusLine().getStatusCode());
if(httpResponse.getStatusLine().getStatusCode()==200){
//取出字符串
result= EntityUtils.toString(httpResponse.getEntity());
Log.i("huguifeng","huguifeng-------------result="+result);
//system.out.println("result= "+result);
}
}catch(Exception e){
Log.i("huguifeng","huguifeng-------------e="+e.toString());
e.printStackTrace();
}
//判断返回的数据是否为php中成功登入是输出的
if(result!= null && result.equals("login succeed")){
isLoginSucceed = true;
}
return isLoginSucceed;
}
以上代码即可验证验登录功能,又可以直接查询返回的JSON数组信息。
在app module模块的build.gradle下面需要添加:
useLibrary 'org.apache.http.legacy'
因为android最新版本不支持apache
这个功能本来想在本机实现的,用模拟器,发现有如下问题:
1.模拟器的IP地址和本机地址是一样的,
2.我的是UBUNTU系统装了WINDOWS虚拟机, 模拟器在虚拟机,新的API版本不支持。老版本又慢得出奇。
希望有朋友遇到可以避免同样的问题。