如果其他论点不起作用?

时间:2013-11-22 19:20:19

标签: java android if-statement jsoup

我遇到一个问题,其中if else参数只执行最后一个参数之一。

我有这个论点,如果条件在一定时间之间,那么jsoup应该解析一组数据,如果时间变量超出了一组时间,那么jsoup应该解析一组不同的数据,但它总是只解析最后一组。

我无法看到我犯了哪些错误,但对此可能原因的任何意见都非常感激。

Calendar firstLimit= Calendar.getInstance();
firstLimit.set(Calendar.HOUR_OF_DAY, 18);
firstLimit.set(Calendar.MINUTE, 00);
firstLimit.set(Calendar.SECOND, 00);

Calendar secondLimit= Calendar.getInstance();
secondLimit.set(Calendar.HOUR_OF_DAY, 06);
secondLimit.set(Calendar.MINUTE, 00);
secondLimit.set(Calendar.SECOND, 00);

Calendar currentTime=Calendar.getInstance();

Document docWeather1;

try {
    //Current weather from Met Office
    Connection.Response response = Jsoup.connect("http://datapoint.metoffice.gov.uk/public/data/val/wxfcs/"
            + "all/xml/3604?res=daily&key=2dd5950b-91e1-4671-9d83-625f2ae9cbf5")
        .timeout(10000)
        .ignoreHttpErrors(true)
        .execute();

    int statusCode = response.statusCode();

    if(statusCode == 200) {
        String xml = Jsoup.connect("http://datapoint.metoffice.gov.uk/public/data/val/"
                + "wxfcs/all/xml/3604?res=daily&key=2dd5950b-91e1-4671-9d83-625f2ae9cbf5")
            .timeout(10000).get().toString();//Turns document into a string ready for parsing                       
        docWeather1 = Jsoup.parse(xml, "", Parser.xmlParser());

        if(currentTime.before(firstLimit) && currentTime.after(secondLimit)){

            String weatherType = docWeather1.select("Rep:eq(0)").first().attr("W");
            arr_data.add(weatherType);
            String tempDayMax = docWeather1.select("Rep:eq(0)").first().attr("Dm");
            arr_data.add(tempDayMax);
            String tempDayFeelLike = docWeather1.select("Rep:eq(0)").first().attr("FDm");
            arr_data.add(tempDayFeelLike);
            String windSpeed = docWeather1.select("Rep:eq(0)").first().attr("S");
            arr_data.add(windSpeed);
            String windDirection = docWeather1.select("Rep:eq(0)").first().attr("D");
            arr_data.add(windDirection);
            String pricipProb = docWeather1.select("Rep:eq(0)").first().attr("Ppd");
            arr_data.add(pricipProb);

            System.out.println("Current Weather Night Data Collected and Stored");
        } else {
            String xml1 = Jsoup.connect("http://datapoint.metoffice.gov.uk/public/data/val/"
                                        + "wxfcs/all/xml/3604?res=daily&key=2dd5950b-91e1-4671-9d83-625f2ae9cbf5").timeout(10000).get().toString();//Turns document into a string ready for parsing                     
            docWeather1 = Jsoup.parse(xml1, "", Parser.xmlParser());

            String weatherType = docWeather1.select("Rep:eq(1)").first().attr("W");
            arr_data.add(weatherType);
            String tempDayMax = docWeather1.select("Rep:eq(1)").first().attr("Dm");
            arr_data.add(tempDayMax);
            String tempDayFeelLike = docWeather1.select("Rep:eq(1)").first().attr("FDm");
            arr_data.add(tempDayFeelLike);
            String windSpeed = docWeather1.select("Rep:eq(1)").first().attr("S");
            arr_data.add(windSpeed);
            String windDirection = docWeather1.select("Rep:eq(1)").first().attr("D");
            arr_data.add(windDirection);
            String pricipProb = docWeather1.select("Rep:eq(1)").first().attr("Ppd");
            arr_data.add(pricipProb);

            System.out.println("Current Weather Data Day Collected and Stored");
        }}else{
        System.out.println("received error code : " + statusCode);
    }
} catch (IOException e) {
    e.printStackTrace();
}   

return arr_data;// return ArrayList from here

1 个答案:

答案 0 :(得分:1)

只有当您在上午6点到下午6点之间运行程序时,if语句才会返回true并从第一个块执行,因为您要将当前时间与这些时间进行比较。尝试在白天运行程序,您将得到不同的结果。如果那不是预期的功能,那么还有另一个问题。