Php generates an Android client to scan the login QR code _ php instance-PHP source code

Source: Internet
Author: User
This article describes in detail how php generates an Android client to scan the QR code that can be logged on. it has some reference value, if you are interested, can you refer to this article for details about generating Android client scan QR codes that can be logged on in php? it has some reference value, for more information, see

The examples in this article share with you the QR code generated on the php web page and the specific code for scanning logon on the Android client for your reference. The details are as follows:

The ZXing open-source library with code scanning function on Github is used, and the web API for generating QR code images through random numbers is used. The entire process goes through three steps:

1. the PHP web page generates a QR code and the random number is stored in the database;
2. scan the Android client and save the username to the location corresponding to the random number;
3. at intervals of time, PHP poll the database through Ajax to determine whether it is empty. if it is not empty, it will jump to the webpage.

Code:
1. generate a QR code image by using a random number, and execute the polling operation command on the home page.

   Qrlogin  
     
 "Width =" 300px "/>  Script xmlHttpRequest. onreadystatechange = function () {if (xmlHttpRequest. status = 200 & xmlHttpRequest. readyState = 4) {result = xmlHttp. responseText; if (result = true) {// if username is not empty, the page window is redirected. location. href = 'Welcome. php ';}}} function polling () {// execute the polling operation 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

2. database connection page

 

3. for the page on which the round-robin operation is executed, jump if username is not empty

 

4. customize the API to save the username of the client

 

5. Activity on the Android client performing the code scanning operation

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 May 12, 2016 **/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/ "; // Change to the corresponding address on the PC end @ 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) {// scan the code to operate Intent intent = new Intent (this, CaptureActivity. class); start ActivityForResult (intent, 0); // return result} @ 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"); // The client returns the scan result after scanning the code. the random number corresponding to the QR code is retrieved. String username = etUsername. getText (). toString (); String url = WEB_URL + "saveUsername. php? Randnumber = "+ randnumber +" & username = "+ username; HttpUtils. login (url); // access url }}}

6. network request

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

The above is all the content of this article, hoping to help you learn.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.