使用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地址。
?
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;
}
?
?
轉載于:https://www.cnblogs.com/sarah-susan/archive/2011/12/19/2293629.html
總結
以上是生活随笔為你收集整理的使用Android应用调用WebService实现天气预报的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 通过调用API函数实现的无边框窗体的拖拽
- 下一篇: [Linux命令]Sed命令参数