首先检查app包名、安装在手机上的签名和微信开放平台上面填写的包名、签名是否一致,如果不一致请改成一致。 public class MyApp extends Application { public static IWXAPI api;//此对象是专门用来向微信发送数据的一个重要接口,使用强引用持有,所有的信息发送都是基于这个对象的 @Override public void onCreate() { super.onCreate(); api = WXAPIFactory.createWXAPI(this, ShareKeys.WX_APPID, false); api.registerApp(ShareKeys.WX_APPID); } }
在微信登陆、分享、支付时统一用 MyApp.api.xxx();
比如
微信登陆
final SendAuth.Req req = new SendAuth.Req();
req.scope = "snsapi_userinfo";//
req.state = "mcl";
MyApp.api.sendReq(req);
微信分享
WXWebpageObject webpage=new WXWebpageObject();
webpage.webpageUrl="https://www.baidu.com/";
WXMediaMessage msg = new WXMediaMessage(webpage);
msg.title = "app名称";
msg.description = "mcl";
Bitmap thumb = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
msg . thumbData = bmpToByteArray (thumb, true) ;
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction("webpage");
req.message = msg;
req.scene = wechat;
MyApp.api.sendReq(req);
微信支付
MyApp.api.registerApp(weChatPayResult.getAppid());
PayReq request = new PayReq();
request.appId = weChatPayResult.getAppid();
request.partnerId = weChatPayResult.getPartnerid();
request.prepayId = weChatPayResult.getPrepayid();
request.packageValue = weChatPayResult.getPackageX();
request.nonceStr = weChatPayResult.getNoncestr();
request.timeStamp = String.valueOf(weChatPayResult.getTimestamp());
request.sign = weChatPayResult.getSign();
MyApp.api.sendReq(request);