時隔已久的一個任務,今天終於可以畫上一個句號了。心情是萬分的激動,雖然這份小成就來的有點遲但還是按捺不住心情的澎湃。下面我就先上幾張圖片來展示一下我的成績
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”的表。
到此所有的工作已經完成了。可以進行測試了。測試的結果,去上面的一開始的兩張截圖。