1

I need a little help.

I have a string array (urllinks) which I want to fill with url links which are being parsed with jsoup through a for loop.

In below code example there are 2 urls but the list only gets filled with the first link. I don`t know how many links will be parsed, can be 1 but also 12.

public static String[] urllinks;

...

for (int i = 0; i < links.size(); i++) {    // links size = 2  
    String url = doc.select("a").attr("abs:href");
    urllinks[i] = url;

}

Any help would be appreciated.

Thanks in advance.

2
  • String url will give you the same result everytime because you don't change anything. also, you should use ArrayList because you said that you don't know how many links will be parsed and array is for constant size of links. Commented Oct 24, 2016 at 17:12
  • what do you think this doc.select("a").attr("abs:href"); does, and why would you expect to successive calls to yield different results? Commented Oct 24, 2016 at 18:39

2 Answers 2

3

You problem is due to the fact that you call attr("abs:href") on doc.select("a") which returns an object of type Elements such that you always get the first match as stated in the javadoc:

Get an attribute value from the first matched element that has the attribute.

You should rather iterate as next:

List<String> urls = new ArrayList<>();
// Iterate over all the links that have an attribute abs:href
for (Element link : doc.select("a[abs:href]")) {
    urls.add(link.attr("abs:href"));
}
urllinks = urls.toArray(new String[urls.size()]);
Sign up to request clarification or add additional context in comments.

5 Comments

First of all thx Nicolas for the answer but I get a ArrayIndexOutOfBoundsException on urllinks[i]. (length=1; index=1
No problem, take your time ;)
Unfortunatly same Exception.
it means that urllinks doesn't have the right length, it should be 2 not 1
I provided another approach based on a list please check again
0

To answer my own question, I solved it by creating an arraylist and then convert it back to a string array. Dirty but it works.

{
  private static String[] urllinks;
  private static ArrayList<String> mStringList = new ArrayList<>();
  ...

  int i = 0;
  for (Element el : links) {
       String url = el.attr("abs:href");
       if (url != null) {
                mStringList.add(i, url);
                i++;
            } 
       else {
          // Do something
       }

  urllinks = mStringList.toArray(urllinks);
  // check if urls are actualy in string array
  Log.d("LINK 1", urllinks[0]);
  Log.d("LINK 2", urllinks[1]);
...
}

2 Comments

i is useless in this case, if you don't filter the links first as I did you will get empty String in case you have links without the attribute abs:href
It works like expected Nicolas, I check on empty urls also. Changed code.

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.