## APP支持H5微信支付案例
**对于微信支付,android端需用webview直接加载给定的url即可,代码如下:**
```java
public class MainActivity extends AppCompatActivity {
//h5的url链接(替换成正式的url)
private String url = "http://xxx.com/pay.html";
//定义支付域名(替换成卓训H5的域名即可)
private String realm = "https://webapi.zhuoxuncn.com";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = ((WebView) findViewById(R.id.webview));
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
webView.setWebViewClient(new MyWebviewClient());
}
/*** 重写WebViewClient */
private class MyWebviewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//微信H5支付核心代码
if (url.startsWith("weixin://wap/pay?")) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
return true;
} else {
Map<String, String> extraHeaders = new HashMap<>();
extraHeaders.put("Referer", realm);
view.loadUrl(url, extraHeaders);
}
return true;
}
//处理https请求
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, android.net.http.SslError error) {
handler.proceed();
} }}
网友评论