QQ登录

package com.example.qqlogin;


import org.json.JSONException;
import org.json.JSONObject;


import com.lidroid.xutils.BitmapUtils;
import com.tencent.connect.UserInfo;
import com.tencent.connect.auth.QQToken;
import com.tencent.connect.common.Constants;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity {


private TextView tv;
private Tencent tencent;
private ImageView iv;
Handler handler=new Handler(){


@Override
public void handleMessage(Message msg) {
if(msg.what==1){
JSONObject response = (JSONObject) msg.obj;
String str;
try {
str = response.getString("nickname");
String url=response.getString("figureurl_1");
Toast.makeText(MainActivity.this, url, 0).show();
Intent intent=new Intent(MainActivity.this,TwoActivity.class);
intent.putExtra("st", str);
intent.putExtra("url", url);
startActivity(intent);


} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

};


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tv = (TextView) findViewById(R.id.tv);
iv = (ImageView) findViewById(R.id.iv);
}
    public void login(View v){
    String appid="1105364936";
   
    tencent = Tencent.createInstance(appid, getApplicationContext());
    tencent.login(this, "all", new MyIUiListener());
   
    }
      public class MyIUiListener implements IUiListener{


@Override
public void onCancel() {
// TODO Auto-generated method stub

}


@Override
public void onComplete(Object object) {
try {
String token = ((JSONObject) object).getString(Constants.PARAM_ACCESS_TOKEN);
String expires = ((JSONObject) object).getString(Constants.PARAM_EXPIRES_IN);
String openId = ((JSONObject) object).getString(Constants.PARAM_OPEN_ID);
tencent.setAccessToken(token, expires);
tencent.setOpenId(openId);
QQToken token1=tencent.getQQToken();
UserInfo info=new UserInfo(MainActivity.this, token1);
info.getUserInfo(new IUiListener() {

@Override
public void onError(UiError arg0) {
// TODO Auto-generated method stub

}

@Override
public void onComplete(Object arg0) {
handler.obtainMessage(1, arg0).sendToTarget();


}

@Override
public void onCancel() {
// TODO Auto-generated method stub

}
});


} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}


@Override
public void onError(UiError arg0) {
Toast.makeText(MainActivity.this, "登录失败", 0).show();

}



      }
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  // TODO Auto-generated method stub
  if (requestCode == Constants.REQUEST_LOGIN) {
     Tencent.onActivityResultData(requestCode, resultCode, data, null);
     }


  super.onActivityResult(requestCode, resultCode, data);
  }

}

-------------------------------------登录成功

Intent intent=getIntent();
String str=intent.getStringExtra("st");
String url=intent.getStringExtra("url");
TextView tv=(TextView) findViewById(R.id.tv_two);
tv.setTextColor(Color.RED);
tv.setText(str);
wv = (WebView) findViewById(R.id.wv);
wv.setWebViewClient(new WebViewClient());


wv.loadUrl(url);

-----------------------权限

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />


      <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="tencent1105364936" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.tencent.connect.common.AssistActivity"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Translucent.NoTitleBar" />



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值