本文实例为大家分享了php网页生成二维码,Android客户端扫描登录的具体代码,供大家参考,具体内容如下

使用了Github上具有扫码功能的ZXing开源库,使用了通过随机数生成二维码图片网络API,整个过程经过三步:

1.PHP网页生成二维码,相应随机数存储到数据库中;
2.Android客户端扫码,携带username保存至随机数对应的位置;
3.每隔一段时间,PHP通过Ajax轮询数据库,判断是否为空,不为空则跳转网页。

具体代码:
1. 通过随机数生成二维码图片,并执行轮询操作命令的主页面    

<html>
 <head>
  <title>qrlogin</title>
  <meta charset="UTF-8"/>
 </head>
 <body>
  <"";
    for($i=0;$i<8;$i++){
    $randnumber.=rand(0,9);
    }
    //将生成的随机数保存至数据库
    mysql_query("insert into login_data (randnumber) values ('$randnumber')")
    
  "http://qr.liantu.com/api.php" width="300px"/>
  <input hidden="hidden" type="text" name="randnumber" id="randnumber"value="<"/>
 
 </body>
 <script>
  xmlHttpRequest.onreadystatechange = function(){
    if(xmlHttpRequest.status == 200 && xmlHttpRequest.readyState ==4){
  result = xmlHttp.responseText;
  if(result==true){//username不为空则跳转页面
     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,true);
    xmlHttpRequest.send();
 }
    setInterval("polling()",1000);
</script>
 
</html>

2. 数据库连接页面    

<"localhost","root","") or die(mysql_error());
mysql_select_db("qr_login");
 
"htmlcode">
<"select * from login_data where randnumber='$randnumber'");
$row = mysql_fetch_array($result);
if($row['username']!="")
  echo "true";
else
  echo "false";
"htmlcode">
<"update qr_login set username='$username' where randnumber= '$randnumber'");
 
 
"htmlcode">
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年5月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端相应地址
 
  @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) {
    // 扫码操作
    Intent intent = new Intent(this, CaptureActivity.class);
    startActivityForResult(intent, 0);//返回结果
  }
 
  @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");//客户端扫码后返回扫描结果,将二维码对应的随机数取出
      String username = etUsername.getText().toString();
      String url = WEB_URL + "saveUsername.php" + randnumber
          + "&username=" + username;
      HttpUtils.login(url);//访问url
    }
  }
 
}

6. 网络请求类

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();
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助。

华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com