简体   繁体   中英

Android Jsoup.connect(String).execute() fails

I'm attempting to access selected data from the html table that is returned from http://reednavigation.com/lunars/lunars_v5.asp (full path works in browser and can be found in code below in the commented parameter Almanac_full).

I've tried replicating code seen elsewhere on stackoverflow for similar problems but can't get past the first Jsoup.connect().execute line. It always lands in my exception catch.

I am a relative novice at Android and Java, so any ideas (and as much tolerance as you can muster) will be greatly appreciated.

Thanks

Code:

    try {
        String Month= "August";
        String Day = "9";
        String Year = "2014";
        String Almanac = "http://reednavigation.com/lunars/lunars_v5.asp";
        //String Almanac_full = "http://reednavigation.com/lunars/lunars_v5.asp?BodySel=Include+Navigation+Stars&todaycheck=0&GMTmonth=August&GMTday=9&GMTyear=2014&gmtsel=Gr.+Mean+Time&hristep=Every+Hour&degformat=dd.dddd&usesha=on&OutType=N&LatDeg=&LatMin=&LatName=N&LonDeg=&LonMin=&LonName=W";

        Response response1 = Jsoup.connect(Almanac).method(Connection.Method.GET)
                .data("BodySel", "Include+Navigation+Stars")
                .data("todaycheck", "0")
                .data("GMTmonth", Month)
                .data("GMTday", Day)
                .data("GMTyear", Year)
                .data("gmtsel", "Gr.+Mean+Time")
                .data("hristep", "Every+Hour")
                .data("degformat", "dd.dddd")
                .data("usesha", "on")
                .data("OutType", "N")
                .data("LatDeg", "")
                .data("LatMin", "")
                .data("LatName", "N")
                .data("LonDeg", "")
                .data("LonMin", "")
                .data("LonName", "W")
                .timeout(10000)
                .execute();

        Map<String, String> cookies = response1.cookies(); //Get the session cookie

        Document doc = Jsoup.connect(Almanac).method(Connection.Method.POST)
        .data("BodySel", "Include+Navigation+Stars")
        .data("todaycheck", "0")
        .data("GMTmonth", Month)
        .data("GMTday", Day)
        .data("GMTyear", Year)
        .data("gmtsel", "Gr.+Mean+Time")
        .data("hristep", "Every+Hour")
        .data("degformat", "dd.dddd")
        .data("usesha", "on")
        .data("OutType", "N")
        .data("LatDeg", "")
        .data("LatMin", "")
        .data("LatName", "N")
        .data("LonDeg", "")
        .data("LonMin", "")
        .data("LonName", "W")
        .timeout(10000)
        .cookies(cookies)
        .post();

        String title = doc.title();

        Elements BodyElements = doc.getElementsContainingText(Body);

    }
    catch (Exception e) {
        System.out.println("Error occured reading URL.");
        OutputText.setText("Error occured reading URL.");
    }

Turns out the issue was my attempt to perform a networking operation on the application's main thread.

This Q and As is the go to for the solution (Async) How to fix android.os.NetworkOnMainThreadException?

Thanks to @DanielB for guiding me toward the answer.

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