使用Android应用调用WebService实现天气预报
对于手机等小型设备而言,它们的计算资源,存储资源都十分有限,因此Android应用不大可能需要对外提供WebService,Android应用通常只是充当WebService的客户端,调用远程WebService.
第一步:编写WebService
1.在Microsoft Visual Studio,文件——新建——网站——"已安装的模板"(Visual C#),.net Frameeork 4.0,Asp.NET空网站。
2.添加新项——Web服务(WebService)
3.需要注意的是Namespace应该是自己计算机的IP地址
[WebService(Namespace = "http://10.1.2.106/Webservice/")]
4.写WebService.参数名是theCityCode.当参数的值是“0519”的时候,返回字符串数组 weather
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Services; /// <summary>///WeatherWebService 的摘要说明/// </summary>[WebService(Namespace = "http://10.1.2.106/Webservice/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 // [System.Web.Script.Services.ScriptService]public class WeatherWebService : System.Web.Services.WebService { public WeatherWebService () { //如果使用设计的组件,请取消注释以下行 //InitializeComponent(); } [WebMethod(Description="获取城市天气情况")]public String[] GetWeather(String theCityCode) { String[] weather = new String[16]; weather[0] = "常州"; weather[1] = "12月19日 晴"; weather[2] = "0℃/6℃"; weather[3] = "北风4-5级"; weather[4] = "12月20日 多云"; weather[5] = "3℃/7℃"; weather[6] = "东北风3-4级"; weather[7] = "12月21日 阴转多云"; weather[8] = "5℃/9℃"; weather[9] = "东北风3-4级"; weather[10] = "0.gif"; weather[11] = "0.gif"; weather[12] = "1.gif"; weather[13] = "1.gif"; weather[14] = "2.gif"; weather[15] = "1.gif"; if (theCityCode == "0519") {return weather; }return null; } }
5.发布网站。之后出现的界面应该是这样子:
第二步:Android手机客户端
1.新建一个类,WebService.java来调用已经发布好的Web Service.根据城市编号来获取天气情况。
需要注意的是:定义Web Service提供服务的URL,浏览之后
http://localhost/sj/WeatherWebService.asmx?wsdl
将地址栏中的LOCALHOST改成自己的IP地址。
package my.learn.weather; import java.io.IOException; import org.ksoap2.SoapEnvelope;import org.ksoap2.serialization.SoapObject;import org.ksoap2.serialization.SoapSerializationEnvelope;import org.ksoap2.transport.HttpTransportSE;import org.xmlpull.v1.XmlPullParserException; public class WebService {// 定义Web Service 的命名空间 static final String SERVICE_NAMESPACE = "http://10.1.2.106/Webservice/";// 定义Web Service 提供服务的URL static final String SERVICE_URL = "http://10.1.2.106/sj/WeatherWebService.asmx"; // 调用Web Service 获取天气情况 public static SoapObject getWeatherOfChangZhou(String cityCode)throws IOException, XmlPullParserException { cityCode = "0519"; String methodName = "GetWeather";// 1.创建HttpTransportSE对象 HttpTransportSE ht = new HttpTransportSE(SERVICE_URL); ht.debug = true;// 2.创建SoapSerializationEvvelope对象 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);// 3.创建SoapObject对象,创建该对象时需要传入所调用的命名空间,以及WebService的方法名 SoapObject soapObject = new SoapObject(SERVICE_NAMESPACE, methodName);// 4.传递参数给Web Service服务器端,调用addPeoperty("指定参数名="theCityCode",指定参数值=0519") soapObject.addProperty("theCityCode", cityCode);// 5.调用HttpTransportSE的setOutputSoapObject()方法或者// 直接对BodyOut属性赋值,将前两步创建的SoapObject对象设为SoapSerializationEnvelope的传出Soap消息题 envelope.bodyOut = soapObject;// 设置与.Net提供的Web Service保持较好的兼容性 envelope.dotNet = true; // 6.call(a,b)a的意思是soapAction ht.call(SERVICE_NAMESPACE + methodName, envelope);
//7.访问SoapSerializationEnvelope的对象bodyIn属性,该属性返回一个soapObject对象,该队形就是代表了Web Service的返回消息,解析该对象就可以获得调用Web Service的返回值。
SoapObject result = (SoapObject) envelope.bodyIn; SoapObject detail = (SoapObject) result.getProperty(methodName + "Result");return detail; } }
2.新建一个Activity将调用的webservice数据进行显示出来
private void showWeather(String city) throws IOException,// 显示天气情况 XmlPullParserException { String cityName = null;int iconToday[] = new int[2];int iconTomorrow[] = new int[2];int iconAfterday[] = new int[2]; String weatherToday = null;// 今天的天气 String weatherTomorrow = null;// 明天的天气 String weatherAfterday = null;// 后天的天气 // 获取远程Web Service 返回的对象 SoapObject detail = WebService.getWeatherOfChangZhou(city); // 解析城市 cityName = detail.getProperty(0).toString();// 解析今天的天气情况 String date = detail.getProperty(1).toString(); weatherToday = "今天:" + date.split(" ")[0];// 因为 日期和 天气状况,写在同一个字符创当中,中间是以// 空格分隔开,所以用到split来分开为两个字符串 weatherToday = weatherToday + "\n天气:" + date.split(" ")[1]; weatherToday = weatherToday + "\n气温:" + detail.getProperty(2).toString(); weatherToday = weatherToday + "\n风力:" + detail.getProperty(3).toString() + "\n"; iconToday[0] = parseIcon(detail.getProperty(10).toString()); iconToday[1] = parseIcon(detail.getProperty(11).toString()); // 解析明天的天气情况 date = detail.getProperty(4).toString(); weatherTomorrow = "明天:" + date.split(" ")[0]; weatherTomorrow = weatherTomorrow + "\n天气:" + date.split(" ")[1]; weatherTomorrow = weatherTomorrow + "\n气温:" + detail.getProperty(5).toString(); weatherTomorrow = weatherTomorrow + "\n风力:" + detail.getProperty(6).toString() + "\n"; iconTomorrow[0] = parseIcon(detail.getProperty(12).toString()); iconTomorrow[1] = parseIcon(detail.getProperty(13).toString()); // 解析明天的天气情况 date = detail.getProperty(7).toString(); weatherAfterday = "后天:" + date.split(" ")[0]; weatherAfterday = weatherAfterday + "\n天气:" + date.split(" ")[1]; weatherAfterday = weatherAfterday + "\n气温:" + detail.getProperty(8).toString(); weatherAfterday = weatherAfterday + "\n风力:" + detail.getProperty(9).toString() + "\n"; iconAfterday[0] = parseIcon(detail.getProperty(14).toString()); iconAfterday[1] = parseIcon(detail.getProperty(15).toString()); // 更新天气内容 txtCity.setText("城市:" + cityName); txtToday.setText(weatherToday); todayWhIcon1.setImageResource(iconToday[0]); todayWhIcon2.setImageResource(iconToday[1]); txtTomorrow.setText(weatherTomorrow); tomorrowWhIcon1.setImageResource(iconTomorrow[0]); tomorrowWhIcon2.setImageResource(iconTomorrow[1]); txtAfterday.setText(weatherAfterday); afterdayWhIcon1.setImageResource(iconAfterday[0]); afterdayWhIcon2.setImageResource(iconAfterday[1]); }
3.工具方法,该方法负责把返回的天气图标字符串,转换为程序的图片资源ID。
private int parseIcon(String strIcon) {if (strIcon == null)return -1;if ("0.gif".equals(strIcon))return R.drawable.a_0;if ("1.gif".equals(strIcon))return R.drawable.a_1;if ("2.gif".equals(strIcon))return R.drawable.a_2;if ("3.gif".equals(strIcon))return R.drawable.a_3;if ("4.gif".equals(strIcon))return R.drawable.a_4;if ("5.gif".equals(strIcon))return R.drawable.a_5;if ("6.gif".equals(strIcon))return R.drawable.a_6;if ("7.gif".equals(strIcon))return R.drawable.a_7;if ("8.gif".equals(strIcon))return R.drawable.a_8;if ("9.gif".equals(strIcon))return R.drawable.a_9;if ("10.gif".equals(strIcon))return R.drawable.a_10;if ("11.gif".equals(strIcon))return R.drawable.a_11;if ("12.gif".equals(strIcon))return R.drawable.a_12;if ("13.gif".equals(strIcon))return R.drawable.a_13;if ("14.gif".equals(strIcon))return R.drawable.a_14;if ("15.gif".equals(strIcon))return R.drawable.a_15;if ("16.gif".equals(strIcon))return R.drawable.a_16;if ("17.gif".equals(strIcon))return R.drawable.a_17;if ("18.gif".equals(strIcon))return R.drawable.a_18;if ("19.gif".equals(strIcon))return R.drawable.a_19;if ("20.gif".equals(strIcon))return R.drawable.a_20;if ("21.gif".equals(strIcon))return R.drawable.a_21;if ("22.gif".equals(strIcon))return R.drawable.a_22;if ("23.gif".equals(strIcon))return R.drawable.a_23;if ("24.gif".equals(strIcon))return R.drawable.a_24;if ("25.gif".equals(strIcon))return R.drawable.a_25;if ("26.gif".equals(strIcon))return R.drawable.a_26;if ("27.gif".equals(strIcon))return R.drawable.a_27;if ("28.gif".equals(strIcon))return R.drawable.a_28;if ("29.gif".equals(strIcon))return R.drawable.a_29;if ("30.gif".equals(strIcon))return R.drawable.a_30;if ("31.gif".equals(strIcon))return R.drawable.a_31;return 0; }
转载于:https://www.cnblogs.com/sarah-susan/archive/2011/12/19/2293629.html
使用Android应用调用WebService实现天气预报相关推荐
- 我的Android进阶之旅------Android通过调用Webservice实现天气预报
通过这一篇文章WebService的读书笔记对Web Service的认识,现在来写一个小应用Android通过调用Webservice实现天气预报来加强对Web Srevice的学习 在开发天气预报 ...
- 在Android中使用Android Ksoap2调用WebService
一.WebService介绍 WebService是基于SOAP协议可实现web服务器与web服务器之间的通信,因采用SOAP协议传送XML数据具有平台无关性,也是成为解决异构平台之间通信的重要解决方 ...
- android之调用webservice实现图片上传
http://www.cnblogs.com/top5/archive/2012/02/16/2354517.html 最近boss要求做android客户端的图片上传和下载,就是调用服务器的webs ...
- C#调用WebService实现天气预报
C#调用WebService实现天气预报 C#调用WebService实现天气预报 一.了解WebService? 二.天气预报实现 三.实现途径 C#调用WebService实现天气预报 本文主要是 ...
- Android中调用webservice的工具类
最近学习WebService,感觉利用这个借口开发网站的Android客户端方便及了,用到一个工具类,这里铭记一下. public static final String WebServiceName ...
- android ksoap调用webservice批量上传多张图片
这几天一直在开发app,哎呀,什么都是第一接触,想想自己自学Java,然后自学Android,一直没有放弃,曾想放弃的,但是想到爸妈供我上学,不能在宿舍里面玩游戏,加入学校实验室,一天没课就来着里学习 ...
- android wsdl封装,在Android中调用C#写的WebService(附源代码)
由于项目中要使用Android调用C#写的WebService,于是便有了这篇文章.在学习的过程中,发现在C#中直接调用WebService方便得多,直接添加一个引用,便可以直接使用将WebServi ...
- Android使用ksoap2-android调用WebService学习
之前主要做客户端UI交互,很少处理数据和接触服务端,但现在的移动设备根本不可能离得开网络连接,数据的交换.最近学习的是在android端如何去调用远程WebService,都说WebService是一 ...
- 老菜鸟迈出的第一步——Android调用WebService碰到的种种问题
首先声明本人是个菜鸟,菜到什么程度?我马士兵的java教学视频javase部分还没看完,郭霖的<第一行代码>我才看了不超过20页.就是这样的菜鸟以一个企业网管的身份接到了经理给我出的练习题 ...
最新文章
- 怎样才算熟悉python-怎么样才算是精通 Python?
- 打造万能的Python开发环境
- 从单体到混乱的微服务,阿里云托管式服务网格是如何诞生的?
- 【NLP】通俗讲解从Transformer到BERT模型!
- MySQL基础篇(01):经典实用查询案例,总结整理
- 山寨AirPods2020年出货量已达6亿副,是正版7倍,网友:怪不得满大街都是
- cs寄存器 x86 特权模式_Windows操作系统管理进程和线程:内核模式和用户模式
- override与final
- UNITY开发VR从入门到放弃---VR自学手册
- 基于.Net C# 通信开发-网络调试助手
- openGL 例子:创建一个二十面体
- 【深度】谈谈我对于5G的理解
- OutLook邮箱服务器设置
- thermal系列(7)-Thermal配置和调试
- 详细不啰嗦,电脑重装系统win10教程分享
- 【社区图书馆】《新程序员005:开源深度指南 新金融背后的科技力量》
- MCU 上电复位功能的使用注意点
- 表单reset重置按钮的作用并非是清空表单
- 计算机毕业设计基于asp.net网上考试报名系统——计算机毕业设计
- 游戏设计自学记录(27)