我正在加载webview中的网址,该网址会重定向到支付网关屏幕。我的网址是:
https://**hostname**/pgway/gateway/payment/paymentDebitwithPin.jsp?PaymentID=8501798561070830&trandata=b0cb7bcf4cb2f52b355fa4eb3f66905721323d2dfbbb00682dafe34bfca9948a&id=10100125
加载此网址时会重定向到url
https://**hostname**/pgway/gateway/payment/TransactionError.jsp
我用来加载此网址的代码是
pgWebview = (WebView)findViewById(R.id.pgWebview);
pgWebview.getSettings().setUseWideViewPort(true);
pgWebview.getSettings().setLoadWithOverviewMode(true);
pgWebview.setWebViewClient(
new SSLTolerentWebViewClient()
);
pgWebview.loadUrl(webURL);
重定向的屏幕显示消息为
Unauthorized Transaction. Your request cannot be process as the transaction is already initiated.
我也尝试过shouldOverrideUrlLoading方法,但无论我得到同样的错误。任何人都可以帮我解决这个问题吗?
答案 0 :(得分:0)
你需要为它启用javascript。在加载url之前添加这些行: -
pgWebview.getSettings().setJavaScriptEnabled(true);
pgWebview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
答案 1 :(得分:0)
通过这种方式检查你会在加载网址时知道发生了什么,网址重定向是否发生在移动浏览器中?
webView.loadUrl(actualURL);
public class AppWebViewClients extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d(TAG, "shouldOverrideUrlLoading");
Log.d(TAG, "actualURL: " + actualURL);
Log.d(TAG, "NewURL: " + url);
if (changedUrl && !url.equals(actualURL)) {
// page has been clicked
//TODO- load the page after redirection happens
view.loadUrl(url);
return true;
} else {
// Designate Urls that you want to load in WebView still.
view.loadUrl(url);
return true;
}
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
//SHOW LOADING IF IT ISNT ALREADY VISIBLE
Log.d(TAG, "onPageStarted");
}
public void onPageFinished(WebView view, String url) {
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.d(TAG, "Error Description: " + description);
Log.d(TAG, "failingUrl: " + failingUrl);
}
@Override
public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {
try{
final AlertDialog.Builder builder = new AlertDialog.Builder(WebViewActivity.this);
builder.setMessage(R.string.notification_error_ssl_cert_invalid);
builder.setPositiveButton("continue", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
handler.proceed();
}
});
builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
handler.cancel();
}
});
final AlertDialog dialog = builder.create();
dialog.show();
}catch (Exception e){
e.printStackTrace();
}
}
}
答案 2 :(得分:0)
更好的方法可能是检查 onPageFinished()
中的重定向网址<强>代码强>
@Override
public void onPageFinished(WebView view, String url) {
Log.d("Final URL", url);
}
答案 3 :(得分:0)
当您通过扩展 calss 在 webview 中设置 webclient 时,此解决方案对我有用:WebViewClient()
override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
Log.d(TAG, "shouldOverrideUrlLoading")
Log.d(TAG, "actualURL: $url")
Log.d(TAG, "NewURL: $url")
val parsedUrl = URL(url)
val hitTestResult = view.hitTestResult
//hitTestResult==null solve the redirect problem
if (!TextUtils.isEmpty(url) && hitTestResult == null) {
view.loadUrl(url)
return true
}
//your handling logic here
}