1

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;
}
Zain
  • 37,492
  • 7
  • 60
  • 84
Meggan Sam
  • 319
  • 1
  • 4
  • 17
  • 1
    I think you have to use it in a for loop and iterate over your array. – Jakob Oct 28 '21 at 20:49
  • 1
    Don't those links referenced from anchor tags? – Zain Oct 28 '21 at 20:52
  • @Jakobヤコブ could you please explain how? Sorry if I'm asking for too much – Meggan Sam Oct 28 '21 at 20:54
  • 1
    You are already iterating over the links when you append them to the string. Why don't you put the logic to add https there? Also, why do you append them all to a string with newlines, and later split the string on newlines? Why not just collect them in a list? – David Conrad Oct 28 '21 at 20:57
  • 1
    @MegganSam I don't know the exact output, but it looks like your *links_array* contains all your links. So you could use a for loop to iterate over your array and apply your function to every element. – Jakob Oct 28 '21 at 20:58
  • @DavidConrad interesting input, I'm still learning and coding from online tutorials so I'm not sure how I would go by your idea. – Meggan Sam Oct 28 '21 at 20:59
  • 1
    Your code is working only on the first link because you perform the prepend before you split the list of links into an array with `.split("\n")`. If you want the prepend to apply to all links, you need to perform the operation after the split, iterating through the whole list. – Tustin2121 Oct 28 '21 at 21:01
  • In the code where you have `for (Element link : links)`, just store the result of `link.attr("player-data")` in a String variable and apply the logic to add `https:` to it if it starts with `//` before you append it to `jsoupStr`. – David Conrad Oct 28 '21 at 21:30

2 Answers2

2

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];
Jakob
  • 1,858
  • 2
  • 15
  • 26
1

If this code working just for first missing link:

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

I believe you can use loop for control every link.

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
batuhan
  • 13
  • 2