摘要:今天终于完成了qq登陆的相关内容,主要将数据以json形式回调,取出其中openid、昵称、40*40像素头像链接地址进入数据库。
遇到问题:openid怎么也存不进去,但是去掉openid其他数据可以存进去。(一脸懵逼)
解决方法:重写servlet和util。
private void checkLoginQQ(HttpServletRequest request, HttpServletResponse response) throws IOException { String openid=request.getParameter("openid"); String imguser=request.getParameter("imguser"); String name=request.getParameter("nickname"); Boolean flag=Boolean.parseBoolean(request.getParameter("flag")); System.out.println("checkloginqq"); response.getWriter().write(DBUtil.checkLoginQQ(openid, imguser, name, flag)); }
public static String checkLoginQQ(String openid,String imguser,String name,boolean flag) { //检查qq登陆 JSONObject jsonObject=new JSONObject(); jsonObject.put("status", false); //是否成功标志 Connection connection=getConnection(); String sql="select * from user where id='"+openid+"'"; ResultSet rs=null; if(flag) { try { Statement statement=connection.createStatement(); rs=statement.executeQuery(sql); if(!rs.next()) { sql="insert into user values('"+openid+"','"+name+"'," + "'123','"+getDateString(true)+"','"+imguser.replace("\", "")+"')"; statement.executeUpdate(sql); } jsonObject.put("status", true); jsonObject.put("id", openid); jsonObject.put("msg", "登陆成功"); closeDB(connection, statement, rs); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }else { jsonObject.put("msg", "登陆失败,授权未成功"); } System.out.println(jsonObject.toString()); return jsonObject.toString(); }
private class BaseUiListener implements IUiListener { @Override public void onComplete(Object o) { Log.d("qq", o.toString()); try { JSONObject obj = (JSONObject) o; openID = obj.getString("openid"); String accessToken = obj.getString("access_token"); String expires = obj.getString("expires_in"); mTencent.setOpenId(openID); mTencent.setAccessToken(accessToken, expires); } catch (JSONException e) { e.printStackTrace(); } final QQToken qqToken = mTencent.getQQToken(); UserInfo info = new UserInfo(getApplicationContext(), qqToken); info.getUserInfo(new IUiListener() { //获取基本信息 @Override public void onComplete(Object o) { final JSONObject obj = (JSONObject) o; Log.d("jsonobj",((JSONObject) o).toString().replace("\","")); new Thread(new Runnable() { //开启线程验证登陆 @Override public void run() { try { String reqdata = "method=checkloginqq&openid=" + openID + "&nickname=" + obj.getString("nickname") + "" + "&flag=" + (obj.getInt("ret") == 0) + "&imguser=" + obj.getString("figureurl_2") + ""; String json=String.valueOf(HttpUtil.sendPost(requrl, reqdata)); Log.d("resjson",json); final JSONObject jsonObject = new JSONObject(json); runOnUiThread(new Runnable() { @Override public void run() { try { Toast.makeText(Login.this, jsonObject.getString("msg"), Toast.LENGTH_SHORT).show(); if (jsonObject.getBoolean("status")) { //true代表成功 startActivity(new Intent(Login.this, MainActivity.class) .putExtra("id", jsonObject.getString("id"))); } } catch (JSONException e) { e.printStackTrace(); } } }); } catch (JSONException e) { e.printStackTrace(); } } }).start(); } @Override public void onError(UiError uiError) { Toast.makeText(Login.this, "获取信息失败,请重试", Toast.LENGTH_SHORT).show(); } @Override public void onCancel() { } }); } @Override public void onError(UiError e) { Toast.makeText(Login.this, "获取授权失败,请重试", Toast.LENGTH_SHORT).show(); } @Override public void onCancel() { } }