Android Jsoup.connect(String).execute()失败

时间:2014-08-09 23:09:35

标签: java android asp.net jsoup

我正在尝试从http://reednavigation.com/lunars/lunars_v5.asp返回的html表中访问所选数据(浏览器中的完整路径可以在注释参数Almanac_full的下面的代码中找到)。

我已经尝试复制在stackoverflow上其他地方看到的类似问题的代码但是无法通过第一个Jsoup.connect()。执行行。它总是落在我的异常捕获中。

我是Android和Java的相对新手,所以任何想法(以及尽可能多的容忍度)都将受到高度赞赏。

由于

代码:

    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.");
    }

1 个答案:

答案 0 :(得分:0)

原来问题是我尝试在应用程序的主线程上执行网络操作。

这个Q和As是解决方案(Async)How to fix android.os.NetworkOnMainThreadException?

感谢@DanielB指导我找到答案。