Android 集成QQ登录详细教程和Lorg/apache/http/conn/scheme/Sch处理
程序员文章站
2022-08-15 19:13:54
首先 现在SDK,目前只能通过Jar包的形式引入下载地址https://wiki.open.qq.com/wik然后:https://open.tencent.com/选择:然后下一步下一步 等创建好:打开刚才下载的SDK拷贝到项目的libs或者jniLibs 然后AddasLibra然后在Manifest中
首先 现在SDK,目前只能通过Jar包的形式引入
下载地址
然后:
然后下一步下一步 等创建好:
打开刚才下载的SDK
拷贝到项目的libs或者jniLibs 然后AddasLibra
然后在Manifest中
<!-- 第三方登录 QQ-->
<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="tencent1105546048" />
</intent-filter>
</activity>
<activity
android:name="com.tencent.connect.common.AssistActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<!-- end -->
Code:
在需要集成的页面
mTencent = Tencent.createInstance(Constant.QQ_APP_ID, this.getApplicationContext());
登录方法
//qq登录
llQQLogin.setOnClickListener(v -> {
if (!mTencent.isSessionValid()) {
mTencent.login(this, "all", this);
}
});
需要实现:
implements IUiListener
Code::::::::::::::::::
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Tencent.onActivityResultData(requestCode,resultCode,data,this);
}
/**
* 腾讯QQ登录回调
* */
@Override
public void onComplete(Object object) {
initOpenIdAndToken(object);
//获取用户信息
getUserInfo();
}
private void initOpenIdAndToken(Object object) {
JSONObject jb = (JSONObject) object;
try {
String openID = jb.getString("openid"); //openid用户唯一标识
String access_token = jb.getString("access_token");
String expires = jb.getString("expires_in");
mTencent.setOpenId(openID);
mTencent.setAccessToken(access_token, expires);
} catch (JSONException e) {
e.printStackTrace();
}
}
private void getUserInfo() {
QQToken token = mTencent.getQQToken();
UserInfo mInfo = new UserInfo(context, token);
mInfo.getUserInfo(new IUiListener() {
@Override
public void onComplete(Object object) {
JSONObject jb = (JSONObject) object;
try {
String name = jb.getString("nickname");
String figureurl = jb.getString("figureurl_qq_2"); //头像图片的url
ToastUtils.show(name);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onError(UiError uiError) {
}
@Override
public void onCancel() {
}
});
}
@Override
public void onError(UiError uiError) {
}
@Override
public void onCancel() {
}
集成完可能会遇到:
NoClassDefFoundError: Failed resolution of: Lorg/apache/http/conn/scheme/Sch
经过测试一般会发生在9.0+的版本上
首先最好加入https网络权限:
请参考:
https://blog.csdn.net/qq_29769851/article/details/105663758
然后:
在Manifest中:加入
<!-- QQ --> <uses-library android:name="org.apache.http.legacy" android:required="false" />
本文地址:https://blog.csdn.net/qq_29769851/article/details/107561907