phpÉú³ÉAndroid¿Í»§¶ËɨÃè¿ÉµÇ¼µÄ¶þάÂë

5年以前  |  阅读数:247 次  |  编程语言:PHP 

±¾ÎÄʵÀýΪ´o¼Ò*ÖÏiÁËphpÍøÒ³Éu³É¶þάÂe£¬Android¿Í»§¶ËɨÃeµÇ¼µÄ¾ßÌa´uÂe£¬¹(C)´o¼Ò²Î¿¼£¬¾ßÌaÄÚÈÝÈçÏÂ

ʹÓÃÁËGithubÉϾßÓÐɨÂe¹¦ÄܵÄZXing¿ªÔ´¿a£¬Ê¹ÓÃÁËͨ¹ýËae»uÊýÉu³É¶þάÂeͼƬÍøÂçAPI£¬Õu¸o¹ý³Ì¾­¹ýÈý²½£º

1.PHPÍøÒ³Éu³É¶þάÂe£¬ÏaÓ¦Ëae»uÊý´ae´¢µ½Êý¾Ý¿aÖУ»
2.Android¿Í»§¶ËɨÂe£¬Ð¯´øusername±£´aeÖÁËae»uÊý¶ÔÓ¦µÄλÖã»
3.ÿ¸oÒ»¶Îʱ¼a£¬PHPͨ¹ýAjaxÂÖѯÊý¾Ý¿a£¬ÅжÏÊÇ*ñΪ¿Õ£¬²»Îª¿ÕÔoÌøתÍøÒ³¡£

¾ßÌa´uÂe£º
1. ͨ¹ýËae»uÊýÉu³É¶þάÂeͼƬ£¬²¢Ö´ÐÐÂÖѯ²Ù×÷ÃuÁiµÄÖ÷Ò³Ãae


    <html>
     <head>
      <title>qrlogin</title>
      <meta charset="UTF-8"/>
     </head>
     <body>
      <?php
      /**
       * @author Cenquanyu
       * @version 2016Äe5ÔÂ12ÈÕ
       *
       */
        require 'mysql_connect.php';
        $randnumber = "";
        for($i=0;$i<8;$i++){
        $randnumber.=rand(0,9);
        }
        //½«Éu³ÉµÄËae»uÊý±£´aeÖÁÊý¾Ý¿a
        mysql_query("insert into login_data (randnumber) values ('$randnumber')")

      ?>

      <img src="http://qr.liantu.com/api.php?text=<?php echo $randnumber;?>" width="300px"/>
      <input hidden="hidden" type="text" name="randnumber" id="randnumber"value="<?php echo $randnumber;?>"/>

     </body>
     <script>
      xmlHttpRequest.onreadystatechange = function(){
        if(xmlHttpRequest.status == 200 && xmlHttpRequest.readyState ==4){
      result = xmlHttp.responseText;
      if(result==true){//username²»Îª¿ÕÔoÌøתҳÃae
         window.location.href='welcome.php';
      }
    }
    }
     }
     function polling(){

       //Ö´ÐÐÂÖѯ²Ù×÷
       var xmlHttpRequest;
       if(window.XMLHttpRequest){
         xmlHttpRequest = new XMLHttpRequest();
         }
       else{
         xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
       }
        randnumber = document.getElementById('randnumber').value;
        xmlHttpRequest.open("GET","polling.php?randnumber="+ randnumber,true);
        xmlHttpRequest.send();
     }
        setInterval("polling()",1000);
    </script>

    </html>

2. Êý¾Ý¿aÁ¬½ÓÒ³Ãae


    <?php
    /**
     * Êý¾Ý¿aÁ¬½ÓÎļþ
     * @author Cenquanyu
     * @version 2016Äe5ÔÂ12ÈÕ
     * 
     */
    $con = mysql_connect("localhost","root","") or die(mysql_error());
    mysql_select_db("qr_login");

    ?>

3.Ö´ÐÐÂÖѯ²Ù×÷µÄÒ³Ãae£¬username²»Îª¿ÕÔoÌøת


    <?php
    /**
     * @author Cenquanyu
     * @version 2016Äe5ÔÂ12ÈÕ
     * Ö´ÐÐÂÖѯ²Ù×÷£¬²eѯËae»uÊýÔÚÊý¾Ý¿aÖеÄÏaӦλÖõÄusername×Ö¶ÎΪ²»Îª¿Õ
     * Ϊ¿Õ£¬Ôo*µ»Øfalse,Ò³Ãae²»Ìøת
     * ²»Îª¿Õ£¬Ôo˵Ã÷ÓÐÓû§½øÐÐÁ˸öþάÂeµÄɨÂeµÇ¼£¬Ò³Ãae½øÐÐÌøת
     */
    require 'mysql_connect.php';
    $randnumber = $_GET['randnumber'];
    $result = mysql_query("select * from login_data where randnumber='$randnumber'");
    $row = mysql_fetch_array($result);
    if($row['username']!="")
      echo "true";
    else
      echo "false";
    ?>

4.×Ô¶¨ÒaµÄAPI£¬¶Ô¿Í»§¶ËµÄusername½øÐб£´ae


    <?php
    /**
     * @author Cenquanyu
     * @version 2016Äe5ÔÂ12ÈÕ
     * ×Ô¶¨ÒaAPIÓÃÓÚAndroid¿Í»§¶ËɨÂeµÇ¼£¬½«¿Í»§¶ËµÄusername±£´aeÖÁ¶þάÂe¶ÔÓ¦µÄËae»uÊýÔÚÊý¾Ý¿aÖеÄÏaӦλÖá£
     * ²ÎÊý£ºusername£¬randnumber
     * ÎÞ*µ»ØÖµ
     */
    $randnumber = $_GET('randnumber');
    $username = $_GET('username');

    require 'mysql_connect.php';
    mysql_query("update qr_login set username='$username' where randnumber= '$randnumber'");


    ?>

5. Android¿Í»§¶ËÖ´ÐÐɨÂe²Ù×÷µÄActivity


    package com.Cenquanyu.qrlogin;

    import com.Cenquanyu.qrlogin.R;
    import com.zxing.activity.CaptureActivity;

    import android.app.Activity;
    import android.content.Intent;
    import android.graphics.Paint.Cap;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    /**
     * @author Cenquanyu
     * @version 2016Äe5ÔÂ12ÈÕ
     * 
     */
    public class MainActivity extends Activity implements OnClickListener {

      private Button btnScan;
      private EditText etUsername;


      private static final String WEB_URL = "http://172.31.19.202/QRLogin/";//¸Ä³ÉPC¶ËÏaÓ¦µØÖ*

      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnScan = (Button) findViewById(R.id.btnScan);
        btnScan.setOnClickListener(this);
        etUsername = (EditText) findViewById(R.id.etUsername);
      }

      @Override
      public void onClick(View v) {
        // ɨÂe²Ù×÷
        Intent intent = new Intent(this, CaptureActivity.class);
        startActivityForResult(intent, 0);//*µ»Ø½a¹u
      }

      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
          String randnumber = data.getExtras().getString("result");//¿Í»§¶ËɨÂeºo*µ»ØɨÃe½a¹u£¬½«¶þάÂe¶ÔÓ¦µÄËae»uÊýÈ¡³o
          String username = etUsername.getText().toString();
          String url = WEB_URL + "saveUsername.php?randnumber=" + randnumber
              + "&username;=" + username;
          HttpUtils.login(url);//*ÃÎÊurl
        }
      }

    }

6. ÍøÂçÇeÇoÀa


    package com.Cenquanyu.qrlogin;

    import java.io.IOException;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;

    public class HttpUtils{
      public static void login(final String url){
        new Thread(new Runnable() {
          @Override
          public void run() {
            HttpURLConnection connection;
            try {
              connection = (HttpURLConnection) new URL(url).openConnection();
              connection.setRequestMethod("GET");
              connection.getInputStream();
            } catch (Exception e) {
              e.printStackTrace();
            }
          }
        }).start();
      }
    }

ÒÔÉϾÍÊDZ¾ÎĵÄÈ«²¿ÄÚÈÝ£¬Ï£Íu¶Ô´o¼ÒµÄѧϰÓÐËu°iÖu¡£

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例