0

我有一個webview需要打開一個特定的鏈接。當我點擊該鏈接時,它會將我重定向到另一個鏈接......我的問題是我可以打開第一個鏈接,但第二個鏈接不會被調用...請幫助我!Android WebView不加載自動重定向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); 
    } 
+0

MyMasterPiece的答案爲我做了訣竅http://stackoverflow.com/questions/18377769/webview-not-able-to-load-https-url-in-android –

+0

可能重複的[Webview無法加載https網址在Android?](http://stackoverflow.com/questions/18377769/webview-not-able-to-load-https-url-in-android) – deblocker

回答

1

您應該使用

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

請按照google official link獲取更多信息。