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

标签 javascript android url redirect webview

我有一个网页加载到我的 WebView 中。该网页加载网站的登录页面。当用户输入他的凭据时,他会被重定向到某个页面。我想在用户输入他的凭据并重定向到另一个 Activity 而不是默认的重定向页面后停止加载网页。

我这样做的想法是捕获重定向的 url 并将其与我创建的 url 进行比较,如果两者匹配,它应该停止加载默认网页并加载另一个 Activity 。但是,我能够捕获重定向的 Urls 并将它们显示在 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;

            }

最佳答案

尝试像这样实现:

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

}

按照我的回答尝试并给我反馈

关于javascript - 停止在 Url 重定向 android 上加载网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22316655/

相关文章:

javascript - Phonegap - 使用 window.location.replace 离开索引范围后处理推送通知

android - 比较两个数组列表元素并获取不常见的元素

java - startActivity()期间的"Application was stopped"

python - 如何在 Python 中使用请求处理 <TooManyRedirects : Exceeded 30 redirects.> 异常?

wordpress - 如何在 WordPress 中仅关闭 *标签* 的永久链接?

javascript - 当 span 中的文本调整大小以填充整个 div 时,垂直对齐 div 内的 span

javascript - 下拉菜单中的 Bootstrap 4 选项卡

javascript - 如何将javascript html和css打包到一个js文件中

android - sendMessage 无法正常工作(可穿戴)

node.js - URL 中的 pump.io 端口