微信开放平台 : https://open.weixin.qq.com/
一、准备工作 :
1. Android Studio环境下:在build.gradle文件中,添加如下依赖即可:
dependencies {
implementation'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:+'
}
2. 在清单文件AndroidManifest.xml中加入权限:
二、接入 :
1. 注册微信:
想要使我们的程序启动之后,微信终端可以响应我们我们的程序的话.就必须在代码中像微信终端注册我们的id
(可以在程序的入口Activity的OnCreat中注册,也可以在MyApplication中,或者是在其他合适的地方将我们的应用id注册到微信),此处在Application中:
// 1.声明一个statid的IWXAPI 以及APPID
private static IWXAPI mWxApi;
private String WX_APPID = "";
// 2.初始化微信SDK
mWxApi = WXAPIFactory.createWXAPI(this, WX_APPID, true);
mWxApi.registerApp(WX_APPID);
2. 发起登录
if (BaseApplication.mWxApi!= null && BaseApplication.mWxApi.isWXAppInstalled()) {
final SendAuth.Req req = new SendAuth.Req();
req.scope = "snsapi_userinfo";
req.state = "wechat_sdk_fth";
BaseApplication.mWxApi.sendReq(req);
} else {
Toast.makeText(LoginActivity.this, "您还没有安装微信", Toast.LENGTH_SHORT).show();
}
3. 新建一个包 包名为wxapi,新建一个类名为WXEntryActivity的类,这个类名和包名是微信规定的,木有办法,必须一致了
然后在清单文件AndroidManifest.xml中声明该类:
4. WxEntryActivity类代码如下:
public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
private Bundle bundle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
BaseApplication. mIwapi.handleIntent(getIntent(), WXEntryActivity.this); //必须调用此句话
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
BaseApplication. mIwapi.handleIntent(intent, WXEntryActivity.this);//必须调用此句话
}
@Override
public void onReq(BaseReq req) {
System. out.println();
}
/**
* API:https://open.weixin.qq.com/ cgi- bin/showdocument ?action=dir_list&t=resource/res_list&verify=1&id=open1419317853 &lang=zh_CN
* 在此处得到Code之后调用https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code 获取到token和openID。
* 之后再调用https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID 获取用户个人信息
*/
@Override
public void onResp(BaseResp arg0) {
bundle=getIntent().getExtras();
SendAuth.Resp resp = new SendAuth.Resp( bundle);
//获取到code之后,需要调用接口获取到access_token
if (resp. errCode == BaseResp.ErrCode. ERR_OK) {
String code = resp. token;
if(BaseApplication. isWxLogin){
getToken(code);
} else{
WXEntryActivity. this.finish();
}
} else{
WXEntryActivity. this.finish();
}
}
//这个方法会取得accesstoken 和openID
private void getToken(String code){
MyProgressDialog. showDialog(WXEntryActivity.this, "登陆", "正在获取用户信息" );
HttpBase. get("https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxd7673d08f6c1963d&secret=223d726f966031f30125d0e4e7d4aed3&code=" +code+"&grant_type=authorization_code" , new JsonHttpResponseHandler(){
@Override
public void onSuccess( int statusCode, JSONObject response) {
super.onSuccess(statusCode, response);
getUserInfo(response.optString( "access_token"),response.optString( "openid"));
}
@Override
public void onFailure( int statusCode, Throwable e,
JSONObject errorResponse) {
// TODO Auto-generated method stub
super.onFailure(statusCode, e, errorResponse);
MyProgressDialog. closeDialog();
}
});
}
//获取到token和openID之后,调用此接口得到身份信息
private void getUserInfo(String token,String openID){
HttpBase. get("https://api.weixin.qq.com/sns/userinfo?access_token=" +token+"&openid=" +openID, new JsonHttpResponseHandler(){
@Override
public void onSuccess( int statusCode, JSONObject response) {
super.onSuccess(statusCode, response);
try {
String nickname = response.getString("nickname");//获取用户微信名
String sex = response.getInt("sex") == 1 ? "男" : "女";//获取用户性别
String header = response.getString( "headimgurl");//获取用户头像
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure( int statusCode, Throwable e,
JSONObject errorResponse) {
super.onFailure(statusCode, e, errorResponse);
MyProgressDialog. closeDialog();//隐藏dialog
HttpBase. onFailureTips(WXEntryActivity.this, errorResponse);
}
});
}
}
到这里就欧克了!