问题

image.png
商家参数格式有误,请联系商家解决

微信文档描述

https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=15_4
image.png

解决方法

文档说明白了,要在请求头上加上Refrence,
注意并不是在加载webview时加上请求头,
image.png
而是在调用微信支付的时候加上这个请求头

  1. override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
  2. return try {
  3. if (url!!.startsWith("http:") || url.startsWith("https:")) {
  4. val wvHead: MutableMap<String, String> = HashMap()
  5. wvHead["Referer"] = Referer
  6. view!!.loadUrl(url, wvHead)
  7. } else {
  8. val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
  9. startActivity(intent)
  10. }
  11. true
  12. } catch (e: Exception) {
  13. false
  14. }
  15. }

完整程序

  1. package com.wuzhao.ktt.wvtaste
  2. import android.annotation.SuppressLint
  3. import android.content.Intent
  4. import android.net.Uri
  5. import android.os.Bundle
  6. import android.util.Log
  7. import android.view.KeyEvent
  8. import android.webkit.WebView
  9. import android.webkit.WebViewClient
  10. import androidx.appcompat.app.AlertDialog
  11. import androidx.appcompat.app.AppCompatActivity
  12. import com.wuzhao.ktt.R
  13. import kotlin.system.exitProcess
  14. class WebViewTaste : AppCompatActivity() {
  15. private lateinit var wv: WebView
  16. companion object {
  17. // 定义WebView首页地址[伴生对象]
  18. const val WEB_URL = "你的webview url"
  19. const val Referer = "申请的地址"
  20. }
  21. override fun onCreate(savedInstanceState: Bundle?) {
  22. super.onCreate(savedInstanceState)
  23. setContentView(R.layout.activity_web_view_taste)
  24. initWv()
  25. }
  26. @SuppressLint("SetJavaScriptEnabled")
  27. fun initWv() {
  28. wv = findViewById<WebView>(R.id.wv)
  29. wv.settings.javaScriptEnabled = true
  30. wv.webViewClient = webClient
  31. wv.loadUrl(WEB_URL)
  32. }
  33. private val webClient = object : WebViewClient() {
  34. override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
  35. return try {
  36. if (url!!.startsWith("http:") || url.startsWith("https:")) {
  37. val wvHead: MutableMap<String, String> = HashMap()
  38. wvHead["Referer"] = Referer
  39. view!!.loadUrl(url, wvHead)
  40. } else {
  41. val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
  42. startActivity(intent)
  43. }
  44. true
  45. } catch (e: Exception) {
  46. false
  47. }
  48. }
  49. }
  50. override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
  51. return if ((keyCode == KeyEvent.KEYCODE_BACK)) {
  52. if (wv.canGoBack()) {
  53. wv.goBack()//返回上一页面
  54. true
  55. } else {
  56. AlertDialog.Builder(this@WebViewTaste)
  57. .setTitle("退出") //设置对话框标题
  58. .setMessage("确定要退出?") //设置显示的内容
  59. .setPositiveButton(
  60. "确定"
  61. ) { _, _ ->
  62. exitProcess(0)//退出程序
  63. }.setNegativeButton(
  64. "取消"
  65. ) { _, _ ->
  66. }.show()
  67. false
  68. }
  69. } else {
  70. super.onKeyDown(keyCode, event)
  71. }
  72. }
  73. }