Android calls third party QQ login code sharing _android

Source: Internet
Author: User
Tags object object openid

This article for everyone to share the call QQ login related code, I hope to help you, reduce the project development time, the specific contents are as follows

1, to QQ open Platform Registration account (http://open.qq.com/), for the application of QQ app_id, and download the relevant jar package, put to the project's Lib directory.

2, in the manifest.xml registered QQ related activity, the code is as follows

 <activity 
android:name= "com.tencent.connect.common.AssistActivity" 
android:screenorientation= " Portrait " 
android:theme=" @android: Style/theme.translucent.notitlebar "/>   
<activity 
android: Name= "com.tencent.tauth.AuthActivity" 
android:launchmode= "Singletask" 
android:nohistory= "true" > 
 <intent-filter> 
  <action android:name= "Android.intent.action.VIEW"/> 
  <category Android:name= "Android.intent.category.DEFAULT"/> 
  <category android:name= " Android.intent.category.BROWSABLE "/> 
  <data android:scheme=" tencent100491239 "/> 
 </ Intent-filter> 
</activity> 

3, using the relevant API provided by QQ to obtain related OpenID, nicknames, images and other information. Here is a point to note that, because the QQ server back to OpenID and nicknames and other information is in two steps, and the first return of the OpenID fast, return nickname and other information may be slower, so in return to OpenID, you can think that the QQ has been called successful login, Other information, such as nicknames, is processed in an asynchronous manner. Here is the Code section, which has been tested by the project and can be used directly (replace the corresponding app_id)

Import java.io.IOException; 
Import java.net.MalformedURLException; 
 
Import java.net.SocketTimeoutException; 
Import org.apache.http.conn.ConnectTimeoutException; 
Import org.json.JSONException; 
 
Import Org.json.JSONObject; 
 
Import android.app.Activity; 
Import com.smart.util.BroadcastAction; 
Import Com.smart.util.BroadcastUtil; 
Import com.smart.util.Constant; 
Import Com.smart.util.ILog; 
Import Com.smart.util.PrefUtil; 
Import Com.smart.util.Prefkey; 
Import com.tencent.open.HttpStatusException; 
Import com.tencent.open.NetworkUnavailableException; 
Import com.tencent.tauth.Constants; 
Import Com.tencent.tauth.IRequestListener; 
Import Com.tencent.tauth.IUiListener; 
Import com.tencent.tauth.Tencent; 
Import Com.tencent.tauth.UiError; 
 
Import Com.utils.lib.ss.common.ToastHelper; 
 /** * QQ Landing. 
  
 * @author E */public class Qqlogin {private static qqlogin qqlogin = null; public static Qqlogin getinstance () {if (null = = Qqlogin) {qqlogin = new Qqlogin (); 
 return qqlogin; public void Login (activity context, Qqloginlistener Loginlistener) {//constant.qq_app_id here to replace the app_id T for their own application 
  Encent Tencent = Tencent.createinstance (constant.qq_app_id, context); 
   if (null = = Tencent) {Toasthelper.maketext (context, "Error cased!"); 
  Return 
 Tencent.login (Context, "Get_user_info", New Baseuilistener (Tencent,loginlistener)); 
  Class Baseuilistener implements iuilistener{Tencent Tencent = null; 
   
  Qqloginlistener qqloginlistener = null; 
   Public Baseuilistener (Tencent Tencent, Qqloginlistener Qqloginlistener) {super (); 
   This.tencent = Tencent; 
  This.qqloginlistener = Qqloginlistener;  
    @Override public void OnComplete (Jsonobject values) {try {String OpenID = values.getstring ("OpenID"); 
    Qqloginlistener.onloginsuccess (OpenID); Tencent.requestasync (Constants.graph_simple_user_info, NULL, Constants.http_get, new Baseapilistener (QqLoginListen ER), NULL); 
   catch (Exception e) {e.printstacktrace (); 
  @Override public void OnError (Uierror arg0) {qqloginlistener.onloginfail (); 
  @Override public void OnCancel () {qqloginlistener.oncancel (); 
 
  } class Baseapilistener implements irequestlistener{Qqloginlistener qqloginlistener = null; 
   Public Baseapilistener (Qqloginlistener Qqloginlistener) {super (); 
  This.qqloginlistener = Qqloginlistener; @Override public void OnComplete (Jsonobject jsonobject, Object object) {try {ilog.e (jsonobject.tostring 
    ()); 
    String nickname = jsonobject.getstring ("nickname"); 
    String figureurl_qq_1 = jsonobject.getstring ("figureurl_qq_2"); 
    Prefutil.instance (). Setpref (Prefkey.third_nickname, nickname); 
    Prefutil.instance (). Setpref (Prefkey.third_image, figureurl_qq_1); 
   Send broadcast to homepage to update broadcastutil.sendbroadcast (broadcastaction.upload_third_info); catch (Exception e) {E.printstacktraCE (); @Override public void Onconnecttimeoutexception (Connecttimeoutexception arg0, Object arg1) {} @Ov  Erride public void Onhttpstatusexception (Httpstatusexception arg0, Object arg1) {} @Override public void Onioexception (IOException arg0, Object arg1) {} @Override public void Onjsonexception (Jsonexception arg0,  Object arg1) {} @Override public void Onmalformedurlexception (Malformedurlexception arg0, Object arg1) 
  {} @Override public void Onnetworkunavailableexception (Networkunavailableexception arg0, Object arg1) { @Override public void Onsockettimeoutexception (Sockettimeoutexception arg0, Object arg1) {} @Overrid e public void Onunknowexception (Exception arg0, Object arg1) {}}/** * After the authorization is complete, perform a step callback/public STA 
  TIC abstract class qqloginlistener{public void Onloginsuccess (String OpenID) {}; public void Onloginsuccess (string OpenID, StringNickname) {}; 
  public void Onloginfail () {}; 
 public void OnCancel () {}; 
 }

Well, the Code section has already been written, just call login (activity context, Qqloginlistener Loginlistener) method is ok.

The above is the Android to call the third party login detailed steps and ideas, I hope to help you learn.

Related Article

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.