写在前面:只调用微信分享一个接口就仿佛陷入了无底洞,在错误的泥潭里越陷越深,最开(dan)心(teng)的是网上文章各有各的套路,再就是错误问题有些解释的不太明了,当然这锅大部分得要微信自己来背,排版无力吐槽而且漏洞多还不太稳定。
配置和接入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| private static final String APP_ID = "wx888888888"; private IWXAPI api;
private String type = "webpage"; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_web); shareTOWeixin(); } public void shareTOWeixin() {
api = WXAPIFactory.createWXAPI(getApplicationContext(), APP_ID, false); api.registerApp(APP_ID); WXWebpageObject webpage = new WXWebpageObject(); webpage.webpageUrl = url; WXMediaMessage msg = new WXMediaMessage(webpage); msg.title = title; msg.description = intro; Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 150, 150, true); bmp.recycle(); msg.setThumbImage(bmp); SendMessageToWX.Req req = new SendMessageToWX.Req(); req.transaction = type + System.currentTimeMillis(); req.message = msg; req.scene = SendMessageToWX.Req.WXSceneTimeline; api.sendReq(req); }
|
跳坑(带泪的亲历)
- 一定要在你的包名根目录下创建wxapi包,然后在该包下创建activity命名为【WXEntryActivity】(写死不可更改)并实现IWXAPIEventHandler接口,不然怎么挂的都不知道
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| public class WXEntryActivity extends AppCompatActivity implements IWXAPIEventHandler { private static final String APP_ID = "wxf054659decf8f748"; private IWXAPI api; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); api = WXAPIFactory.createWXAPI(this, APP_ID,false); api.registerApp(APP_ID); api.handleIntent(getIntent(),this); } @Override public void onReq(BaseReq baseReq) { } @Override public void onResp(BaseResp baseResp) { String result = null; switch (baseResp.errCode){ case BaseResp.ErrCode.ERR_OK: result = "发送成功"; Toast.makeText(this,result,Toast.LENGTH_SHORT).show(); finish(); break; case BaseResp.ErrCode.ERR_USER_CANCEL: result = "发送失败"; Toast.makeText(this,result,Toast.LENGTH_SHORT).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) { super.onNewIntent(intent); setIntent(intent); api.handleIntent(intent, this); finish(); }
}
|
分享未成功
- 调试(分享)功能时要打【app-release.apk】,并且要对照本地签名和微信服务器端签名是否一致
使用GenSignature获取release包的签名,并和服务器端的应用签名核对是否一致,若不一致,修改服务器端应用签名和本地签名一致,大概需要半天时间同步。
- 若返回结果的为”发送返回”,极大可能是因为修改了appkey(签名)和储存在sharepreference中的access token不匹配,所以被分享平台拒绝。解决办法就是清空微信缓存或者重装微信应用,再或者换一部手机调试都是可行的。