Skip to content
Advertisement

Add https to missing strings of an array?

I’m writing an app for a client who doesn’t have an official API but wants the app to extract video links from his website so I wrote a logic using jsoup. Everything seems to work fine except some of the links don’t start with https so I’m trying to add it before the URL.

Here’s my code:

          new Thread(() -> {
                    final StringBuilder jsoupStr = new StringBuilder();
                    String URL = "https://example.com" +titleString
                            .replaceAll(":", "")
                            .replaceAll(",", "")
                            .replaceAll(" ", "-")
                            .toLowerCase();

                    Log.d("CALLING_URL", " " +URL);

                    try {
                        Document doc = Jsoup.connect(URL).get();
                        Element content = doc.getElementById("list-eps");
                        Elements links = content.getElementsByTag("a");
                        for (Element link : links) {
                            jsoupStr.append("n").append(link.attr("player-data"));
                        }
                    } catch (IOException e) {
                        e.getMessage();
                    }

                    String linksStr = jsoupStr.toString().trim();
                    if (!linksStr.startsWith("https://")) {
                        linksStr = "https:" + linksStr;
                    }
                    String[] links_array = linksStr.split("n");
                    arrayList.addAll(Arrays.asList(links_array));

                }).start();

The website contains about 10 links per video but some links start like “//” instead of https.

This code adds the https but only for the first link it finds missing.

if (!linksStr.startsWith("https://")) {
    linksStr = "https:" + linksStr;
}

Advertisement

Answer

You need to iterate over your final array to apply your function to all links.

String[] links_array = linksStr.split("n");
for(int i = 0; i < length; i++)
    if(!links_array[i].startsWith("https://"))
        links_array[i] = "https:" + links_array[i];
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement