I have a webview which needs to open a particular link. When I hit that link, it redirects me to another link... My problem is that I am able to open the first link but the second link doesn't get called... Please help me!
Here is my code:
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);
}
You should use
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
Please follow google official link for more info.
According to the documentation here for using 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).
Using shouldOverrideUrlLoading(Webview view, String url) is deprecated as of API 24 and should use shouldOverrideUrlLoading(Webview, WebResourceRequest) instead
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With