Closing Jsoup Connection
Asked Answered
S

1

5

I need a little help understanding the basics of Jsoup. The following code works but I'm wondering if the connection needs to be closed somehow. Can't find anything on the Jsoup website about it. If the application remains untouched after the do in background method executes I get a message in log cat every five minutes or so saying "request time failed: java.net.SocketException: Address family not supported by protocol". So I want to make sure I'm not unnecessarily consuming data. Thank you.

            protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        try {
        //  connect to web page based on user input
            Document doc = Jsoup.connect(routeURL).get();


        //  select relevant page elements
            Elements fareStageNumbers = doc.getElementsByClass("fare_stages_inner_table");

       //   test printing out fare stage numbers
            for(Element div : fareStageNumbers){

                Log.v(TAG, div.text());

            }



        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

logcat message:

    01-12 20:58:28.755: D/SntpClient(78): request time failed: java.net.SocketException: Address family not supported by protocol
    01-12 21:03:28.765: D/SntpClient(78): request time failed: java.net.SocketException: Address family not supported by protocol
    01-12 21:08:28.775: D/SntpClient(78): request time failed: java.net.SocketException: Address family not supported by protocol
Stichomythia answered 13/1, 2013 at 11:14 Comment(0)
T
10

Jsoup closes the connection by its own, after the request is done:

// from 'org.jsoup.helper.HttpConnection' class
static HttpConnection.Response execute(Connection.Request req, HttpConnection.Response previousResponse) throws IOException {
    // ...
    HttpURLConnection conn = createConnection(req);
    HttpConnection.Response res;
    try {
        // ...
    } finally {
        // per Java's documentation, this is not necessary, and precludes keepalives. However in practise,
        // connection errors will not be released quickly enough and can cause a too many open files error.
        conn.disconnect();
    }
    // ...
}

Exception: Does your url contain the protocol (the url start with eg. http://)?

Tredecillion answered 13/1, 2013 at 19:14 Comment(4)
Hi, thanks, that's great! Yes, url starts with http://. I don't know what that means though. Should I use www. instead?Stichomythia
No, if your url starts with http:// everything is ok. Can you post the stacktrace of you exception?Tredecillion
I've posted what comes up in logcat. It continues like this printing the same message every five minutes.Stichomythia
https://mcmap.net/q/1765867/-debug-sntpclient-60-request-time-failed-java-net-socketexception-address-family-not-supported-by-protocol found this on another stack overflow post. "SNTP is the Network Time Protocol. The emulator tries to fetch the actual time. I think, this has nothing to do with your app."Stichomythia

© 2022 - 2024 — McMap. All rights reserved.