Home > Back-end >  Add https to missing strings of an array?
Add https to missing strings of an array?

Time:10-29

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

CodePudding user response:

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];

CodePudding user response:

if this code working just for first missing link :

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

}

i think you can use loop for control every link.

  • Related