user3354605
user3354605

Reputation: 179

Stop loading a webpage on Url redirection android

I have a Webpage loading into my WebView. This webpage loads a login page for the website. As the user enters his credentials he gets redirected to a certain page. I want to stop the loading of the webpage after user enters his credentials and get redirected to another activity instead of the default redirected page.

My idea of doing this is to catch the redirected url and compare it with the url created by me, if both match it should stop loading the default webpage and load another activity. However, Iam able to catch the redirected Urls and show them in a AlertDialog, but cannot stop the loading of the default web page.

Can anyone help me on this?

This is what I have tried:-

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;

            }

EDIT:- Suggested by @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;

            }

Upvotes: 1

Views: 1422

Answers (1)

M D
M D

Reputation: 47807

Try to implement like:

  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;

        }

And showAlert2(url) method:

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

}

Try as per my answer and give me feedback on this

Upvotes: 1

Related Questions