使用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

Web Service
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地址。


 

WebService.java
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数据进行显示出来

showWeather
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。

parseIcon.java
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;
}

 

 

posted on 2011-12-19 17:11  sarah_susan  阅读(2109)  评论(0编辑  收藏  举报

导航