由于现在市场越来越开了,每个app 都基本上都有第三方登录;
下面介绍的是qq登陆
第一步 去官网创建一个应用 ,没有开发者账号可以点击这里查看流程并注册
这时候我们获取APPID 和 APPKEY了,我们下载官网的jar包 下载地址
把jar包放到lib文件下,右键把jar包和项目依赖
接下来配置AndroidMainfest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="x x x x x x x">
<!-- 添加下面这几个权限 -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
......
<!-- 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.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="这里填写你注册ID" />
</intent-filter>
</activity>
<!-- SDK_V2.0后添加 -->
<activity
android:name="com.tencent.connect.common.AssistActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:theme="@android:style/Theme.NoTitleBar" />
</application>
这是我们完成了简单配置程序,下面我们试着回调函数,首先在登陆Activity页面初始化Tencent值,然后调用login方法登陆;
/**
* 这是登陆界面
*/
public class LoginActivity extends BaseActivity{
/**
* 第一步定义一个全局的Tencent
*/
private Tencent mTencent;
/**
* qq登陆成功返回的数据
*/
private LoginParmas.Tencent mTencentParams;
/**
* 成功回调
*/
private MyIUiLisener mLisener;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setLayoutView(xxx);
/**
* 这里完成初始化
*/
mTencent = Tencent.createInstance("这里填写获取的ID",this.getApplicationContext());
mLisener = new MyIUiLisener();
/**
* 登陆按钮点击事件
*/
findViewById(R.id.xxx).onClickLisener(new OnClickLisener(){
@Override
public void onClick(View v) {
/**
* 这里是打开qq登陆界面并获取登陆一些token信息
*
* "all"代表的是获取全部权限
*/
mTencent.login(LoginActiviyt.this,"all",mLisener);
}
});
}
/**
* 登陆成功的回调
*/
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data){
//这里转接回调
Tencent.onActivityResultData(requestCode, resultCode, data, mLisener);
}
/**
* 登陆成功回调
*/
class MyIUiLisener implements IUiLisener{
@Override
public void onComplete(Object o) {
//这里是登陆成功的回调 jsonObject是成功返回回调的json数据
JsonObject jsonObject = (JsonObject)o;
//在这里用第三方json解析数据
mTencentParmas = new Gson().
fromJson(jsonObject.toString(), LoginParams.Tencent.class);
}
@Override
public void onError(UiError uiError) {
//登陆失败的回调
}
@Override
public void onCancel() {
//返回登陆调用
}
}
}
}
/**
* Created by zhiShen
* Data:16/5/31 15:37
* by:
*/
public class LoginParams {
/**
* qq登陆数据
*/
public static class Tencent{
private int ret;
private String openid;
private String access_token;
private String pay_token;
private String expires_in;
private String pf;
private String pfkey;
private String msg;
private int login_cost;
private int query_authority_cost;
private int authority_cost;
public int getRet() {
return ret;
}
public void setRet(int ret) {
this.ret = ret;
}
public String getOpenid() {
return openid;
}
public void setOpenid(String openid) {
this.openid = openid;
}
public String getAccess_token() {
return access_token;
}
public void setAccess_token(String access_token) {
this.access_token = access_token;
}
public String getPay_token() {
return pay_token;
}
public void setPay_token(String pay_token) {
this.pay_token = pay_token;
}
public String getExpires_in() {
return expires_in;
}
public void setExpires_in(String expires_in) {
this.expires_in = expires_in;
}
public String getPf() {
return pf;
}
public void setPf(String pf) {
this.pf = pf;
}
public String getPfkey() {
return pfkey;
}
public void setPfkey(String pfkey) {
this.pfkey = pfkey;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int getLogin_cost() {
return login_cost;
}
public void setLogin_cost(int login_cost) {
this.login_cost = login_cost;
}
public int getQuery_authority_cost() {
return query_authority_cost;
}
public void setQuery_authority_cost(int query_authority_cost) {
this.query_authority_cost = query_authority_cost;
}
public int getAuthority_cost() {
return authority_cost;
}
public void setAuthority_cost(int authority_cost) {
this.authority_cost = authority_cost;
}
}
}
上面完成了简单的登陆功能,接下来完成获取登陆用户详细的数据。回到刚才的Activity 页面。
public class LoginActivity extends BaseActivity
{
/**
* qq登陆成功返回的数据
*/
private LoginParmas.Tencent mTencentParams;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setLayoutView(R.layout.xxx);
//创建一个获取详情事件
findViewById(R.id.xxx).onClickLisener(new OnClickLisener(){
@Override
public void onClick(View v)
{
/**
* 开启一个子线程
*/
new Thear(new Runable()
{
@Override
public void run()
{
try {
Bundle bundle = new Bundle();
bundle.putString("openid", mTencentParams.getOpenId());
bundle.putString("openkey", mTencentParams.getAccess_token());
bundle.putString("pf", mTencentParams.getPf());
bundle.putString("appid","你的appid");
bundle.putString("format", "json");
/**
* 参数1:列表 url :http://wiki.open.qq.com/wiki/website/API列表
* 参数2:需要传过去的参数
* 参数3:get或post请求;
* 返回用户空间的数据;
*/
JSONObject object = mTencent.request("v3/user/get_info", bundle, Constants.HTTP_GET);
Log.e("TAG", "object-->" + object);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
}
}
}
昨天被这玩意糊弄了一个下午,官网文档很多参数都不知道那里去了