PHP build android Client scan can log in QR code, Android client _php tutorial

Source: Internet
Author: User

PHP generated android client scan can log in QR code, Android client


The example of this article for everyone to share the PHP page generated two-dimensional code, Android client scan login specific code for your reference, the specific content as follows

Using the Zxing Open Source Library with the sweep code feature on GitHub, the three-step process of generating a two-dimensional code image network API via random numbers is done:

1.PHP Web page generated two-dimensional code, the corresponding random number stored in the database;
2.Android Client Scan code, carry username to the location of the random number corresponding;
3. Every once in a while, PHP polls the database through Ajax, determines whether it is empty, and jumps to the Web page without being empty.

Specific code:
1. Generate a QR code image by random number and execute the main page of the polling Operation command

   Qrlogin  
 
      <?php  /**   * @author cenquanyu   * @version May 12, 2016   * */    require ' mysql_connect.php ';    $randnumber = "";    for ($i =0; $i <8; $i + +) {    $randnumber. =rand (0,9);    }    Save the generated random number to the database    mysql_query ("INSERT into Login_data (randnumber) VALUES (' $randnumber ')")      ?>     " Width= "300px"/>      

2. Database Connection page

<?php/** * Database Connection File * @author Cenquanyu * @version May 12, 2016 * *  /$con = mysql_connect ("localhost", "root", "") or Die (Mysql_error ()); mysql_select_db ("Qr_login");?>

3. The page that performs the polling operation, username is not empty, jumps

<?php/** * @author Cenquanyu * @version May 12, 2016 * Polling operation, query random number in the database in the corresponding location of the username field is not empty * is empty, then returns false, the page does not jump * is not empty, then said The user has made the QR code of the scan code login, the page to jump */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. Customize the API to save the client's username

<?php/** * @author Cenquanyu * @version May 12, 2016 * Custom API for Android client scan login, save the client's username to a QR code corresponding to the random number in the corresponding location in the database. * Parameters: Username,randnumber * No return value */$randnumber = $_get (' Randnumber '); $username = $_get (' username '); Require ' mysql_connect.php '; mysql_query ("Update qr_login set username= ' $username ' where randnumber= ' $randnumber '");  ?>

5. Activity of the Android client to perform scan 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 PC-side address @Override protected void onCreate (Bun    Dle 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) {//Sweep code Operation InteNT Intent = new Intent (this, captureactivity.class); Startactivityforresult (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 scans the code and returns the scan result, the QR code pair      The random number should be taken out of String username = Etusername.gettext (). toString ();      String url = web_url + "saveusername.php?randnumber=" + randnumber + "&username=" + username; Httputils.login (URL);//Access URL}}}

6. Network Request Class

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 the whole content of this article, I hope that everyone's study has helped.

http://www.bkjia.com/PHPjc/1127908.html www.bkjia.com true http://www.bkjia.com/PHPjc/1127908.html techarticle PHP generated android client scan can log in QR code, Android client This example for everyone to share the PHP page generated QR code, android client scan login specific code for everyone ...

  • 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.