第三方登录(QQ登录)

来源:互联网 发布:千兆路由器和千兆端口 编辑:程序博客网 时间:2024/05/16 15:56
 public void login() {//                                   在腾讯开放平台获取的APPID        mTencent = Tencent.createInstance("1106541335", this.getApplicationContext());        if (!mTencent.isSessionValid()) {            mTencent.login(this, Scope, listener);        }    }    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        Tencent.onActivityResultData(requestCode, resultCode, data, listener);    }
回调监听类
package jiyun.com.lovepet.qq;import android.content.SharedPreferences;import android.util.Log;import com.google.gson.Gson;import com.tencent.tauth.IUiListener;import com.tencent.tauth.UiError;import org.json.JSONObject;import java.io.IOException;import java.util.HashMap;import java.util.Map;import jiyun.com.lovepet.api.App;import jiyun.com.lovepet.qq.bean.QQ;import jiyun.com.lovepet.qq.bean.QQphoto;import okhttp3.Call;import okhttp3.Callback;import okhttp3.OkHttpClient;import okhttp3.Request;import okhttp3.Response;/** * Created by DELL on 2017/12/17. */public class BaseUiListener implements IUiListener {//"https://graph.qq.com/user/get_user_info?access_token=access_token&oauth_consumer_key=1106541335&openid=openid&format=json"    private String access_token1;    private Object oauth_consumer_key;    private String openid1;    private String format;    @Override    public void onComplete(Object o) {//        mBaseMessageText.setText("onComplete:");//        mMessageText.setText(o.toString());        doComplete((JSONObject) o);    }    protected void doComplete(JSONObject values) {//        values.toString();        Log.e("doComplete: ", values.toString());        Gson gson = new Gson();        QQ qq = gson.fromJson(values.toString(), QQ.class);        String access_token = qq.getAccess_token();        SharedPreferences sharedPreferences = App.baseActivity.getSharedPreferences("user_info", 0);        SharedPreferences.Editor edit = sharedPreferences.edit();        edit.putString("access_token", access_token);        int authority_cost = qq.getAuthority_cost();        int expires_in = qq.getExpires_in();        int login_cost = qq.getLogin_cost();        String msg = qq.getMsg();        String openid = qq.getOpenid();        edit.putString("openid", openid);        String pay_token = qq.getPay_token();        String pf = qq.getPf();        String pfkey = qq.getPfkey();        int query_authority_cost = qq.getQuery_authority_cost();        int ret = qq.getRet();        //获取用户QQ信息        getQQ(access_token, openid);    }    public void getQQ(String access_token, String openid) {        StringBuffer buffer = new StringBuffer("https://graph.qq.com/user/get_user_info");        Map<String, Object> map = new HashMap<>();        map.put("access_token", access_token);        map.put("oauth_consumer_key", 1106541335);        map.put("openid", openid);        map.put("format", "json");        buffer.append("?");        for (int i = 0; i < map.size(); i++) {            access_token1 = (String) map.get("access_token");            oauth_consumer_key = map.get("oauth_consumer_key");            openid1 = (String) map.get("openid");            format = (String) map.get("format");        }        buffer.append("access_token").append("=").append(access_token1).append("&")                .append("oauth_consumer_key").append("=").append(oauth_consumer_key).append("&")                .append("openid").append("=").append(openid1).append("&")                .append("format").append("=").append(format);        final Request request = new Request.Builder()                .url(buffer.toString())                .build();        OkHttpClient okHttpClient=new OkHttpClient.Builder().build();        Call call = okHttpClient.newCall(request);        call.enqueue(new Callback() {            @Override            public void onFailure(Call call, IOException e) {                Log.e("onFailure: ",e.getMessage() );            }            @Override            public void onResponse(Call call, Response response) throws IOException {                String string = response.body().string();                if (response.code() == 200) {                    Gson gson = new Gson();                    QQphoto qQphoto = gson.fromJson(string, QQphoto.class);                    //城市                    String city = qQphoto.getCity();                    Log.e("所在城市: ",city );//                    大小为30×30像素的QQ空间头像URL。                    String figureurl = qQphoto.getFigureurl();                    Log.e("QQ空间头像: ",figureurl );//                    大小为50×50像素的QQ空间头像URL。                    String figureurl_1 = qQphoto.getFigureurl_1();                    Log.e("QQ空间头像: ",figureurl_1 );//                    大小为100×100像素的QQ空间头像URL。                    String figureurl_2 = qQphoto.getFigureurl_2();                    Log.e("QQ空间头像: ",figureurl_2 );//                    大小为40×40像素的QQ头像URL。                    String figureurl_qq_1 = qQphoto.getFigureurl_qq_1();                    Log.e("QQ空间头像: ",figureurl_qq_1 );//                    大小为100×100像素的QQ头像URL。需要注意,不是所有的用户都拥有QQ的100x100的头像,但40x40像素则是一定会有                    String figureurl_qq_2 = qQphoto.getFigureurl_qq_2();                    Log.e("QQ空间头像: ",figureurl_qq_2 );//                    性别。 如果获取不到则默认返回"男"                    String gender = qQphoto.getGender();                    Log.e("性别: ",gender );                    int is_lost = qQphoto.getIs_lost();//                    标识用户是否为黄钻用户(0:不是;1:是)。                    String is_yellow_vip = qQphoto.getIs_yellow_vip();//                    标识是否为年费黄钻用户(0:不是; 1:是)                    String is_yellow_year_vip = qQphoto.getIs_yellow_year_vip();                    String level = qQphoto.getLevel();//                    用户在QQ空间的昵称。                    String nickname = qQphoto.getNickname();                    Log.e("昵称: ",nickname );                    String province = qQphoto.getProvince();//                    返回码                    int ret = qQphoto.getRet();//                    如果ret<0,会有相应的错误信息提示,返回数据全部用UTF-8编码。                    String msg = qQphoto.getMsg();//                    标识用户是否为黄钻用户(0:不是;1:是)                    String vip = qQphoto.getVip();                    //出生在哪年                    String year = qQphoto.getYear();//                    黄钻等级                    String yellow_vip_level = qQphoto.getYellow_vip_level();                }            }        });    }    @Override    public void onError(UiError e) {//        showResult("onError:", "code:" + e.errorCode + ", msg:"//                + e.errorMessage + ", detail:" + e.errorDetail);        Log.e("onError:", "code:" + e.errorCode + ", msg:"                + e.errorMessage + ", detail:" + e.errorDetail);    }    @Override    public void onCancel() {//        showResult("onCancel", "");        Log.e("onCancel: ", "");    }}
//获取QQ信息的bean类
package jiyun.com.lovepet.qq.bean;/** * Created by DELL on 2017/12/18. */public class QQphoto {    private int ret;    private String msg;    private int is_lost;    private String nickname;    private String gender;    private String province;    private String city;    private String year;    private String figureurl;    private String figureurl_1;    private String figureurl_2;    private String figureurl_qq_1;    private String figureurl_qq_2;    private String is_yellow_vip;    private String vip;    private String yellow_vip_level;    private String level;    private String is_yellow_year_vip;    public int getRet() {        return ret;    }    public void setRet(int ret) {        this.ret = ret;    }    public String getMsg() {        return msg;    }    public void setMsg(String msg) {        this.msg = msg;    }    public int getIs_lost() {        return is_lost;    }    public void setIs_lost(int is_lost) {        this.is_lost = is_lost;    }    public String getNickname() {        return nickname;    }    public void setNickname(String nickname) {        this.nickname = nickname;    }    public String getGender() {        return gender;    }    public void setGender(String gender) {        this.gender = gender;    }    public String getProvince() {        return province;    }    public void setProvince(String province) {        this.province = province;    }    public String getCity() {        return city;    }    public void setCity(String city) {        this.city = city;    }    public String getYear() {        return year;    }    public void setYear(String year) {        this.year = year;    }    public String getFigureurl() {        return figureurl;    }    public void setFigureurl(String figureurl) {        this.figureurl = figureurl;    }    public String getFigureurl_1() {        return figureurl_1;    }    public void setFigureurl_1(String figureurl_1) {        this.figureurl_1 = figureurl_1;    }    public String getFigureurl_2() {        return figureurl_2;    }    public void setFigureurl_2(String figureurl_2) {        this.figureurl_2 = figureurl_2;    }    public String getFigureurl_qq_1() {        return figureurl_qq_1;    }    public void setFigureurl_qq_1(String figureurl_qq_1) {        this.figureurl_qq_1 = figureurl_qq_1;    }    public String getFigureurl_qq_2() {        return figureurl_qq_2;    }    public void setFigureurl_qq_2(String figureurl_qq_2) {        this.figureurl_qq_2 = figureurl_qq_2;    }    public String getIs_yellow_vip() {        return is_yellow_vip;    }    public void setIs_yellow_vip(String is_yellow_vip) {        this.is_yellow_vip = is_yellow_vip;    }    public String getVip() {        return vip;    }    public void setVip(String vip) {        this.vip = vip;    }    public String getYellow_vip_level() {        return yellow_vip_level;    }    public void setYellow_vip_level(String yellow_vip_level) {        this.yellow_vip_level = yellow_vip_level;    }    public String getLevel() {        return level;    }    public void setLevel(String level) {        this.level = level;    }    public String getIs_yellow_year_vip() {        return is_yellow_year_vip;    }    public void setIs_yellow_year_vip(String is_yellow_year_vip) {        this.is_yellow_year_vip = is_yellow_year_vip;    }}
//获取QQ id和token的bean类
package jiyun.com.lovepet.qq.bean;/** * Created by DELL  on 2017/12/17. */public class QQ {    private int ret;    private String openid;    private String access_token;    private String pay_token;    private int 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 int getExpires_in() {        return expires_in;    }    public void setExpires_in(int 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;    }}
//好像没有用的的监听类
package jiyun.com.lovepet.qq;import android.util.Log;import com.tencent.open.utils.HttpUtils;import com.tencent.tauth.IRequestListener;import org.apache.http.conn.ConnectTimeoutException;import org.json.JSONException;import org.json.JSONObject;import java.io.IOException;import java.net.MalformedURLException;import java.net.SocketTimeoutException;/** * Created by DELL  on 2017/12/17. */public class BaseApiListener implements IRequestListener {    @Override    public void onComplete(JSONObject jsonObject) {//        showResult("IRequestListener.onComplete:", jsonObject.toString());        Log.e("onComplete: ",jsonObject.toString() );        doComplete(jsonObject);    }    private void doComplete(JSONObject jsonObject) {    }    @Override    public void onIOException(IOException e) {//        showResult("IRequestListener.onIOException:", e.getMessage());        Log.e("onIOException: ", e.getMessage());    }    @Override    public void onMalformedURLException(MalformedURLException e) {//        showResult("IRequestListener.onMalformedURLException", e.toString());        Log.e("onMalformedURLException", e.toString());    }    @Override    public void onJSONException(JSONException e) {//        Log.e("IRequestListener.onJSONException:", e.getMessage());        Log.e("onJSONException: ", e.getMessage());    }    @Override    public void onConnectTimeoutException(ConnectTimeoutException e) {// TODO Auto-generated method stub    }    @Override    public void onSocketTimeoutException(SocketTimeoutException e) {// TODO Auto-generated method stub    }    //1.4版本中IRequestListener 新增两个异常    @Override    public void onNetworkUnavailableException(HttpUtils.NetworkUnavailableException e) {// 当前网络不可用时触发此异常    }    @Override    public void onHttpStatusException(HttpUtils.HttpStatusException e) {// http请求返回码非200时触发此异常    }    @Override    public void onUnknowException(Exception e) {// 出现未知错误时会触发此异常    }}
清单文件配置 : 用哪个你复制哪个
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="jiyun.com.lovepet">
    <uses-permission android:name="android.permission.INTERNET"/>    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <application        android:name=".api.App"        android:allowBackup="true"        android:icon="@mipmap/pet_icon"        android:label="@string/app_name"        android:supportsRtl="true"        android:theme="@style/AppTheme">        <activity android:name=".ui.MainActivity"></activity>        <activity android:name=".ui.order.activity.MyOrderActivity" />        <activity android:name=".ui.BaseActivity" />        <activity android:name=".ui.pet.activity.MyPetActivity" />        <activity android:name=".ui.pet.activity.LoginActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <activity android:name=".ui.pet.activity.BingPhoneActivity" />        <activity android:name=".ui.pet.activity.ToolbarActivity" />        <activity android:name=".ui.pet.activity.WeChatActivity" />        <activity android:name=".ui.pet.activity.RegisterActivity"></activity>        <activity android:name=".order.XiangQingActivity">
        </activity>        <activity android:name=".order.EvaluateActivity">
        </activity>
        <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="tencent1106541335" />            </intent-filter>        </activity>
        <activity            android:name="com.tencent.connect.common.AssistActivity"            android:configChanges="orientation|keyboardHidden|screenSize"            android:theme="@android:style/Theme.Translucent.NoTitleBar" />
        <activity android:name=".ui.pet.activity.HomeActivity" />
        <application>            <activity android:name="com.tencent.plus.ImageActivity" />        </application>
    </application>
</manifest>
  
QQ登录配置等信息的网址 :http://wiki.open.qq.com/wiki/%E5%88%9B%E5%BB%BA%E5%B9%B6%E9%85%8D%E7%BD%AE%E5%B7%A5%E7%A8%8B
获取头像等个人信息的网址:http://wiki.open.qq.com/wiki/website/get_simple_userinfo
原创粉丝点击