zoukankan      html  css  js  c++  java
  • 免费的天气Web Service接口

    免费的天气Web Service接口

    在android应用当中很多时候需要获取天气的信息,这里提供怎么获取天气信息:

    1. http://www.ayandy.com/Service.asmx?wsdl

     官网:http://www.ayandy.com

    2. http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl

    网站:http://www.webxml.com.cn/zh_cn/index.aspx ,此网站提供各种webservice接口

    3.更多WS接口(天气预报,IP地址搜索,火车时刻表,汇率等等)

    转至:http://developer.51cto.com/art/200908/147125.htm

    在用网上的Web Service的接口的时候都要引入一个jar包:  下载地址

    4.用android 的一个例子来获取天气数据:                         程序一定要注意是否打开了联网的权限

    MainActivity:

     1 public class MainActivity extends Activity {
     2 
     3     Button btn = null;
     4 
     5     TextView text = null;
     6 
     7     @Override
     8     protected void onCreate(Bundle savedInstanceState) {
     9         super.onCreate(savedInstanceState);
    10         setContentView(R.layout.activity_main);
    11 
    12         initView();
    13 
    14     }
    15 
    16     private void initView() {
    17         final WeatherUtil weatherutil = new WeatherUtil();
    18 
    19         btn = (Button) findViewById(R.id.btn);
    20         text = (TextView) findViewById(R.id.weatherInfo);
    21         btn.setOnClickListener(new Button.OnClickListener() {
    22 
    23             @Override
    24             public void onClick(View v) {
    25                 String weather = "";
    26                 int provinceCode = weatherutil.getProvinceCode("辽宁"); // 3119
    27                 int cityCode = weatherutil.getCityCode(provinceCode, "大连"); // 974
    28                 List<String> weatherList = weatherutil.getWeather(cityCode);
    29                 for (String str : weatherList) {
    30 
    31                     weather = weather + str;
    32                 }
    33 
    34                 text.setText(weather);
    35 
    36             }
    37         });
    38     }
    39 
    40     @Override
    41     public boolean onCreateOptionsMenu(Menu menu) {
    42         getMenuInflater().inflate(R.menu.main, menu);
    43         return true;
    44     }
    45 
    46 }

    获取天气的帮助类:

    WeatherUtil.class:

      1 public class WeatherUtil {
      2 
      3     private static String SERVICES_HOST = "www.webxml.com.cn";
      4     private static String WEATHER_SERVICES_URL = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/";
      5     private static String PROVINCE_CODE_URL = WEATHER_SERVICES_URL
      6             + "getRegionProvince";
      7     private static String CITY_CODE_URL = WEATHER_SERVICES_URL
      8             + "getSupportCityString?theRegionCode=";
      9     private static String WEATHER_QUERY_URL = WEATHER_SERVICES_URL
     10             + "getWeather?theUserID=&theCityCode=";
     11 
     12     public WeatherUtil() {
     13     }
     14 
     15     public static int getProvinceCode(String provinceName) {
     16         Document document;
     17         DocumentBuilderFactory documentBF = DocumentBuilderFactory
     18                 .newInstance();
     19         documentBF.setNamespaceAware(true);
     20         int provinceCode = 0;
     21         try {
     22             DocumentBuilder documentB = documentBF.newDocumentBuilder();
     23             InputStream inputStream = getSoapInputStream(PROVINCE_CODE_URL); // 具体webService相关
     24             document = documentB.parse(inputStream);
     25             NodeList nodeList = document.getElementsByTagName("string"); // 具体webService相关
     26             int len = nodeList.getLength();
     27             for (int i = 0; i < len; i++) {
     28                 Node n = nodeList.item(i);
     29                 String result = n.getFirstChild().getNodeValue();
     30                 String[] address = result.split(",");
     31                 String pName = address[0];
     32                 String pCode = address[1];
     33                 if (pName.equalsIgnoreCase(provinceName)) {
     34                     provinceCode = Integer.parseInt(pCode);
     35                 }
     36             }
     37             inputStream.close();
     38         } catch (DOMException e) {
     39             e.printStackTrace();
     40         } catch (ParserConfigurationException e) {
     41             e.printStackTrace();
     42         } catch (SAXException e) {
     43             e.printStackTrace();
     44         } catch (IOException e) {
     45             e.printStackTrace();
     46         }
     47         return provinceCode;
     48     }
     49 
     50     public static int getCityCode(int provinceCode, String cityName) {
     51         Document doc;
     52         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
     53         dbf.setNamespaceAware(true);
     54         int cityCode = 0;
     55         try {
     56             DocumentBuilder db = dbf.newDocumentBuilder();
     57             InputStream is = getSoapInputStream(CITY_CODE_URL + provinceCode); // 具体webService相关
     58             doc = db.parse(is);
     59             NodeList nl = doc.getElementsByTagName("string"); // 具体webService相关
     60             int len = nl.getLength();
     61             for (int i = 0; i < len; i++) {
     62                 Node n = nl.item(i);
     63                 String result = n.getFirstChild().getNodeValue();
     64                 String[] address = result.split(",");
     65                 String cName = address[0];
     66                 String cCode = address[1];
     67                 if (cName.equalsIgnoreCase(cityName)) {
     68                     cityCode = Integer.parseInt(cCode);
     69                 }
     70             }
     71             is.close();
     72         } catch (DOMException e) {
     73             e.printStackTrace();
     74         } catch (ParserConfigurationException e) {
     75             e.printStackTrace();
     76         } catch (SAXException e) {
     77             e.printStackTrace();
     78         } catch (IOException e) {
     79             e.printStackTrace();
     80         }
     81         return cityCode;
     82     }
     83 
     84     public static InputStream getSoapInputStream(String url) {
     85         InputStream inputStream = null;
     86         try {
     87             URL urlObj = new URL(url);
     88             URLConnection urlConn = urlObj.openConnection();
     89             urlConn.setRequestProperty("Host", SERVICES_HOST); // 具体webService相关
     90             urlConn.connect();
     91             inputStream = urlConn.getInputStream();
     92         } catch (MalformedURLException e) {
     93             e.printStackTrace();
     94         } catch (IOException e) {
     95             e.printStackTrace();
     96         }
     97         return inputStream;
     98     }
     99 
    100     public static List<String> getWeather(int cityCode) {
    101         List<String> weatherList = new ArrayList<String>();
    102         Document document;
    103         DocumentBuilderFactory documentBF = DocumentBuilderFactory
    104                 .newInstance();
    105         documentBF.setNamespaceAware(true);
    106         try {
    107             DocumentBuilder documentB = documentBF.newDocumentBuilder();
    108             InputStream inputStream = getSoapInputStream(WEATHER_QUERY_URL
    109                     + cityCode);
    110             document = documentB.parse(inputStream);
    111             NodeList nl = document.getElementsByTagName("string");
    112             int len = nl.getLength();
    113             for (int i = 0; i < len; i++) {
    114                 Node n = nl.item(i);
    115                 String weather = n.getFirstChild().getNodeValue();
    116                 System.out.println(i + "----->>>>" + weather);
    117 
    118                 weatherList.add(weather);
    119             }
    120             inputStream.close();
    121         } catch (UnsupportedEncodingException e) {
    122             e.printStackTrace();
    123         } catch (DOMException e) {
    124             e.printStackTrace();
    125         } catch (ParserConfigurationException e) {
    126             e.printStackTrace();
    127         } catch (SAXException e) {
    128             e.printStackTrace();
    129         } catch (IOException e) {
    130             e.printStackTrace();
    131         }
    132         return weatherList;
    133     }
    134 
    135 }

    源码下载:源码

  • 相关阅读:
    Java学习--泛型
    java学习--自定义类的实例的大小比较和排序
    Java学习--枚举
    java学习--修饰符
    Java学习--变量
    POI--各种样式的XSSFCellStyle的生成
    POI 使用颜色字符串生成XSSFColor对象
    Others # 看到的一些創意 / 知乎不錯的提問/ Android安全
    RO # 不错站点
    Linux # Virtual Box VBoxGuestAdditions.iso
  • 原文地址:https://www.cnblogs.com/liangstudyhome/p/3700420.html
Copyright © 2011-2022 走看看