[英]WebViewClient will not open page handled in shouldOverrideUrlLoading()
I'm using webview to show transaction process (payment by creditCard). 我正在使用Webview显示交易过程(通过信用卡付款)。 If I use webView.load(given URL) it is working and it will redirect me to bank page in my browser from my app. 如果我使用webView.load(给定的URL),它将正常工作,它将把我从我的应用重定向到浏览器中的银行页面。
But I need to track if the payment is successful (it will redirect me to specific URL) and sent data back to the server. 但是我需要跟踪付款是否成功(它将把我重定向到特定的URL)并将数据发送回服务器。
I used webViewClient, but as I use the client, it will not redirect me to a browser page, it will stay at "processing data" screen inside the app for eternity. 我使用了webViewClient,但是当我使用客户端时,它不会将我重定向到浏览器页面,它将一直停留在应用程序内的“处理数据”屏幕上。 Even if I add only view.loadurl(URL) and return true inside shouldOverrideUrlLoading, it will do the same thing. 即使我只添加view.loadurl(URL)并在shouldOverrideUrlLoading内部返回true,它也会做同样的事情。 There are 4-5 redirects till the final result. 有4-5次重定向,直到最终结果。 StartURL(add creditCardInfo) - Redirect to validation - redirect to payment confirmation - redirect to specific URL which returns data(if branch) StartURL(add creditCardInfo)-重定向到验证-重定向到付款确认-重定向到返回数据的特定URL(如果分支)
class PurchaseWebView: AppCompatActivity() {
private lateinit var purchaseWebView: WebView
private val api: API = API.getInstance(this)
private val webViewActivity = this
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.purchase_web_view_layout)
purchaseWebView = findViewById(R.id.webview)
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
purchaseWebView.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
createLog("WebView Loading url... ", url)
if (url.startsWith("purchase.complete.url.com", 0)){
api.purchaseCreditCardResult(url, webViewActivity, object: IPurchaseCallback{
override fun onError(errorJSON: JSONObject) {
createLog("WebView Error ", errorJSON.toString())
}
override fun onSuccess(purchaseJSON: JSONObject?) {
createLog("WebView Success ", "Finishing Activity")
}
})
return false
} else {
view.loadUrl(url)
return true
}
}
}
@RequiresApi(Build.VERSION_CODES.N)
purchaseWebView.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean {
if (request.url.toString().startsWith("purchase.complete.url.com", 0)){
api.purchaseCreditCardResult(request.url.toString(), webViewActivity, object: IPurchaseCallback{
override fun onError(errorJSON: JSONObject) {
createLog("WebView Error ", errorJSON.toString())
}
override fun onSuccess(purchaseJSON: JSONObject?) {
createLog("WebView Success ", "Finishing Activity")
}
})
return false
} else {
view.loadUrl(request.url.toString())
return true
}
}
}
purchaseWebView.loadUrl(intent.extras.getString("purchaseURL"))
}
I think that the problem is in this particular call view.loadUrl(request.url.toString())
When you're overriding shouldOverrideUrlLoading
you should return only true
or false
, which means - allow or forbid loading of passed url. 我认为问题出在这个特定的调用view.loadUrl(request.url.toString())
当您覆盖shouldOverrideUrlLoading
您应该只返回true
或false
,这意味着-允许或禁止加载传递的url。 But when you're returning true
and also calls loadUrl
you're trying to load same url twice. 但是,当您返回true
并且还调用loadUrl
您尝试两次加载相同的url。 Remove the loadUrl
command and just return true
. 删除loadUrl
命令,然后返回true
。
override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
view.loadUrl(request.url.toString()) // Why?
return true
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.