繁体   English   中英

Android WebView 不加载自动重定向 url

[英]Android WebView does not load auto-redirect url

我有一个需要打开特定链接的网络视图。 当我点击该链接时,它会将我重定向到另一个链接......我的问题是我能够打开第一个链接但第二个链接没有被调用......请帮助我!

这是我的代码:

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);
    }

你应该使用

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
   view.loadUrl(url);
   return false;
}

请关注谷歌官方链接以获取更多信息。

根据此处使用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).

从 API 24 开始,不推荐使用shouldOverrideUrlLoading(Webview view, String url) ,应该改用shouldOverrideUrlLoading(Webview, WebResourceRequest)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM