微信開放平臺>微信登陸、分享、支付。調不起來,呼叫失敗
阿新 • • 發佈:2018-12-29
首先檢查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);