Android 實現QQ第三方登錄
-
Android 實現QQ第三方登錄
首先肯定是去下載SDK和DEMO
http://wiki.open.qq.com/wiki/mobile/SDK下載
本文是我自己整合後的簡單DEMO。
先看下效果圖吧
原理:我們要使用QQ登錄我們的應用,不是不用註冊,是我們在後臺爲用戶註冊了,但是用戶不知道,註冊需要唯一標識,上圖的那串字母與數字的組合就是我們要獲得的唯一標識:OpenID.<喎�"http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+uPrXxbT6wuvAtMu1sMmhozwvcD4KPHA+ytfPyKOsztLDx9KqvNPU2G9wZW5fc2RrLmphcrrNbXRhLXNkay0xLjAwLmphctXiwb249rzcsPzLs7Hjv7TPws7S19y5stPDtb21xMDgPC9wPgo8cD48aW1nIHNyYz0="http://www.2cto.com/uploadfile/Collfiles/20140910/201409100906079.jpg" alt="\">
其中,AppConstant中是用來放置APPID的,由於考慮到還可能引入其他第三方登錄,爲方便管理,故創建此類。Util是根據路徑從網上獲取圖片的處理類
好了進入主題
首先在AndroidManifest.xml中進行兩個定義如果不定義是不行的
123456789101112<intent-filter>
<category android:name=
"android.intent.category.DEFAULT"
>
<category android:name=
"android.intent.category.BROWSABLE"
>
<data android:scheme=
"tencent222222"
><!--—注意在這裏用你的appid替換
222222
---->
</data></category></category></action></intent-filter>
</activity>
</activity>
然後是兩個權限
123<uses-permission android:name=
"android.permission.INTERNET"
>
<uses-permission android:name=
"android.permission.ACCESS_NETWORK_STATE"
>
</uses-permission></uses-permission>
接下來是佈局文件,activity_main.xml登錄按鈕,獲取頭像、暱稱、openid的textview
12345678<linearlayout xmlns:android=
"http://schemas.android.com/apk/res/android"
xmlns:tools=
"http://schemas.android.com/tools"
android:layout_width=
"fill_parent"
android:layout_height=
"fill_parent"
android:orientation=
"vertical"
>
<button android:id=
"@+id/login"
android:layout_width=
"fill_parent"
android:layout_height=
"wrap_content"
android:text=
"登錄"
>
<imageview android:id=
"@+id/user_logo"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
>
<textview android:id=
"@+id/user_nickname"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:textcolor=
"#80505050"
android:textsize=
"18sp"
>
<textview android:id=
"@+id/user_openid"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
>
</textview></textview></imageview></button></linearlayout>
然後是MainActivity
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081public
class
MainActivity
extends
Activity
implements
OnClickListener {
TextView openidTextView;
TextView nicknameTextView;
Button loginButton;
ImageView userlogo;
private
Tencent mTencent;
public
static
QQAuth mQQAuth;
public
static
String mAppid;
public
static
String openidString;
public
static
String nicknameString;
public
static
String TAG=
"MainActivity"
;
Bitmap bitmap =
null
;
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//用來登錄的Button
loginButton=(Button)findViewById(R.id.login);
loginButton.setOnClickListener(
this
);
//用來顯示OpenID的textView
openidTextView=(TextView)findViewById(R.id.user_openid);
//用來顯示暱稱的textview
nicknameTextView=(TextView)findViewById(R.id.user_nickname);
//用來顯示頭像的Imageview
userlogo=(ImageView)findViewById(R.id.user_logo);
}
public
void
onClick(View v) {
// TODO Auto-generated method stub
switch
(v.getId()) {
case
R.id.login:
LoginQQ();
break
;
default
:
break
;
}
}
//這裏是調用QQ登錄的關鍵代碼
public
void
LoginQQ() {
//這裏的APP_ID請換成你應用申請的APP_ID,我這裏使用的是DEMO中官方提供的測試APP_ID 222222
mAppid = AppConstant.APP_ID;
//第一個參數就是上面所說的申請的APPID,第二個是全局的Context上下文,這句話實現了調用QQ登錄
mTencent = Tencent.createInstance(mAppid,getApplicationContext());
/**通過這句代碼,SDK實現了QQ的登錄,這個方法有三個參數,第一個參數是context上下文,第二個參數SCOPO 是一個String類型的字符串,表示一些權限
官方文檔中的說明:應用需要獲得哪些API的權限,由“,”分隔。例如:SCOPE = “get_user_info,add_t”;所有權限用“all”
第三個參數,是一個事件監聽器,IUiListener接口的實例,這裏用的是該接口的實現類 */
mTencent.login(MainActivity.
this
,
"all"
,
new
BaseUiListener());
}
/**當自定義的監聽器實現IUiListener接口後,必須要實現接口的三個方法,
* onComplete onCancel onError
*分別表示第三方登錄成功,取消 ,錯誤。*/
private
class
BaseUiListener
implements
IUiListener {
public
void
onCancel() {
// TODO Auto-generated method stub
}
public
void
onComplete(Object response) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
"登錄成功"
,
0
).show();
try
{
//獲得的數據是JSON格式的,獲得你想獲得的內容
//如果你不知道你能獲得什麼,看一下下面的LOG
Log.e(TAG,
"-------------"
+response.toString());
openidString = ((JSONObject) response).getString(
"openid"
);
openidTextView.setText(openidString);
Log.e(TAG,
"-------------"
+openidString);
//access_token= ((JSONObject) response).getString("access_token"); //expires_in = ((JSONObject) response).getString("expires_in");
}
catch
(JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/**到此已經獲得OpneID以及其他你想獲得的內容了
QQ登錄成功了,我們還想獲取一些QQ的基本信息,比如暱稱,頭像什麼的,這個時候怎麼辦?
sdk給我們提供了一個類UserInfo,這個類中封裝了QQ用戶的一些信息,我麼可以通過這個類拿到這些信息
如何得到這個UserInfo類呢? */
QQToken qqToken = mTencent.getQQToken();
UserInfo info =
new
UserInfo(getApplicationContext(), qqToken);
//這樣我們就拿到這個類了,之後的操作就跟上面的一樣了,同樣是解析JSON
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980info.getUserInfo(
new
IUiListener() {
public
void
onComplete(
final
Object response) {
// TODO Auto-generated method stub
Log.e(TAG,
"---------------111111"
);
Message msg =
new
Message();
msg.obj = response;
msg.what =
0
;
mHandler.sendMessage(msg);
Log.e(TAG,
"-----111---"
+response.toString());
/**由於圖片需要下載所以這裏使用了線程,如果是想獲得其他文字信息直接
* 在mHandler裏進行操作
*
*/
new
Thread(){
@Override
public
void
run() {
// TODO Auto-generated method stub
JSONObject json = (JSONObject)response;
try
{
bitmap = Util.getbitmap(json.getString(
"figureurl_qq_2"
));
}
catch
(JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Message msg =
new
Message();
msg.obj = bitmap;
msg.what =
1
;
mHandler.sendMessage(msg);
}
}.start();
}
public
void
onCancel() {
Log.e(TAG,
"--------------111112"
);
// TODO Auto-generated method stub
}
public
void
onError(UiError arg0) {
// TODO Auto-generated method stub
Log.e(TAG,
"-111113"
+
":"
+arg0);
}
});
}
public
void
onError(UiError arg0) {
// TODO Auto-generated method stub
}
}
Handler mHandler =
new
Handler() {
@Override
public
void
handleMessage(Message msg) {
if
(msg.what ==
0
) {
JSONObject response = (JSONObject) msg.obj;
if
(response.has(
"nickname"
)) {
try
{
nicknameString=response.getString(
"nickname"
);
nicknameTextView.setText(nicknameString);
Log.e(TAG,
"--"
+nicknameString);
}
catch
(JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
else
if
(msg.what ==
1
){
Bitmap bitmap = (Bitmap)msg.obj;
userlogo.setImageBitmap(bitmap);
}
}
};
}
上圖是登錄Q的返回LOG
上圖是我們獲得騰訊提供的UserInfo返回的LOG
然後是AppConstant.java
123public
class
AppConstant {
public
static
String APP_ID=
"222222"
;
}
然後是Util.java
12345678910111213141516171819202122232425public
class
Util {
public
static
String TAG=
"UTIL"
;
public
static
Bitmap getbitmap(String imageUri) {
Log.v(TAG,
"getbitmap:"
+ imageUri);
// 顯示網絡上的圖片
Bitmap bitmap =
null
;
try
{
URL myFileUrl =
new
URL(imageUri);
HttpURLConnection conn = (HttpURLConnection) myFileUrl
.openConnection();
conn.setDoInput(
true
);
conn.connect();
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
Log.v(TAG,
"image download finished."
+ imageUri);
}
catch
(IOException e) {
e.printStackTrace();
Log.v(TAG,
"getbitmap bmp fail---"
);
return
null
;
}
return
bitmap;
}
}
至此全部代碼就在這裏了,我們獲得了OpenID這個唯一標識最關鍵的東西,然後看項目中