简体   繁体   中英

Jsoup library with Android

hay everyone; I'm having problem displaying image url from java file (Connection), that use Jsoup library to extract the urls and assign them to ArrayList of type String.

public class Connection {

ArrayList<String> StringArray = new ArrayList<String>();
private String fileExtension = "jpg";
private String html = "http://mcs.une.edu.au/~salmutai/cameras";
private int count = 0;


public void UrlExtract() throws Exception {

    Document doc = Jsoup.connect(html).get();
    Elements castURL = doc.select("a");
    for (Element el : castURL) {
        String src = el.absUrl("href");
        String extension = src.substring(src.lastIndexOf(".") + 1,
                src.length());
        if (extension.equalsIgnoreCase(fileExtension)) {
            StringArray.add(src);
            count++;

        }
    }

}

public int getCount() throws Exception {
    return count;
}

public String ImgArray(int i) throws Exception {

    String[] StrArray = new String[count];
    StrArray = (String[]) StringArray.toArray(StrArray);
    return StrArray[i];
}

}

how can I use Toast to show exact url and the count of images. also, i'm using GridView to display all the images in order. the pervious code is working in Java Application, but it doesn't work in Android app

Regarding the problem as to why your parsing code doesn't work when running from android, I can only assume that the html retrieved might be different, but that is only a guess. You need to debug the code when running it from you android. A solution might be to output the retrieved html to a log file and see if there is a difference.

Regarding showing your message using Toast:

String text = "Found " + StringArray.size() + " images at the url: " + html;
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();

NOTE: you don't need to increment the count variable, you can just see how big the StringArray list is, by calling StringArray.size()

the main problem now is when I call this code in Activity 2:

try {
Connection co = new Connection();
co.UrlExtract();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

the app is crashing :(.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM