当前位置:   article > 正文

Android实现QQ第三方登录_登录icqq

登录icqq

1.叫老大给AppId。

2.在xml里加入权限(我这里有多余的,不过你要是负责整个App的话,这几个多出的就不用在意,你后面还是会用到的):

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission-sdk-23 android:name="android.permission.aad" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission
    android:name="android.permission.WRITE_APN_SETTINGS"
    tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

3.在application下加入:

<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="tencentXXXXXXXX" />
        <!--把XXXXXXXX换成老大给你的AppID,千万不要把前面几个字母tencent去掉-->
    </intent-filter>
</activity>
<activity
    android:name="com.tencent.connect.common.AssistActivity"
    android:configChanges="orientation|keyboardHidden"
    android:screenOrientation="behind"
    android:theme="@android:style/Theme.Translucent.NoTitleBar" />

 

4.看你的文档是需要什么参数登录,这里分两个情况登录,A:获取QQ返回的openid登录,B:获取用户资料登录

A:1.

String token;
String expires_in;
String uniqueCode;
//授权登录监听(最下面是返回结果)
private IUiListener loginListener = new IUiListener() {
    @Override
    public void onComplete(Object o) {
        uniqueCode = ((JSONObject) o).optString("openid"); //QQ的openid
        try {
            token = ((JSONObject) o).getString("access_token");
            expires_in = ((JSONObject) o).getString("expires_in");

            //在这里直接可以处理登录
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onError(UiError uiError) {
    }

    @Override
    public void onCancel() {
    }
};

A:2.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    mTencent.onActivityResultData(requestCode, resultCode, data, loginListener);
    if (requestCode == Constants.REQUEST_API) {
        if (resultCode == Constants.REQUEST_QQ_SHARE ||
                resultCode == Constants.REQUEST_QZONE_SHARE ||
                resultCode == Constants.REQUEST_OLD_SHARE) {
            mTencent.handleResultData(data, loginListener);
        }
    }
}

A3.

@OnClick({R.id.ic_qq, R.id.ic_wechat, R.id.ic_sina})
public void onViewClicked(View view) {
    switch (view.getId()) {
        case R.id.ic_qq:
            //QQ登录
            if (null == mTencent) {
                mTencent = Tencent.createInstance("老大给你的AppId,这里不用tencent,直接是那几个数字就行", this.getApplicationContext());
                if (!mTencent.isSessionValid()) {
                    mTencent.login(this, "all", loginListener);
                }
            }
            break;
        case R.id.ic_wechat:
            wxLogin();//微信登录
            break;
        case R.id.ic_sina:
            break;
    }
}

 

B:1.文档:

B:2

String token;
String expires_in;
String uniqueCode;
//授权登录监听(最下面是返回结果)
private IUiListener loginListener = new IUiListener() {
    @Override
    public void onComplete(Object o) {
        //获取openid和有效期(expires_in)
        uniqueCode = ((JSONObject) o).optString("openid"); //QQ的openid
        try {
            token = ((JSONObject) o).getString("access_token");
            expires_in = ((JSONObject) o).getString("expires_in");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        //获取QQ返回的用户信息(里面的参数直接照搬,别乱改,上面也是)
        QQToken qqtoken = mTencent.getQQToken();
        mTencent.setOpenId(uniqueCode);
        mTencent.setAccessToken(token, expires_in);
        UserInfo info = new UserInfo(getApplicationContext(), qqtoken);
        info.getUserInfo(new IUiListener() {
            @Override
            public void onComplete(Object o) {
                String nickname = ((JSONObject) o).optString("nickname");
                String sexStr = ((JSONObject) o).optString("sex");
                String headImg = ((JSONObject) o).optString("figureurl_qq_2");
                int sex = 0;
                switch (sexStr) {
                    case "男":
                        sex = 1;
                        break;
                }
                //QQ第三方登录(5个参数)
                loginByQQOpenId(nickname, sex, headImg, uniqueCode, 0);
            }

            @Override
            public void onError(UiError uiError) {
            }

            @Override
            public void onCancel() {
            }
        });
    }

    @Override
    public void onError(UiError uiError) {
    }

    @Override
    public void onCancel() {
    }
};

B:3

//使用QQ登录
private void loginByQQOpenId(String nick, int sex, String headImg, String uniqueCode, int userSource) {
    Retrofit retrofit = RetrofitManager.getInstance().getRetrofit();
    Call<User> getuser = retrofit.create(DataServer.class).getuser(nick, sex, headImg, uniqueCode, userSource);
    getuser.enqueue(new retrofit2.Callback<User>() {
        @Override
        public void onResponse(Call<User> call, Response<User> response) {
            if (response.body().getStatus() == 200) {
                //登录成功跳转到自己指定的Activity
                Intent it = new Intent(LoginActivity.this, StartActivity.class);
                startActivity(it);
            } else {
                //登录失败
                TipsUtil.toste(LoginActivity.this, response.body().getMessage());
            }
        }

        @Override
        public void onFailure(Call<User> call, Throwable t) {
            TipsUtil.getToast(LoginActivity.this, "网络异常");
        }
    });
}

B:4

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    mTencent.onActivityResultData(requestCode, resultCode, data, loginListener);
    if (requestCode == Constants.REQUEST_API) {
        if (resultCode == Constants.REQUEST_QQ_SHARE ||
                resultCode == Constants.REQUEST_QZONE_SHARE ||
                resultCode == Constants.REQUEST_OLD_SHARE) {
            mTencent.handleResultData(data, loginListener);
        }
    }
}

B:5.

@OnClick({R.id.ic_qq, R.id.ic_wechat, R.id.ic_sina})
public void onViewClicked(View view) {
    switch (view.getId()) {
        case R.id.ic_qq:
            //QQ登录
            if (null == mTencent) {
                      mTencent = Tencent.createInstance("101420843", this.getApplicationContext());
                if (!mTencent.isSessionValid()) {
                    mTencent.login(this, "all", loginListener);
                }
            }
            break;
        case R.id.ic_wechat:
            wxLogin();//微信登录  
            break;
        case R.id.ic_sina:
            break;
    }
}

 

这样基本就完事了,如果我上面的请求方式不理解可以往下看:

1.User是文档中的返回示例:

public class User implements Serializable{


    /**
     * message : 请求成功
     * status : 200
     * code : 0
     * token : hpvymDJdYxvaj2cX+vM/9Q==
     * currentTime : 1524896417273
     */

    private String message;
    private int status;
    private String code;
    private String token;
    private long currentTime;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public long getCurrentTime() {
        return currentTime;
    }

    public void setCurrentTime(long currentTime) {
        this.currentTime = currentTime;
    }

2.我这里是单例Retrofit:

public class RetrofitManager {
    //基地址
    public static final String BASE_URL = "文档中的基地址";//比如:http://103.41.185.29:8080

    //单例
    private static RetrofitManager instance;
    private Retrofit retrofit;

    private RetrofitManager() {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)//网络请求的地址
                .addConverterFactory(GsonConverterFactory.create())//数据解析器
                .build();
    }
    
    public static RetrofitManager getInstance() {
        if (instance == null) {
            instance = new RetrofitManager();
        }
        return instance;
    }

    public Retrofit getRetrofit() {
        return retrofit;
    }
    
}

3.公开一个方法接口:

public interface DataServer {

    //第三方登录-QQ
    @POST("/api/user/login_qq")
    @FormUrlEncoded
    Call<User> getuser(@Field("nickName") String nickName,
                       @Field("sex") int sex,
                       @Field("headImg") String headImg,
                       @Field("uniqueCode") String uniqueCode,
                       @Field("userSource") int userSource);


}
  

4.Retrofit和Gson的依赖:

implementation 'com.squareup.retrofit2:retrofit:2.7.1'
implementation 'com.squareup.retrofit2:converter-gson:2.6.0'
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/172829
推荐阅读
相关标签
  

闽ICP备14008679号