1.把mob或者友盟demo的全部文件jar放到相应的位置
2.项目根目录建立wxapi,写WXEntryActivity
3.如果登录失败:(1)检查微信开放平台的appid,appcecret
(2)检查包名跟签名,根据微信的签名工具
(3)记得打包签名
/**
* 微信客户端回调activity示例 */ public class WXEntryActivity extends Activity implements IWXAPIEventHandler, Callback.CommonCallback<String> { private String weixinCode = ""; private final static String weixinAPP_ID = "wx486657116f44a985"; private final static String weixinAppSecret = "3534c52f4451fbdee5bcb175e1d3f4b6"; String Openid = null; public static IWXAPI weixinApi; private String asstoken; private String openId; private Context mContext; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); mContext = this; weixinApi = WXAPIFactory.createWXAPI(this, weixinAPP_ID); weixinApi.handleIntent(getIntent(), this); } // 微信发送请求到第三方应用时,会回调到该方法 @Override public void onReq(BaseReq req) { // TODO Auto-generated method stub } // 第三方应用发送到微信的请求处理后的响应结果,会回调到该方法 @Override public void onResp(BaseResp resp) { // TODO Auto-generated method stub String result = ""; switch (resp.errCode) { case BaseResp.ErrCode.ERR_OK: result = "发送成功"; SendAuth.Resp sendResp = (SendAuth.Resp) resp; String wxCode = sendResp.code; weixinCode = sendResp.code; //根据code获取accesstoken String accessTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + weixinAPP_ID + "&secret=" + weixinAppSecret + "&code=" + weixinCode + "&grant_type=authorization_code"; x.http().get(new RequestParams(accessTokenUrl), this); break; case BaseResp.ErrCode.ERR_USER_CANCEL: result = "登录取消"; Toast.makeText(this, result, Toast.LENGTH_LONG).show(); finish(); break; case BaseResp.ErrCode.ERR_AUTH_DENIED: result = "登录被拒绝"; Toast.makeText(this, result, Toast.LENGTH_LONG).show(); finish(); break; default: result = "登录失败"; Toast.makeText(this, result, Toast.LENGTH_LONG).show(); finish(); break; } } @Override protected void onNewIntent(Intent intent) { // TODO Auto-generated method stub super.onNewIntent(intent); setIntent(intent); weixinApi.handleIntent(intent, this); } @Override /** * 获取微信用户的数据 */ public void onSuccess(String result) { if (result!=null) { com.alibaba.fastjson.JSONObject tokenObj = JSON .parseObject(result); String accessToken = tokenObj.getString("access_token"); Log.d("Tag", accessToken); final String openId = tokenObj.getString("openid"); // 检验授权凭证(access_token)是否有效 //根据token获取id String checkTocken = "https://api.weixin.qq.com/sns/auth?access_token=" + accessToken + "&openid=" + openId; // 获取用户个人信息
//根据openid获取个人信息
String userUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=" + accessToken + "&openid=" + openId; x.http().get(new RequestParams(userUrl), new CommonCallback<String>() { @Override public void onSuccess(String result) { com.alibaba.fastjson.JSONObject userObj = JSON .parseObject(result); String userOpenid = userObj.getString("openid"); Log.d("Tag", openId); Openid = userObj.getString("openid"); String userNickname = userObj.getString("nickname"); Log.d("Tag", userNickname); String userHeadImage = userObj.getString("headimgurl"); Log.d("Tag", userHeadImage);
//调用后台接口,登录完毕
} @Override public void onError(Throwable ex, boolean isOnCallback) { } @Override public void onCancelled(CancelledException cex) { } @Override public void onFinished() { } }); } } public void onError(Throwable ex, boolean isOnCallback) { } public void onCancelled(CancelledException cex) { } public void onFinished() { } }