免费的天气Web Service接口

时间:2022-05-08 06:02:28

免费的天气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:

 public class MainActivity extends Activity {

     Button btn = null;

     TextView text = null;

     @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); initView(); } private void initView() {
final WeatherUtil weatherutil = new WeatherUtil(); btn = (Button) findViewById(R.id.btn);
text = (TextView) findViewById(R.id.weatherInfo);
btn.setOnClickListener(new Button.OnClickListener() { @Override
public void onClick(View v) {
String weather = "";
int provinceCode = weatherutil.getProvinceCode("辽宁"); //
int cityCode = weatherutil.getCityCode(provinceCode, "大连"); //
List<String> weatherList = weatherutil.getWeather(cityCode);
for (String str : weatherList) { weather = weather + str;
} text.setText(weather); }
});
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }

获取天气的帮助类:

WeatherUtil.class:

 public class WeatherUtil {

     private static String SERVICES_HOST = "www.webxml.com.cn";
private static String WEATHER_SERVICES_URL = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/";
private static String PROVINCE_CODE_URL = WEATHER_SERVICES_URL
+ "getRegionProvince";
private static String CITY_CODE_URL = WEATHER_SERVICES_URL
+ "getSupportCityString?theRegionCode=";
private static String WEATHER_QUERY_URL = WEATHER_SERVICES_URL
+ "getWeather?theUserID=&theCityCode="; public WeatherUtil() {
} public static int getProvinceCode(String provinceName) {
Document document;
DocumentBuilderFactory documentBF = DocumentBuilderFactory
.newInstance();
documentBF.setNamespaceAware(true);
int provinceCode = 0;
try {
DocumentBuilder documentB = documentBF.newDocumentBuilder();
InputStream inputStream = getSoapInputStream(PROVINCE_CODE_URL); // 具体webService相关
document = documentB.parse(inputStream);
NodeList nodeList = document.getElementsByTagName("string"); // 具体webService相关
int len = nodeList.getLength();
for (int i = 0; i < len; i++) {
Node n = nodeList.item(i);
String result = n.getFirstChild().getNodeValue();
String[] address = result.split(",");
String pName = address[0];
String pCode = address[1];
if (pName.equalsIgnoreCase(provinceName)) {
provinceCode = Integer.parseInt(pCode);
}
}
inputStream.close();
} catch (DOMException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return provinceCode;
} public static int getCityCode(int provinceCode, String cityName) {
Document doc;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
int cityCode = 0;
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream is = getSoapInputStream(CITY_CODE_URL + provinceCode); // 具体webService相关
doc = db.parse(is);
NodeList nl = doc.getElementsByTagName("string"); // 具体webService相关
int len = nl.getLength();
for (int i = 0; i < len; i++) {
Node n = nl.item(i);
String result = n.getFirstChild().getNodeValue();
String[] address = result.split(",");
String cName = address[0];
String cCode = address[1];
if (cName.equalsIgnoreCase(cityName)) {
cityCode = Integer.parseInt(cCode);
}
}
is.close();
} catch (DOMException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return cityCode;
} public static InputStream getSoapInputStream(String url) {
InputStream inputStream = null;
try {
URL urlObj = new URL(url);
URLConnection urlConn = urlObj.openConnection();
urlConn.setRequestProperty("Host", SERVICES_HOST); // 具体webService相关
urlConn.connect();
inputStream = urlConn.getInputStream();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return inputStream;
} public static List<String> getWeather(int cityCode) {
List<String> weatherList = new ArrayList<String>();
Document document;
DocumentBuilderFactory documentBF = DocumentBuilderFactory
.newInstance();
documentBF.setNamespaceAware(true);
try {
DocumentBuilder documentB = documentBF.newDocumentBuilder();
InputStream inputStream = getSoapInputStream(WEATHER_QUERY_URL
+ cityCode);
document = documentB.parse(inputStream);
NodeList nl = document.getElementsByTagName("string");
int len = nl.getLength();
for (int i = 0; i < len; i++) {
Node n = nl.item(i);
String weather = n.getFirstChild().getNodeValue();
System.out.println(i + "----->>>>" + weather); weatherList.add(weather);
}
inputStream.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (DOMException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return weatherList;
} }

源码下载:源码

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

  1. 通过ajax访问Tomcat服务器web service接口时出现No &&num;39&semi;Access-Control-Allow-Origin&&num;39&semi; 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&period;InvalidCastException&colon; 无法将类型为&OpenCurlyDoubleQuote;System&period;Collections&period;Generic&period;List&grave;1&lbrack;System&period;String&rsqb;”的对象强制转换为类型&OpenCurlyDoubleQuote;System&period;String&lbrack;&rsqb;”)

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

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

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

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

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

随机推荐

  1. sersync

    一.准备 1.目标:从192.168.0.1上把/app/web/cnblogs.com/data下文件同步到192.168.0.2下的/app/web/cnblogs-slave.com/data: ...

  2. 用git管理源代码

    自从做iOS开发的一年多以来,之前一直都是用svn进行代码管理.因为工作需要,我也开始用git管理源代码.关于git的基本使用,在此做一个详细的介绍,希望能对初次接触git的人有所帮助! 本篇博客针对 ...

  3. WebService 学习

    WebService是一种跨编程语言和跨操作系统平台的远程调用技术. XML+XSD, SOAP 和 WSDL 是构成WebService平台的三大技术. SOAP = HTTP协议 + XML数据格 ...

  4. js实现对比百分比

    <script> for(var i=1;i<=3;i++){ for(var j=2;j<=4;j++){ var hid="w_"+4+j+" ...

  5. 问题记录-运行Tomcat,项目程序没有响应

    问题描述:运行Tomcat,项目程序没有响应原因在于 修改成一致路径即可解决.

  6. 二、urllib进阶

    Handler处理器 和 自定义Opener opener是 urllib.request.OpenerDirector 的实例,我们之前一直都在使用的urlopen,它是一个特殊的opener(也就 ...

  7. freemarker&period;template&period;TemplateException&colon;Error executing macro&colon;mainSelect

    1.错误描述 freemarker.template.TemplateException:Error executing macro:mainSelect require parameter:id i ...

  8. CTR预估算法

    GBRT(Gradient Boost Regression Tree)渐进梯度回归树,XGBoost是GBRT的一个工程实现 LR(Logistics Regression )逻辑回归 Spark ...

  9. python列表和字符串的三种逆序遍历方式

    python列表和字符串的三种逆序遍历方式 列表的逆序遍历 a = [1,3,6,8,9] print("通过下标逆序遍历1:") for i in a[::-1]: print( ...

  10. windows中xcopy命令详解

    一.格式: 二.举例说明: 1.复制文件,文件路径有空格的,那么就使用双引号括起来.如果目标路径已经有相同文件了,使用覆盖方式而不进行提示.在复制文件的同时也复制空目录或子目录      xcopy  ...