Search code examples
javahttpurljsoup

JSoup cant connect to URL


I have a URL which is a http server that contains links to images and I want to take those links and put them in a list of strings... However Jsoup cant connect, after some reseach I found out i Need to set up a user agent to search it and stuff....

Jsoup.connect cannot get correct html contents

Thats the link I found, quite old post, and I dont know how to apply that to my code, Here is my function

public void GetDataFromURL() throws IOException {
    String URL = "http://*IP ADDRESS*/SS/?C=M;O=D";
    Document document = Jsoup.connect(URL).userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.120 Safari/535.2").get();
    Elements links = document.select("a[href]");
    for (Element link : links.subList(5, 40)) {
        urls.add(link.attr("abs:href"));
    }

When I run the app it throws a Fatal error, which is networking in main thread which I dont really know how it works I was hoping the user agent would run the connect off the main thread but that doesnt seem to be happening and I dont know what to do .

android.os.NetworkOnMainThreadException


Solution

  • Your problem is not Jsoup related. It's about how Android handles connections.

    How do I fix android.os.NetworkOnMainThreadException?