Android+PHP服务器+MySQL实现安卓端的登录
时隔已久的一个任务,今天终于可以画上一个句号了。心情是万分的激动,虽然这份小成就来的有点迟但还是按捺不住心情的澎湃。下面我就先上几张图片来展示一下我的成绩
android源代码:
首先最重要的一件事是添加权限:
<uses-permission android:name="android.permission.INTERNET"/>
LoginActivity.java
package com.itcast.datalogin; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.app.Activity; import android.content.Intent; import android.text.TextUtils; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class LoginActivity extends Activity { protected static final int ERROR = 2; protected static final int SUCCESS = 1; private EditText et_qq; private EditText et_psd; private Handler handler = new Handler(){ public void handleMessage(Message msg) { switch (msg.what) { case SUCCESS: Toast.makeText(LoginActivity.this,(String)msg.obj, 1).show(); break; case ERROR: Toast.makeText(LoginActivity.this,"登录失败", 1).show(); break; } }; }; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); et_qq = (EditText) findViewById(R.id.et_qq); et_psd = (EditText) findViewById(R.id.et_pwd); } public void login(View view){ final String qq = et_qq.getText().toString(); final String psd = et_psd.getText().toString(); if(TextUtils.isEmpty(qq)||TextUtils.isEmpty(psd)){ Toast.makeText(this, "用户和密码不能为空", 0).show(); return; } new Thread(){ public void run(){ try { //http://localhost/xampp/android/login.php //区别1、url的路径不同 String path = "http://192.168.1.101:80/xampp/login.php"; URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //区别2、请求方式post conn.setRequestMethod("POST"); conn.setRequestProperty("User-Agent", "Mozilla/5.0(compatible;MSIE 9.0;Windows NT 6.1;Trident/5.0)"); //区别3、必须指定两个请求的参数 conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");//请求的类型 表单数据 String data = "username="+qq+"&password="+psd+"&button="; ; conn.setRequestProperty("Content-Length", data.length()+"");//数据的长度 //区别4、记得设置把数据写给服务器 conn.setDoOutput(true);//设置向服务器写数据 byte[] bytes = data.getBytes(); conn.getOutputStream().write(bytes);//把数据以流的方式写给服务器 int code = conn.getResponseCode(); System.out.println(code); if(code == 200){ InputStream is = conn.getInputStream(); String result = StreamTools.readStream(is); Message mas= Message.obtain(); mas.what = SUCCESS; mas.obj = result; handler.sendMessage(mas); }else{ Message mas = Message.obtain(); mas.what = ERROR; handler.sendMessage(mas); } }catch (IOException e) { // TODO Auto-generated catch block Message mas = Message.obtain(); mas.what = ERROR; handler.sendMessage(mas); } } }.start(); } }
StreamTools.java
package com.itcast.datalogin; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; public class StreamTools { /* * 把一个流里面的内容转换成一个字符串 * return 流的字符串 null 解析失败 * */ public static String readStream(InputStream is){ try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = -1; while ((len = is.read(buffer))!=-1) { baos.write(buffer,0,len); } baos.close(); return new String(baos.toByteArray()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return ""; } } }
activity_login.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <EditText android:id="@+id/et_qq" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="144803094" /> <EditText android:id="@+id/et_pwd" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPassword" android:text="144803094" /> <Button android:onClick="login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="right" android:text="登录"/> </LinearLayout>
PHP代码:
<?php if(isset($_POST['button'])){ $username=$_POST['username'];//得到用户输入的用户名 $password=$_POST['password'];//密码 mysql_connect('127.0.0.1:3306','root','')or die(mysql_error());//连接数据库 mysql_select_db('mvc_study');//选择数据库 mysql_query('set names utf8'); $sql = "select * from users where username = '$username' and password='$password'"; $rs=mysql_query($sql); if(mysql_num_rows($rs)==1){//如果数据库的行数为1则成功否则失败 echo $username; echo '已经成功登录'; }else{ echo $username; echo '登录失败'; } }else{ echo 'test!'; } ?>
MySQL:
先创建一个名字为“mvc_study”的数据库。然后创建一个名字为“users”的表。
到此所有的工作已经完成了。可以进行测试了。测试的结果,去上面的一开始的两张截图。
posted on 2016-07-26 22:55 kang_ya_ping 阅读(11322) 评论(1) 编辑 收藏 举报