【问题标题】:Stop loading a webpage on Url redirection android停止在 URL 重定向 android 上加载网页
【发布时间】:2014-04-14 12:42:28
【问题描述】:

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

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

            }

【问题讨论】:

  • 我不明白这里的一件事。你为什么在onPageFinished(...) 中检查URL
  • @SimplePlan 我必须在用户登录后停止加载..关于我该如何继续的任何建议?
  • 登录后还是登录之间?
  • @SimplePlan 登录后。用户被重定向到演示网页。我想加载另一个活动而不是那个页面
  • 等待2分钟我会提供解决方案

标签: javascript android url redirect webview


【解决方案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();
    }

}

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

【讨论】:

  • 它的工作方式与我的代码相同。仍然加载默认网页
  • @user3354605 一个问题,你为什么要将httpClient 实施到shouldOverrideUrlLoading(.......) 中?
  • 我必须对用户进行身份验证
  • 即使我删除了 HttpClient 代码,它仍然给我相同的结果。我只是担心停止加载演示页面
  • @user3354605如果真的想这样做然后将我的showAlert2(url)方法添加到shouldOverrideUrlLoading(.......)中并尝试。
猜你喜欢
  • 2013-08-16
  • 1970-01-01
  • 2021-09-17
  • 2021-11-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多