場景
一個H5的頁面,需要在app內用webview展示,然後涉及到支付,要求能夠調用微信支付來付款,頁面沒有問題,但是寫完出現了一些問題。
問題
頁面不顯示,展示空白
需要設置:
webSettings.setDomStorageEnabled(true);
否則,一些節點不支持顯示,建議都加上這一句,並設置爲true。
點擊支付調不起微信
因爲,webview支持http和https的scheme,我們知道微信的schem是weixin://
,所以我們需要攔截這個url,並手動處理。
需要我們重寫:
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("weixin://wap/pay?") || url.startsWith("http://weixin/wap/pay")) {
try {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
} catch (ActivityNotFoundException e) {
// 處理錯誤
}
return true;
}
return true;
}
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, android.net.http.SslError error) {
//重寫此方法可以讓webview處理https請求
handler.proceed();
}
});
跳轉微信,報參數錯誤,聯繫商戶
這個在微信的文檔裏寫的很清楚:
if (url.startsWith("weixin://wap/pay?") || url.startsWith("http://weixin/wap/pay")) {
try {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
} catch (ActivityNotFoundException e) {
//處理錯誤
}
return true;
} else {
Map<String, String> extraHeaders = new HashMap<>();
extraHeaders.put("Referer", "商戶域名");
view.loadUrl(url, extraHeaders);
}
return true;
就是添加extraHeaders.put("Referer", "商戶域名");
這一句,key爲Referer,value爲你跳轉支付的商戶域名。
以上!