前面已經講過了基于JSON資料解析的天氣預報的開發,這次我們主要涉及的是基于WebService的天氣預報。
學習WebService需要具備以下知識。
1.SOAP(Simple Object Access Protocol)
[簡單對象通路協定](http://zh.wikipedia.org/zh-cn/SOAP)
2.WSDL(Web Service Discription language)
[WebService描述語言](http://zh.wikipedia.org/zh-cn/WSDL)
3.UDDI(Universal Description ,Description and Integration)
[統一描述,發現和整合協定](https://zh.wikipedia.org/zh-cn/UDDI)
這裡涉及到的是對SOAP操作
首先看我們的wsdl源:http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl
如果需要對WSDL想了解的話。請猛點
1.http://www.w3school.com.cn/wsdl/index.asp
2.http://www.360doc.com/content/08/1113/23/56145_1920514.shtml
首先我們通過這裡的wsdl的資訊得知有如下方法
1.getRegionCountry
獲得國外國家名稱和與之對應的ID
輸入參數:無,傳回資料:一維字元串數組。、
2.getRegionDataset
獲得中國省份、直轄市、地區;國家名稱(國外)和與之對應的ID
輸入參數:無,傳回資料:DataSet。
3.**getRegionProvince
獲得中國省份、直轄市、地區和與之對應的ID
輸入參數:無,傳回資料:一維字元串數組。
4.getSupportCityDataset
獲得支援的城市/地區名稱和與之對應的ID
輸入參數:theRegionCode = 省市、國家ID或名稱,傳回資料:DataSet。
5.**getSupportCityString
獲得支援的城市/地區名稱和與之對應的ID
輸入參數:theRegionCode = 省市、國家ID或名稱,傳回資料:一維字元串數組。
6.**getWeather
獲得天氣預報資料
輸入參數:城市/地區ID或名稱,傳回資料:一維字元串數組。
這裡隻是用到了加黑的方法。方法如下
1.到code.google.com/p/ksoap2-android/下載下傳jar檔案(ksoap-android-assembly.jar)這裡可以從附件中獲得。
2.将jar添加到項目中。加入Build Path
3.如何使用ksoap-android
1.建立HttpTransportSE對象,調用WebService
2.建立SoapSerializationEnvelope對象
3.SoapObject對象建立,傳圖方法和命名空間
4.為SoapObject設定屬性值
5.設定SoapObject為SoapSerializationEnvelope的傳出消息
6.調用call()
7.利用SoapSerializationEnvelope讀出對象,獲得屬性值。
見代碼
package com.cater.weather;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class WeatherActivity extends Activity
{
private final static String uri = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx";
private final static String namespace = "http://WebXml.com.cn/";
private static HttpTransportSE httpTransportSE;
private static SoapSerializationEnvelope envelope;
private static SoapObject soapObject;
private Spinner provinceSpinner;
private Spinner citySpinner;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.weather);
provinceSpinner = (Spinner) findViewById(R.id.spinner1);
citySpinner = (Spinner) findViewById(R.id.spinner2);
editText = (EditText) findViewById(R.id.editText1);
httpTransportSE = new HttpTransportSE(uri);
httpTransportSE.debug = true;
envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
List<String> provinces = getRegionProvince();
ListAdapter provinceAdapter = new ListAdapter(WeatherActivity.this, provinces);
provinceSpinner.setAdapter(provinceAdapter);
provinceSpinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> view, View parent, int position, long id)
{
List<String> cities = getCityListByProvince(provinceSpinner.getSelectedItem().toString());
ListAdapter cityAdapter = new ListAdapter(WeatherActivity.this, cities);
citySpinner.setAdapter(cityAdapter);
citySpinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> view, View parent, int position, long id)
{
showWeather(citySpinner.getSelectedItem().toString());
}
@Override
public void onNothingSelected(AdapterView<?> arg0)
{
}
});
}
@Override
public void onNothingSelected(AdapterView<?> arg0)
{
}
});
}
private void showWeather(String city)
{
SoapObject soapObject = getWeatherByCity(city);
String weatherOfToday = soapObject.getProperty(4).toString();
editText.setText(weatherOfToday);
}
private static List<String> getRegionProvince()
{
String methodName = "getRegionProvince";
soapObject = new SoapObject(namespace, methodName);
envelope.bodyOut = soapObject;
try
{
httpTransportSE.call(namespace + methodName, envelope);
if (envelope.getResponse() != null)
{
SoapObject result = (SoapObject) envelope.bodyIn;
SoapObject detail = (SoapObject) result.getProperty(methodName + "Result");
return parseProvinceOrCity(detail);
}
}
catch (IOException e)
{
e.printStackTrace();
}
catch (XmlPullParserException e)
{
e.printStackTrace();
}
return null;
}
private static List<String> parseProvinceOrCity(SoapObject detail)
{
List<String> list = new ArrayList<String>();
for (int i = 0; i < detail.getPropertyCount(); i++)
{
list.add(detail.getProperty(i).toString().split(",")[0]);
}
return list;
}
private static List<String> getCityListByProvince(String province)
{
String methodName = "getSupportCityString";
soapObject = new SoapObject(namespace, methodName);
soapObject.addProperty("theRegionCode", province);
envelope.bodyOut = soapObject;
try
{
httpTransportSE.call(namespace + methodName, envelope);
if (envelope.getResponse() != null)
{
SoapObject result = (SoapObject) envelope.bodyIn;
SoapObject detail = (SoapObject) result.getProperty(methodName + "Result");
return parseProvinceOrCity(detail);
}
}
catch (IOException e)
{
e.printStackTrace();
}
catch (XmlPullParserException e)
{
e.printStackTrace();
}
return null;
}
private static SoapObject getWeatherByCity(String cityName)
{
String methodName = "getWeather";
soapObject = new SoapObject(namespace, methodName);
soapObject.addProperty("theCityCode", cityName);
envelope.bodyOut = soapObject;
try
{
httpTransportSE.call(namespace + methodName, envelope);
if (envelope.getResponse() != null)
{
SoapObject result = (SoapObject) envelope.bodyIn;
SoapObject detail = (SoapObject) result.getProperty(methodName + "Result");
return detail;
}
}
catch (IOException e)
{
e.printStackTrace();
}
catch (XmlPullParserException e)
{
e.printStackTrace();
}
return null;
}
private class ListAdapter extends BaseAdapter
{
private List<String> list;
private Context context;
public ListAdapter(Context context, List<String> list)
{
this.context = context;
this.list = list;
}
@Override
public int getCount()
{
if (list == null)
{
return 0;
}
return list.size();
}
@Override
public Object getItem(int position)
{
if (list != null)
{
return list.get(position);
}
return null;
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
TextView textView = new TextView(context);
textView.setTextColor(Color.BLACK);
textView.setText(list.get(position));
return textView;
}
}
}