QQ第三方登录返回头像和昵称

来源:互联网 发布:mac pro桌面图标大小 编辑:程序博客网 时间:2024/06/11 20:07
public class MainActivity extends AppCompatActivity {    private static final String TAG = "MainActivity";    private static final String APP_ID = "1105602574";//官方获取的APPID    private Tencent mTencent;    private BaseUiListener mIUiListener;    private UserInfo mUserInfo;    private TextView name;    private ImageView img;    @Override        protected void onCreate(Bundle savedInstanceState) {            super.onCreate(savedInstanceState);            setContentView(R.layout.activity_main);            mTencent = Tencent.createInstance(APP_ID,MainActivity.this.getApplicationContext());            Button dian = (Button) findViewById(R.id.dian);        name = (TextView) findViewById(R.id.name);        img = (ImageView) findViewById(R.id.img);            dian.setOnClickListener(new View.OnClickListener() {                @Override                public void onClick(View v) {                    mIUiListener = new BaseUiListener();                    mTencent.login(MainActivity.this,"all", mIUiListener);                    mUserInfo = new UserInfo(MainActivity.this, mTencent.getQQToken());                    mUserInfo.getUserInfo(mIUiListener);                }            });        }    private class BaseUiListener implements IUiListener {        @Override        public void onComplete(Object response) {            Toast.makeText(MainActivity.this, "授权成功", Toast.LENGTH_SHORT).show();            Log.e(TAG, "response:" + response);            JSONObject obj = (JSONObject) response;            try {                String openID = obj.getString("openid");                String accessToken = obj.getString("access_token");                String expires = obj.getString("expires_in");                mTencent.setOpenId(openID);                mTencent.setAccessToken(accessToken, expires);                QQToken qqToken = mTencent.getQQToken();                mUserInfo = new UserInfo(getApplicationContext(), qqToken);                mUserInfo.getUserInfo(new IUiListener() {                    @Override                    public void onComplete(Object response) {                        Log.e(TAG, "登录成功" + response.toString());                        if(response == null){                            return;                        }                        try {                            JSONObject jo = (JSONObject) response;                            String name1 = jo.getString("nickname");//figureurl_1                            String im = jo.getString("figureurl_1");                            name.setText(name1);                            ImageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(MainActivity.this);                            ImageLoader.getInstance().init(configuration);                            ImageLoader.getInstance().displayImage(im,img);                        } catch (JSONException e) {                            e.printStackTrace();                        }                    }                    @Override                    public void onError(UiError uiError) {                        Log.e(TAG, "登录失败" + uiError.toString());                    }                    @Override                    public void onCancel() {                        Log.e(TAG, "登录取消");                    }                });            } catch (JSONException e) {                e.printStackTrace();            }        }        @Override        public void onError(UiError uiError) {            Toast.makeText(MainActivity.this, "授权失败", Toast.LENGTH_SHORT).show();        }        @Override        public void onCancel() {            Toast.makeText(MainActivity.this, "授权取消", Toast.LENGTH_SHORT).show();        }    }    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        if(requestCode == Constants.REQUEST_LOGIN){            Tencent.onActivityResultData(requestCode,resultCode,data,mIUiListener);        }        super.onActivityResult(requestCode, resultCode, data);    }}


--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

清单文件里的内容

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.bwei.czx.qq">    <uses-permission android:name="android.permission.INTERNET"/>    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>    <application android:allowBackup="true" android:icon="@mipmap/ic_launcher"        android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round"        android:supportsRtl="true" android:theme="@style/AppTheme">        <activity android:name=".MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </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="tencent1105602574" /> <!-- 开放平台获取的APPID -->            </intent-filter>        </activity>        <activity android:name="com.tencent.connect.common.AssistActivity"            android:theme="@android:style/Theme.Translucent.NoTitleBar"            android:screenOrientation="portrait"/>    </application></manifest>





原创粉丝点击