Android WebView 不加载自动重定向 url
Posted
技术标签:
【中文标题】Android WebView 不加载自动重定向 url【英文标题】:Android WebView does not load auto-redirect url 【发布时间】:2017-06-27 01:46:34 【问题描述】:我有一个需要打开特定链接的 web 视图。当我点击该链接时,它会将我重定向到另一个链接...我的问题是我能够打开第一个链接,但第二个链接没有被调用...请帮助我!
这是我的代码:
public class OnlinePayment extends AppCompatActivity
WebView online_payment_activity_web_view;
String url;
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.online_payment_activity);
Bundle extras = getIntent().getExtras();
url = extras.getString("redirect_url");
final ProgressDialog pd = ProgressDialog.show(OnlinePayment.this, "", "Redirecting...", true);
Log.e("Here redirect first", url);
online_payment_activity_web_view=(WebView) findViewById(R.id.online_payment_web_view);
online_payment_activity_web_view.getSettings().setjavascriptEnabled(true);
online_payment_activity_web_view.getSettings().setSupportZoom(true);
online_payment_activity_web_view.getSettings().setBuiltInZoomControls(true);
online_payment_activity_web_view.getSettings().setDisplayZoomControls(false);
online_payment_activity_web_view.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
online_payment_activity_web_view.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
online_payment_activity_web_view.setWebChromeClient(new WebChromeClient());
online_payment_activity_web_view.getSettings().setLoadWithOverviewMode(true);
online_payment_activity_web_view.getSettings().setUseWideViewPort(true);
online_payment_activity_web_view.setWebViewClient(new WebViewClient()
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
return false;
/**
* Notify the host application that an SSL error occurred while loading a
* resource. The host application must call either handler.cancel() or
* handler.proceed(). Note that the decision may be retained for use in
* response to future SSL errors. The default behavior is to cancel the
* load.
*
* @param view The WebView that is initiating the callback.
* @param handler An SslErrorHandler object that will handle the user's
* response.
* @param error The SSL error object.
*/
@Override
public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error)
//final AlertDialog.Builder builder = new AlertDialog.Builder(OnlinePayment.this);
String msg="";
if(error.getPrimaryError()==SslError.SSL_DATE_INVALID
|| error.getPrimaryError()== SslError.SSL_EXPIRED
|| error.getPrimaryError()== SslError.SSL_IDMISMATCH
|| error.getPrimaryError()== SslError.SSL_INVALID
|| error.getPrimaryError()== SslError.SSL_NOTYETVALID
|| error.getPrimaryError()==SslError.SSL_UNTRUSTED)
if(error.getPrimaryError()==SslError.SSL_DATE_INVALID)
msg="The date of the certificate is invalid";
else if(error.getPrimaryError()==SslError.SSL_INVALID)
msg="A generic error occurred";
else if(error.getPrimaryError()== SslError.SSL_EXPIRED)
msg="The certificate has expired";
else if(error.getPrimaryError()== SslError.SSL_IDMISMATCH)
msg="Hostname mismatch";
else if(error.getPrimaryError()== SslError.SSL_NOTYETVALID)
msg="The certificate is not yet valid";
else if(error.getPrimaryError()==SslError.SSL_UNTRUSTED)
msg="The certificate authority is not trusted";
final AlertDialog.Builder builder = new AlertDialog.Builder(OnlinePayment.this);
builder.setMessage(msg);
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();
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
Log.e("---URL onPageStarted---", url);
pd.show();
String [] restructured_url = url.split("\\?");
Log.d("URL NEW----", restructured_url[0]);
if(restructured_url[0].equalsIgnoreCase(Utils.MAIN_URL+"/cgp_dashboard"))
Utils.dashBoardRefresh = true;
pd.hide(); StoreSharePreference.SSP().putBoolean StoreSharePreference.SSP().putBoolean("payment_processed", true);
Log.e("INSIDE SUCCESS", "------");
StoreSharePreference.SSP().putBoolean("isCGPCustomer", true);
StoreSharePreference.SSP().putString("cgp_customer", "yes");
Intent intent = new Intent(OnlinePayment.this, CherishMain.class);
intent.putExtra("fromPayment", true);
intent.putExtra("paymentSucess", true);
intent.setFlags(intent.FLAG_ACTIVITY_CLEAR_TOP |intent.FLAG_ACTIVITY_NEW_TASK | intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
// Move to dashboard
else if(restructured_url[0].equalsIgnoreCase(Utils.MAIN_URL+"/customermaster/paymentfail"))
StoreSharePreference.SSP().putBoolean("payment_processed", false);
Log.e("OUTSIDE SUCCESS", "------");
if (StoreSharePreference.SSP().getBoolean("isCGPCustomer", false) == true || StoreSharePreference.SSP().getString("cgp_customer").equals("yes"))
Intent intent = new Intent(OnlinePayment.this, CherishMain.class);
intent.putExtra("fromPayment", true);
intent.putExtra("paymentSucess", false);
intent.setFlags(intent.FLAG_ACTIVITY_CLEAR_TOP |intent.FLAG_ACTIVITY_NEW_TASK | intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
// Move to dashboard
else
Toast.makeText(getBaseContext(), "Payment failed", Toast.LENGTH_LONG).show();
onBackPressed();
@Override
public void onPageFinished(WebView view, String url)
super.onPageFinished(view,url);
Log.e("---URL ORIGINAL---", url);
String javascript="javascript:document.getElementsByName('viewport')[0].setAttribute('content', 'initial-scale=1.0,maximum-scale=10.0');";
view.loadUrl(javascript);
if(pd.isShowing())
pd.hide();
);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
online_payment_activity_web_view.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW );
online_payment_activity_web_view.loadUrl(url);
【问题讨论】:
MyMasterPiece 的回答对我有用 ***.com/questions/18377769/… Webview not able to load https url in android?的可能重复 【参考方案1】:你应该使用
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
view.loadUrl(url);
return false;
请关注google official link了解更多信息。
【讨论】:
【参考方案2】:根据文档here 使用shouldOverrideUrlLoading
:
Do not call WebView#loadUrl(String) with the requests URL and then return true.
This unnecessarily cancels the current load and starts a new load with the same URL.
The correct way to continue loading a given URL is to simply return false, without calling WebView#loadUrl(String).
使用 shouldOverrideUrlLoading(Webview view, String url)
从 API 24 开始已弃用,应改为使用 shouldOverrideUrlLoading(Webview, WebResourceRequest)
【讨论】:
以上是关于Android WebView 不加载自动重定向 url的主要内容,如果未能解决你的问题,请参考以下文章