1

I am trying to load a newspaper URL using my webview. Here is my webview settings:

 binding.webView.settings
        .run {
            javaScriptEnabled = true
            builtInZoomControls = true
            displayZoomControls = false
            domStorageEnabled = true
            databaseEnabled = true
            loadWithOverviewMode = true
            useWideViewPort = true
        }

The url I am trying to load is : http://feeds.inquisitr.com/~r/google/yDYq/~3/1glI3navSEE/

but its not loading, however when I paste this url in browser the url get changed by the browser and it load perfectly. What else I need to do in my webview so that it will act like a browser and change(decode) the url.

p.s: when I load this url in browser the url becomes "https://www.inquisitr.com/6337911/big-brother-22-week-11-veto-winner/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+google%2FyDYq+%28The+Inquisitr+-+News%29"

What is the solution for this?

1 Answer 1

1

You are using the shortened URL in the above example. You need to get the final URL in order to load into the webView. You can get final URL by implementing the following method:

public static String getFinalURL(String url) throws IOException {
        HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
        con.setInstanceFollowRedirects(false);
        con.connect();
        con.getInputStream();

        if (con.getResponseCode() == HttpURLConnection.HTTP_MOVED_PERM || con.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP) {
            String redirectUrl = con.getHeaderField("Location");
            return getFinalURL(redirectUrl);
        }
        return url;
    }

NB: This method is recursive in-case there are multiple re-directions.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.