編輯:關於Android編程
1,web端生成二維碼,傳遞uuid,並存入數據庫
2,web端輪訓查詢信息,是否有數據庫掃描二維碼信息
3,手機端掃描二維碼,獲取UUID,傳遞用戶名、密碼web端
4,web查詢數據庫中用戶信息(UserLogin),將登陸信息(用戶名,密碼)存入數據庫。
5,web輪訓查到用戶登陸信息,信息完整,則登陸成功。
@Entity @Table(name = "UserLogin") public class UserLogin { @Id @GeneratedValue(generator = "UserLoginIdGenerator") @GenericGenerator(name = "UserLoginIdGenerator", strategy = "uuid") private String id; @Column private String name; @Column private String password; @Column private int uuid; public String getId() { return id; } public void setId(String id) { this.id = id; } public int getUuid() { return uuid; } public void setUuid(int uuid) { this.uuid = uuid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
import java.io.IOException; import java.io.PrintWriter; import java.util.Date; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.engine.transaction.jta.platform.internal.JOnASJtaPlatform; import sun.org.mozilla.javascript.internal.json.JsonParser; import com.alibaba.fastjson.JSONObject; import com.jie.qrcode.util.HibernateUtil; import com.jie.qrcode.util.QRCodeUtil; import com.jie.qrcode.vo.UserLogin; /** * @ClassName: GetQrCodeServlet.java * @Description: TODO(生成二維碼) * @author yangjie * @version V1.0 * @Date 2016-5-16 上午11:05:27 */ public class GetQrCodeServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); // 生成唯一ID int uuid = (int) (Math.random() * 100000); // 二維碼內容 // String content = // "http://192.168.0.199:8080/QrCodeLogin/PhoneLoginServlet?uuid=" // + uuid; JSONObject obj = new JSONObject(); obj.put("url", "http://192.168.0.199:8080/QrCodeLogin/PhoneLoginServlet"); obj.put("uuid", uuid); String content = obj.toString(); System.out.println("obj:" + content); // 生成二維碼 String imgName = uuid + "_" + (int) (new Date().getTime() / 1000) + ".png"; String contextRoot = request.getContextPath(); String webpath = request.getSession().getServletContext() .getRealPath("/"); System.out.println(webpath); String imgPath = webpath + "/img/qrCodeImg/" + imgName; String logoPath = webpath + "/img/logo.png"; QRCodeUtil handler = new QRCodeUtil(); // handler.encoderQRCode(content, imgPath, "png"); handler.encoderQRCode(content, imgPath, logoPath, "png"); // uuid保存 SessionFactory sf = HibernateUtil.getSessionFacotry(); Session session = sf.openSession(); session.beginTransaction();// 事務開始 UserLogin userlogin = new UserLogin(); userlogin.setUuid(uuid); session.save(userlogin);// 將uuid保存到數據庫 session.getTransaction().commit();// 事務提交 session.close(); sf = null; // 生成的圖片訪問地址 String qrCodeImg = "http://192.168.0.199:8080/QrCodeLogin/img/qrCodeImg/" + imgName; String jsonStr = "{\"uuid\":" + uuid + ",\"qrCodeImg\":\"" + qrCodeImg + "\"}"; out.print(jsonStr); out.flush(); out.close(); } }
import java.io.IOException; import java.io.PrintWriter; import java.util.Date; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.hibernate.Criteria; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.criterion.Restrictions; import com.jie.qrcode.util.HibernateUtil; import com.jie.qrcode.vo.UserLogin; /** * @ClassName: LongConnectionCheckServlet.java * @Description: TODO(用長連接,檢查登錄狀態) * @author yangjie * @version V1.0 * @Date 2016-5-16 上午11:09:32 */ public class LongConnectionCheckServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int uuid = Integer.parseInt(request.getParameter("uuid")); String jsonStr = ""; System.out.println("in"); System.out.println("uuid:" + uuid); long inTime = new Date().getTime(); Boolean bool = true; while (bool) { try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } // 根據uuid查詢數據庫登錄表信息 String username = ""; SessionFactory sf = HibernateUtil.getSessionFacotry(); Session session = sf.openSession(); Criteria criteria = session.createCriteria(UserLogin.class); criteria.add(Restrictions.eq("uuid", uuid)); Listresult = criteria.list(); if (result.size() > 0) { for (UserLogin s : result) { username = s.getName(); if(!"".equals(username)&&username!=null){//如果用戶名信息不為空,則登陸成功 bool = false; session.clear(); session.beginTransaction(); session.delete(s);//刪除二維碼登錄信息,表明已登錄 session.getTransaction().commit(); } jsonStr = "{\"uname\":\"" + username + "\"}"; System.out.println(username); } } else { if (new Date().getTime() - inTime > 5000) { bool = false; } } session.close(); sf = null; } System.out.println("login ok : " + jsonStr); PrintWriter out = response.getWriter(); out.print(jsonStr); out.flush(); out.close(); } }
import java.io.IOException; import java.io.PrintWriter; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.hibernate.Criteria; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.criterion.Restrictions; import com.jie.qrcode.util.HibernateUtil; import com.jie.qrcode.vo.UserLogin; /** * @ClassName: PhoneLoginServlet.java * @Description: TODO(手機二維碼登陸) * @author yangjie * @version V1.0 * @Date 2016-5-16 上午11:20:16 */ public class PhoneLoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; public PhoneLoginServlet() { super(); // TODO Auto-generated constructor stub } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int uuid = Integer.parseInt(request.getParameter("uuid")); String uname = request.getParameter("uname"); String upwd = request.getParameter("upwd"); System.out.println(uuid); System.out.println(uname); System.out.println(upwd); // 驗證登陸 boolean bool = true; //是否登陸 boolean islogin = false; if (bool) { // 將登陸信息存入數據庫 SessionFactory sf = HibernateUtil.getSessionFacotry(); Session session = sf.openSession(); Criteria criteria = session.createCriteria(UserLogin.class); criteria.add(Restrictions.eq("uuid", uuid)); Listresult = criteria.list(); if (result.size() > 0) { for (UserLogin s : result) { s.setName(uname); s.setPassword(upwd); session.clear(); session.beginTransaction();//開始事務 session.saveOrUpdate(s); session.getTransaction().commit();// 事務提交 bool = false; islogin = true; } }else{//沒有該uuid信息,表明已登錄,二維碼過期 islogin = false; System.out.println("二維碼已過期,請刷新頁面"); } session.close(); sf = null; } PrintWriter out = response.getWriter(); out.print(islogin);//傳遞登陸是否成功 out.flush(); out.close(); } }
package com.jie.qrcode; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; import com.alibaba.fastjson.JSONObject; import com.lidroid.xutils.HttpUtils; import com.lidroid.xutils.exception.HttpException; import com.lidroid.xutils.http.RequestParams; import com.lidroid.xutils.http.ResponseInfo; import com.lidroid.xutils.http.callback.RequestCallBack; import com.lidroid.xutils.http.client.HttpRequest; import com.lidroid.xutils.http.client.HttpRequest.HttpMethod; import com.zxing.activity.CaptureActivity; public class MainActivity extends Activity implements OnClickListener{ private Button btnScan; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnScan = (Button) findViewById(R.id.btn_scan); btnScan.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(this, CaptureActivity.class); startActivityForResult(intent, 0); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if(resultCode==Activity.RESULT_OK){ String result = data.getExtras().getString("result");//獲取掃描的結果 //Log.i("result",result); JSONObject obj = JSONObject.parseObject(result); String uuid = obj.getString("uuid"); String httpUrl = obj.getString("url"); //Toast.makeText(getApplicationContext(), result, 1000).show(); //將掃描內容提交到網頁 HttpUtils http = new HttpUtils(); RequestParams params = new RequestParams(); params.addBodyParameter("uuid", uuid); //Log.i("uuid",uuid); params.addBodyParameter("uname", "yangjie"); params.addBodyParameter("upwd", "123456"); http.send(HttpMethod.POST, httpUrl, params , new RequestCallBack() { @Override public void onFailure(HttpException arg0, String arg1) { // TODO Auto-generated method stub Log.i("http.send","failuer"); Toast.makeText(getApplicationContext(), "登陸失敗,二維碼過期,請刷新頁面!", 1000).show(); } @Override public void onSuccess(ResponseInfo arg0) { // TODO Auto-generated method stub Log.i("http.send","success"); Toast.makeText(getApplicationContext(), "登陸成功!", 1000).show(); } }); } } }
前言大家都知道Android Studio目前已經更新到2.0 Preview 6了,作為Google大力推崇的開發工具,相對於Eclipse ADT有著不可比擬的優勢。
今天碰到一個關於Button的問題:android Button上面的英文字符串會自動變成大寫,運行的Android 5.1版本,如下圖所示:圖1:Button圖2:Te
前言 我K,今天居然是情人節,對於資深的單身狗來說,簡直是個噩耗,今天注定是各種秀恩愛,心塞中。。。。 話題到此結束,管他什麼情人節,今天給大家帶來的
先給大家展示效果圖,看看是大家想要的效果嗎,如果還滿意,請參考以下代碼:前言 UniversalImageLoader是用於加載圖片的一個開源項目,在其項目介紹中是這麼寫