停止在Url重定向android上加载网页

时间:2014-03-11 04:44:26

标签: javascript android url redirect webview

我的网页已加载到WebView。此网页加载网站的登录页面。当用户输入他的凭证时,他被重定向到某个页面。我希望在用户输入其凭据并重定向到另一个活动而不是默认的重定向页面后停止加载网页。

我这样做的想法是捕获重定向的网址并将其与我创建的网址进行比较,如果两者都匹配则应停止加载默认网页并加载其他活动。但是,Iam能够捕获重定向的Url并在AlertDialog中显示它们,但无法停止加载默认网页。

有人可以帮我吗?

这就是我的尝试: -

myWebView.loadUrl(url);


        myWebView.setWebViewClient(new WebViewClient()
        {

            @Override
            public void onPageFinished(WebView view, String url) 
            {
                // TODO Auto-generated method stub
                super.onPageFinished(view, url);
                if(progressBar.isShowing())
                {
                    progressBar.dismiss();
                }
                String absoluteUrl = view.getUrl();
                absoluteUrl = Uri.decode(absoluteUrl);
                //int absoulteCount = absoluteUrl.length();

                String redirectedUrl = endpointHost+"Authorize/index"+myId;
                //int redirectedCount = redirectedUrl.length();


                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Details.this);
                alertDialogBuilder.setNegativeButton("Ok", new DialogInterface.OnClickListener() 
                {
                    public void onClick(DialogInterface dialog,int id) 
                    {
                        // if this button is clicked, just close
                        // the dialog box and do nothing
                        dialog.cancel();
                    }
                });
                AlertDialog alert = alertDialogBuilder.create();
                alert.setMessage(absoluteUrl);
                alert.show();

                if(absoluteUrl!=null && absoluteUrl.contains(redirectedUrl))
                {
                    view.stopLoading();
                    Intent myIntent = new Intent(Details.this, Home.class);
                    startActivity(myIntent);
                }


            }
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                // TODO Auto-generated method stub
                view.loadUrl(url);

                HttpClient httpClient = new DefaultHttpClient();
                URL myUrl;
                URLConnection connection;
                try
                {
                    myUrl = new URL(url);
                    connection = myUrl.openConnection();
                    connection.setConnectTimeout(3000);
                    connection.connect();

                    int size = connection.getContentLength();

                }
                catch (Exception e) {}

                String htmlContent = "";
                HttpPost httpGet = new HttpPost(url);
                HttpResponse response;
                HttpContext httpContext = new BasicHttpContext();
                try
                {
                    response = httpClient.execute(httpGet);


                    if(response.getStatusLine().getStatusCode() == 200)
                    {
                        HttpEntity entity = response.getEntity();
                        if (entity != null)
                        {
                            InputStream inputStream = entity.getContent();
                            htmlContent = convertToString(inputStream);


                        }


                    }

                }
                catch (Exception e) {}
                return true;

            }

编辑: - @SimplePlan建议

myWebView.setWebViewClient(new WebViewClient()
        {

            @Override
            public void onPageFinished(WebView view, String url) 
            {
                // TODO Auto-generated method stub
                super.onPageFinished(view, url);
                if(progressBar.isShowing())
                {
                    progressBar.dismiss();
                }


            }
            private void showAlert2(String Url) 
            {
                // TODO Auto-generated method stub
                Url = Uri.decode(Url);
                //int absoulteCount = absoluteUrl.length();

                 String redirectedUrl = endpointHost+"/AuthorizeDevice/index"+deviceId;

                //int redirectedCount = redirectedUrl.length();


                if(Url!=null && Url.contains("redirect_uri"+redirectedUrl+"?{StandardTokens}"))
                {
                    myWebView.stopLoading();
                    Intent myIntent = new Intent(Details.this, Home.class);
                    startActivity(myIntent);
                }else{

                     AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Details.this);
                     alertDialogBuilder.setNegativeButton("Ok", new DialogInterface.OnClickListener() 
                     {
                         public void onClick(DialogInterface dialog,int id) 
                         {
                             // if this button is clicked, just close
                             // the dialog box and do nothing
                             dialog.cancel();
                         }
                     });
                     AlertDialog alert = alertDialogBuilder.create();
                     alert.setMessage(Url);
                     alert.show();
                }


            }
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                // TODO Auto-generated method stub
                view.loadUrl(url);
                showAlert2(url);

                return true;

            }

1 个答案:

答案 0 :(得分:1)

尝试实现:

  myWebView.setWebViewClient(new WebViewClient()
    {

        @Override
        public void onPageFinished(WebView view, String url) 
        {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);
            if(progressBar.isShowing())
            {
                progressBar.dismiss();
            }
            showAlert2(url);

        }
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            // TODO Auto-generated method stub
            view.loadUrl(url);

            HttpClient httpClient = new DefaultHttpClient();
            URL myUrl;
            URLConnection connection;
            try
            {
                myUrl = new URL(url);
                connection = myUrl.openConnection();
                connection.setConnectTimeout(3000);
                connection.connect();

                int size = connection.getContentLength();

            }
            catch (Exception e) {}

            String htmlContent = "";
            HttpPost httpGet = new HttpPost(url);
            HttpResponse response;
            HttpContext httpContext = new BasicHttpContext();
            try
            {
                response = httpClient.execute(httpGet);


                if(response.getStatusLine().getStatusCode() == 200)
                {
                    HttpEntity entity = response.getEntity();
                    if (entity != null)
                    {
                        InputStream inputStream = entity.getContent();
                        htmlContent = convertToString(inputStream);


                    }


                }

            }
            catch (Exception e) {}
            return true;

        }

showAlert2(url)方法:

    public void showAlert2(String Url) {

//String absoluteUrl = view.getUrl();
    Url = Uri.decode(Url);
    //int absoulteCount = absoluteUrl.length();

     String redirectedUrl = endpointHost+"Authorize/index"+myId;

    //int redirectedCount = redirectedUrl.length();


    if(Url!=null && Url.contains(redirectedUrl))
    {
        myWebView.stopLoading();
        Intent myIntent = new Intent(Details.this, Home.class);
        startActivity(myIntent);
    }else{

         AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Details.this);
         alertDialogBuilder.setNegativeButton("Ok", new DialogInterface.OnClickListener() 
         {
             public void onClick(DialogInterface dialog,int id) 
             {
                 // if this button is clicked, just close
                 // the dialog box and do nothing
                 dialog.cancel();
             }
         });
         AlertDialog alert = alertDialogBuilder.create();
         alert.setMessage(Url);
         alert.show();
    }

}

根据我的回答尝试并给我反馈