Source code introduction: Because it involves the personal information security of customers, you need to make simple modifications according to this blog (http://blog.csdn.net/csdnfml/article/details/49446479)
Source code effect:
Source code snippet:
-
- public void loginQQ(){
-
- if (!mTencent.isSessionValid()){
- mTencent.login( this , "all" ,loginListener);
- }
- else {
- mTencent.logout( this );
- mTencent.login( this , "all" ,loginListener);
- }
- }
- IUiListener loginListener = new BaseUiListener() {
- @Override
- protected void doComplete(JSONObject values) {
- initOpenidAndToken(values);
- updateUserInfo();
- }
- };
-
- public static void initOpenidAndToken(JSONObject jsonObject) {
- try {
- String token = jsonObject.getString(Constants.PARAM_ACCESS_TOKEN);
- String expires = jsonObject.getString(Constants.PARAM_EXPIRES_IN);
- String openId = jsonObject.getString(Constants.PARAM_OPEN_ID);
- if (!TextUtils.isEmpty(token) && !TextUtils.isEmpty(expires) && !TextUtils.isEmpty(openId)) {
- mTencent.setAccessToken(token, expires);
- mTencent.setOpenId(openId);
- }
- } catch (Exception e) {
- }
- }
-
- private void updateUserInfo() {
- if (mTencent != null && mTencent.isSessionValid()) {
- IUiListener listener = new IUiListener() {
- @Override
- public void onError(UiError e) {
- Message msg = new Message();
- msg.obj = "Change the mobile phone time to obtain network time" ;
- msg.what = 1 ;
- mHandler.sendMessage(msg);
- }
-
- @Override
- public void onComplete( final Object response) {
- Message msg = new Message();
- msg.obj = response;
- msg.what = 0 ;
- mHandler.sendMessage(msg);
- }
- @Override
- public void onCancel() {
- Message msg = new Message();
- msg.obj = "Failed to obtain user information" ;
- msg.what = 2 ;
- mHandler.sendMessage(msg);
- }
- };
- mInfo = new UserInfo( this , mTencent.getQQToken());
- mInfo.getUserInfo(listener);
- } else {
-
- }
- }
- Handler mHandler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- if (msg.what == 0 ) {
- JSONObject response = (JSONObject) msg.obj;
- if (response.has( "nickname" )) {
- try {
- log( "User information is obtained successfully, and the result is returned: " + response.toString());
- mThirdLoginResult.setText( "Login successful\n" + "Nickname:" +response.getString( "nickname" )+ "\nAvatar address:" +response.get( "figureurl_qq_1" ));
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- } else if (msg.what == 1 ){
- mThirdLoginResult.setText(msg+ "" );
- } else if (msg.what == 2 ){
- mThirdLoginResult.setText(msg+ "" );
- }
- }
-
- };
-
- private class BaseUiListener implements IUiListener {
- @Override
- public void onComplete(Object response) {
- if ( null == response ) {
- mToast( "Login failed" );
- return ;
- }
- JSONObject jsonResponse = (JSONObject) response;
- if ( null != jsonResponse && jsonResponse.length() == 0 ) {
- mToast( "Login failed" );
- return ;
- }
- log( "QQ login successfully returned result -" + response.toString());
- doComplete((JSONObject)response);
- }
- protected void doComplete(JSONObject response) {}
- @Override
- public void onError(UiError e) {
- Util.toastMessage(MainActivity. this , "onError: " + e.errorDetail);
- Util.dismissDialog();
- }
- @Override
- public void onCancel() {
- Util.toastMessage(MainActivity. this , "onCancel: " );
- Util.dismissDialog();
- if (isServerSideLogin) {
- isServerSideLogin = false ;
- }
- }
- }
-
Download address: http://download..com/data/2115240 |