免费的天气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("辽宁"); //
  27. int cityCode = weatherutil.getCityCode(provinceCode, "大连"); //
  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. }

源码下载:源码

免费的天气Web Service接口的更多相关文章

  1. 通过ajax访问Tomcat服务器web service接口时出现No 'Access-Control-Allow-Origin' header问题的解决办法

    问题描述 通过ajax访问Web服务器(Tomcat7.0.42)中的json web service接口的时候,报以下跨域问题: XMLHttpRequest cannot load http:// ...

  2. 使用wsimport和JAX-WS调用Web Service接口

    本文简单举例说明如何使用wsimport工具和JAX-WS API调用Web Service接口.此方法的优点:使用JDK自带的工具和API接口,无需依赖第三方库. JDK版本:1.8.0_141开发 ...

  3. 统计随机数及临界值Web Service接口

    (2017-02-04 银河统计) 统计函数API概念   API(Application Programming Interface,应用程序编程接口)是一些预先定义的函数,目的是提供应用程序与开发 ...

  4. 在网页中运用统计Web Service接口

    (2017-02-10 银河统计) 在"统计随机数及临界值Web Service接口"一文中介绍了常用统计分布四类Web Service接口(随机数.分位数.密度函数和累积分布函数 ...

  5. 使用JDK自带功能,实现一个简单的Web Service接口发布

    万事开头难,本篇文章的目的就是使用JDK自带的功能,实现一个最简单的Web Service接口的发布. 下图是项目的组成,主要有三个部分,一个接口(WS),一个接口的实现类(WSImp),还有一个接口 ...

  6. 建立Web Service 接口及调用

    WEB SERVICE 接口: [WebMethod] public string MaterialRequest(string jsonText) { string WorkNo; string P ...

  7. Web Service接口返回泛型的问题(System.InvalidCastException: 无法将类型为“System.Collections.Generic.List`1[System.String]”的对象强制转换为类型“System.String[]”)

    在使用C#写Web Service时遇到了个很奇怪的问题.返回值的类型是泛型(我用的是类似List<string>)的接口,测试时发现总是报什么无法转换为对象的错误,百思不得其解. 后来在 ...

  8. 免费的天气API测试接口

    网上几乎所有的天气接口都需要注册key,然后还各种频率限制,每天调用次数才几百次? 太坑爹了吧 一个简单的天气预报功能, 为什么要搞的这么复杂, 收什么费? 推荐一个真正免费的天气API接口, 返回j ...

  9. web service接口 wsdl和asmx有什么区别

    没有区别,只是后缀名的区别.Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的独立 ...

随机推荐

  1. (六)6.17 Neurons Networks convolutional neural network(cnn)

    之前所讲的图像处理都是小 patchs ,比如28*28或者36*36之类,考虑如下情形,对于一副1000*1000的图像,即106,当隐层也有106节点时,那么W(1)的数量将达到1012级别,为了 ...

  2. JS面向对象组件(五) -- 复制对象(拷贝继承)

    研究到这儿,竟然出现了一个面试题目 var a = { name : '小明' }; var b = a; b.name = '小强'; alert( a.name ); 结果必然是“小强”,原因是对 ...

  3. 字符串string

    1.字符串获取类.封装检测数字的方法 var str = '前端开发'; //alert(str.length); //alert(str.charAt()); //没有参数 取得索引是0 结果是:前 ...

  4. is_file和file_exists效率比较

    目前在弄文件缓存的时候用到了判定文件存在与否,is_file()还是file_exists()呢?is_file和file_exists两者效率比较起来,谁的运行速度更快呢?还是做个测试吧: 1 2 ...

  5. PHP中基本符号及使用方法

    注解符号:这个#还真很少用,能和shell通用还真不错呢. //,  # 单行注解多行注解 引号的使用 ' ' 单引号,没有任何意义,不经任何处理直接拿过来;" "双引号,php动 ...

  6. CURL: CURLE_COULDNT_CONNECT问题探究

    摘自::  存储系统研究: socket connect error 99(Cannot assign request address) 这是最近使用libcurl写http服务的压力测试的时候遇到的 ...

  7. core文件分析

    http://baidutech.blog.51cto.com/4114344/904419/ http://www.newsmth.net/pc/pccon.php?id=10001977& ...

  8. myeclipse 10 优化

    一.Myeclipse10修改字体 MyEclipse10是基于Eclipse3.7内核,但在Eclipse的Preferences-〉general-〉 Appearance->Colors ...

  9. ArcGIS 开发的一些知识学习点

    由于文章太多,不便转载,现主要列举如下: ArcGIS Runtime支持的GP工具列表 ArcGIS Runtime支持的GP工具列表 目录(?)[-] Standard版本Standard 空间分 ...

  10. 自定义View绘制字符串

    import android.app.Activity; import android.os.Bundle; import android.view.Display; import android.v ...