使用Android应用调用WebService实现天气预报
?
對(duì)于手機(jī)等小型設(shè)備而言,它們的計(jì)算資源,存儲(chǔ)資源都十分有限,因此Android應(yīng)用不大可能需要對(duì)外提供WebService,Android應(yīng)用通常只是充當(dāng)WebService的客戶端,調(diào)用遠(yuǎn)程WebService.
第一步:編寫(xiě)WebService
?????????? 1.在Microsoft Visual Studio,文件——新建——網(wǎng)站——"已安裝的模板"(Visual C#),.net Frameeork 4.0,Asp.NET空網(wǎng)站。
?????????? 2.添加新項(xiàng)——Web服務(wù)(WebService)
?????????? 3.需要注意的是Namespace應(yīng)該是自己計(jì)算機(jī)的IP地址
[WebService(Namespace = "http://10.1.2.106/Webservice/")]???????????4.寫(xiě)WebService.參數(shù)名是theCityCode.當(dāng)參數(shù)的值是“0519”的時(shí)候,返回字符串?dāng)?shù)組 weather
Web Service using System;using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
/// <summary>
///WeatherWebService 的摘要說(shuō)明
/// </summary>
[WebService(Namespace = "http://10.1.2.106/Webservice/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允許使用 ASP.NET AJAX 從腳本中調(diào)用此 Web 服務(wù),請(qǐng)取消對(duì)下行的注釋。
// [System.Web.Script.Services.ScriptService]
public class WeatherWebService : System.Web.Services.WebService {
public WeatherWebService () {
//如果使用設(shè)計(jì)的組件,請(qǐng)取消注釋以下行
//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] = "北風(fēng)4-5級(jí)";
weather[4] = "12月20日 多云";
weather[5] = "3℃/7℃";
weather[6] = "東北風(fēng)3-4級(jí)";
weather[7] = "12月21日 陰轉(zhuǎn)多云";
weather[8] = "5℃/9℃";
weather[9] = "東北風(fēng)3-4級(jí)";
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.發(fā)布網(wǎng)站。之后出現(xiàn)的界面應(yīng)該是這樣子:
第二步:Android手機(jī)客戶端
?????????? 1.新建一個(gè)類,WebService.java來(lái)調(diào)用已經(jīng)發(fā)布好的Web Service.根據(jù)城市編號(hào)來(lái)獲取天氣情況。
????????????? 需要注意的是:定義Web Service提供服務(wù)的URL,瀏覽之后
http://localhost/sj/WeatherWebService.asmx?wsdl將地址欄中的LOCALHOST改成自己的IP地址。
?
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 提供服務(wù)的URL
static final String SERVICE_URL = "http://10.1.2.106/sj/WeatherWebService.asmx";
// 調(diào)用Web Service 獲取天氣情況
public static SoapObject getWeatherOfChangZhou(String cityCode)
throws IOException, XmlPullParserException {
cityCode = "0519";
String methodName = "GetWeather";
// 1.創(chuàng)建HttpTransportSE對(duì)象
HttpTransportSE ht = new HttpTransportSE(SERVICE_URL);
ht.debug = true;
// 2.創(chuàng)建SoapSerializationEvvelope對(duì)象
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
// 3.創(chuàng)建SoapObject對(duì)象,創(chuàng)建該對(duì)象時(shí)需要傳入所調(diào)用的命名空間,以及WebService的方法名
SoapObject soapObject = new SoapObject(SERVICE_NAMESPACE, methodName);
// 4.傳遞參數(shù)給Web Service服務(wù)器端,調(diào)用addPeoperty("指定參數(shù)名="theCityCode",指定參數(shù)值=0519")
soapObject.addProperty("theCityCode", cityCode);
// 5.調(diào)用HttpTransportSE的setOutputSoapObject()方法或者
// 直接對(duì)BodyOut屬性賦值,將前兩步創(chuàng)建的SoapObject對(duì)象設(shè)為SoapSerializationEnvelope的傳出Soap消息題
envelope.bodyOut = soapObject;
// 設(shè)置與.Net提供的Web Service保持較好的兼容性
envelope.dotNet = true;
// 6.call(a,b)a的意思是soapAction
ht.call(SERVICE_NAMESPACE + methodName, envelope);
//7.訪問(wèn)SoapSerializationEnvelope的對(duì)象bodyIn屬性,該屬性返回一個(gè)soapObject對(duì)象,該隊(duì)形就是代表了Web Service的返回消息,解析該對(duì)象就可以獲得調(diào)用Web Service的返回值。 SoapObject result = (SoapObject) envelope.bodyIn;
SoapObject detail = (SoapObject) result.getProperty(methodName + "Result");
return detail;
}
}
?????????? 2.新建一個(gè)Activity將調(diào)用的webservice數(shù)據(jù)進(jìn)行顯示出來(lái)
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;// 后天的天氣
// 獲取遠(yuǎn)程Web Service 返回的對(duì)象
SoapObject detail = WebService.getWeatherOfChangZhou(city);
// 解析城市
cityName = detail.getProperty(0).toString();
// 解析今天的天氣情況
String date = detail.getProperty(1).toString();
weatherToday = "今天:" + date.split(" ")[0];// 因?yàn)?日期和 天氣狀況,寫(xiě)在同一個(gè)字符創(chuàng)當(dāng)中,中間是以
// 空格分隔開(kāi),所以用到split來(lái)分開(kāi)為兩個(gè)字符串
weatherToday = weatherToday + "\n天氣:" + date.split(" ")[1];
weatherToday = weatherToday + "\n氣溫:" + detail.getProperty(2).toString();
weatherToday = weatherToday + "\n風(fēng)力:" + 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風(fēng)力:" + 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風(fēng)力:" + detail.getProperty(9).toString() + "\n";
iconAfterday[0] = parseIcon(detail.getProperty(14).toString());
iconAfterday[1] = parseIcon(detail.getProperty(15).toString());
// 更新天氣內(nèi)容
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.工具方法,該方法負(fù)責(zé)把返回的天氣圖標(biāo)字符串,轉(zhuǎn)換為程序的圖片資源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;
}
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/sarah-susan/archive/2011/12/19/2293629.html
總結(jié)
以上是生活随笔為你收集整理的使用Android应用调用WebService实现天气预报的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 通过调用API函数实现的无边框窗体的拖拽
- 下一篇: 为应用“瘦身”!给 Android 应用