Android 天气预报2

来源:互联网 发布:手机淘宝抢购软件 编辑:程序博客网 时间:2024/04/30 23:36
前面已经讲过了基于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;
}


}
}
原创粉丝点击