⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 weatherservice.java

📁 java源代码
💻 JAVA
字号:
// Fig. 29.5: WeatherService.java
// WeatherService provides a method to retrieve weather
// information from the National Weather Service.
package com.deitel.advjhtp1.soap.weather;

// Java core packages
import java.io.*;
import java.net.URL;
import java.util.*;

public class WeatherService {
                                   
   private Vector weatherInformation;  // WeatherBean objects

   // get weather information from NWS
   private void updateWeatherConditions()
   {
      try {
         System.out.println( "Update weather information..." );

         // National Weather Service Travelers Forecast page
         URL url = new URL(
            "http://iwin.nws.noaa.gov/iwin/us/traveler.html" );

         // set up text input stream to read Web page contents
         BufferedReader in = new BufferedReader(
            new InputStreamReader( url.openStream() ) );

         // helps determine starting point of data on Web page
         String separator = "TAV12";  

         // locate separator string in Web page
         while ( !in.readLine().startsWith( separator ) )
            ;    // do nothing

         // strings representing headers on Travelers Forecast
         // Web page for daytime and nighttime weather
         String dayHeader =
            "CITY            WEA     HI/LO   WEA     HI/LO";
         String nightHeader =
            "CITY            WEA     LO/HI   WEA     LO/HI";
         
         String inputLine = "";

         // locate header that begins weather information
         do {
            inputLine = in.readLine();
         } while ( !inputLine.equals( dayHeader ) &&
                   !inputLine.equals( nightHeader ) );

         weatherInformation = new Vector(); // create Vector
         
         // create WeatherBeans containing weather data and 
         // store in weatherInformation Vector
         inputLine = in.readLine();  // get first city's data
 
         // The portion of inputLine containing relevant data 
         // is 28 characters long. If the line length is not at 
         // least 28 characters long, then done processing data.
         while ( inputLine.length() > 28 ) {

            // Prepare strings for WeatherBean for each city. 
            // First 16 characters are city name. Next, six 
            // characters are weather description. Next six 
            // characters are HI/LO or LO/HI temperature.
            weatherInformation.add( 
               inputLine.substring( 0, 16 ) );   
            weatherInformation.add( 
               inputLine.substring( 16, 22 ) );   
            weatherInformation.add( 
               inputLine.substring( 23, 29 ) );
            
            inputLine = in.readLine();  // get next city's data
         }

         in.close();  // close connection to NWS Web server  
         
         System.out.println( "Weather information updated." );
      }
      
      // process failure to connect to National Weather Service
      catch( java.net.ConnectException connectException ) {
         connectException.printStackTrace();
         System.exit( 1 );
      }
      
      // process other exceptions
      catch( Exception exception ) {
         exception.printStackTrace();
         System.exit( 1 );
      }
   }

   // implementation for WeatherService interface method
   public Vector getWeatherInformation() 
   {
      updateWeatherConditions();

      return weatherInformation;
   }
}

/***************************************************************
 * (C) Copyright 2002 by Deitel & Associates, Inc. and         *
 * Prentice Hall. All Rights Reserved.                         *
 *                                                             *
 * DISCLAIMER: The authors and publisher of this book have     *
 * used their best efforts in preparing the book. These        *
 * efforts include the development, research, and testing of   *
 * the theories and programs to determine their effectiveness. *
 * The authors and publisher make no warranty of any kind,     *
 * expressed or implied, with regard to these programs or to   *
 * the documentation contained in these books. The authors     *
 * and publisher shall not be liable in any event for          *
 * incidental or consequential damages in connection with, or  *
 * arising out of, the furnishing, performance, or use of      *
 * these programs.                                             *
 ***************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -