2013-11-22 125 views
0

我有一個問題,其中一個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

那麼,當你有像'}} else''這樣的代碼時,我並不感到驚訝......你有沒有通過調試檢查與'if'條件相關的值? – Zong

+0

如果firstLimit是時間範圍的下限,那麼您的前後是錯誤的方式。正如鄭宗立所說,如果你格式化並縮進o =妥善處理,那麼不匹配的大括號就會突出。 – Simon

+1

使用[extract method](http://www.refactoring.com/catalog/extractMethod.html)模式使此代碼可讀 – MariuszS

回答

1

你的if語句將返回true和僅如果您運行早上6點和下午6點之間的程序的第一個塊執行,因爲你比較當前的時間將這些時間。嘗試在白天運行該程序,你會得到不同的結果。如果這不是預期的功能,那麼還有另一個問題。