参考资源:
http://blog.youkuaiyun.com/pi9nc/article/details/9297085
WebServiceUtil.java
package com.example.myweather;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
public class WebServiceUtil {
/*
* eg:
* <?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<getRegionProvince xmlns="http://WebXml.com.cn/" />
</soap:Body>
</soap:Envelope>*/
static final String SERVICE_NS = "http://WebXml.com.cn/";//xmlns namespace
static final String SERVICE_URL = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx";
/*输入参数:无,返回数据:一维字符串数组 String()
结构:用英文逗号分割的省份(直辖市、地区)和对应ID。示例:
'VB
Array(0) = "上海,31112"
……
……
Array(n) = "城市/地区
*/
//
public static List<String> getProvinceList(){
//<getRegionProvince xmlns="http://WebXml.com.cn/" />
final String methodName = "getRegionProvince";
//step1 --
final HttpTransportSE ht = new HttpTransportSE(SERVICE_URL);
final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
SoapObject soapObject = new SoapObject(SERVICE_NS,methodName);
envelope.bodyOut = soapObject;//
envelope.dotNet = true;
//
FutureTask<List<String>> task = new FutureTask<List<String>>(
new Callable<List<String>>(){
@Override
public List<String> call() throws Exception{
//key: call relevant method: SERVICE_NS + methodName
//the call result is in the envelope
ht.call(SERVICE_NS + methodName, envelope);
if(envelope.getResponse()!=null){
SoapObject result = (SoapObject)envelope.bodyIn;//
/*
<getRegionProvinceResponse xmlns="http://WebXml.com.cn/">
<getRegionProvinceResult>
<string>string</string>
<string>string</string>
</getRegionProvinceResult>
*/
SoapObject detail = (SoapObject) result.getProperty(methodName + "Result");
return parseProvinceorCity(detail);
}
return null;
}
}
);
new Thread(task).start();
try{
return task.get();//
}catch(Exception e){
e.printStackTrace();
}
return null;
}
//
public static List<String> getCityListByProvince(String province){
//Request:
/* <getSupportCityString xmlns="http://WebXml.com.cn/">
<theRegionCode>string</theRegionCode>
</getSupportCityString>*/
//Response:
/*
* <getSupportCityStringResponse xmlns="http://WebXml.com.cn/">
<getSupportCityStringResult>
<string>string</string>
<string>string</string>
</getSupportCityStringResult>
</getSupportCityStringResponse>*/
//输入参数:theRegionCode = 省市、国家ID或名称,返回数据:一维字符串数组。
//返回数据:一维字符串数组 String()。结构:用---英文逗号分割的城市或地区名称和对应ID---。
//eg:Array(0) = "无城市,000000"
final String methodName = "getSupportCityString";
final HttpTransportSE ht = new HttpTransportSE(SERVICE_URL);
final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
SoapObject soapObject = new SoapObject(SERVICE_NS,methodName);
soapObject.addProperty("theRegionCode", province);//<--输入参数:theRegionCode
envelope.bodyOut = soapObject;
envelope.dotNet = true;
//define the thread to get city List of the given province
FutureTask<List<String>> task = new FutureTask<List<String>> (
new Callable<List<String>>(){
@Override
public List<String> call() throws Exception{
ht.call(SERVICE_NS + methodName, envelope);
if(envelope.getResponse() != null){
SoapObject result = (SoapObject)envelope.bodyIn;
SoapObject detail = (SoapObject) result.getProperty("getSupportCityStringResult");
return parseProvinceorCity(detail);
}
return null;
}
});
//run the thread
new Thread(task).start();
try{
return task.get();
}catch(Exception e){
e.printStackTrace();
}
return null;
}
//
private static List<String> parseProvinceorCity(SoapObject detail){
ArrayList<String> result = new ArrayList<String>();
for(int i = 0;i<detail.getPropertyCount();i++){
result.add(detail.getProperty(i).toString().split(",")[0]);//
}
return result;
}
//result type: SoapObject
public static SoapObject getWeatherByCity(String cityName){
/*
* <getWeather xmlns="http://WebXml.com.cn/">
<theCityCode>string</theCityCode> <--- addProperty
<theUserID>string</theUserID>
<getWeatherResponse xmlns="http://WebXml.com.cn/">
<getWeatherResult>
<string>string</string>
<string>string</string>
</getWeatherResult>
</getWeatherResponse>
* */
final String methodName = "getWeather";
final HttpTransportSE ht = new HttpTransportSE(SERVICE_URL);
//ht.debug = true;//
final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
SoapObject soapObject = new SoapObject(SERVICE_NS,methodName);
soapObject.addProperty("theCityCode", cityName);//<--输入参数:theRegionCode type:String
envelope.bodyOut = soapObject;
envelope.dotNet = true;
FutureTask<SoapObject> task = new FutureTask<SoapObject> (
new Callable<SoapObject>(){
@Override
public SoapObject call() throws Exception{
ht.call(SERVICE_NS + methodName, envelope);
if(envelope.getResponse() != null){
SoapObject result = (SoapObject)envelope.bodyIn;
SoapObject detail = (SoapObject) result.getProperty(methodName + "Result");
return detail;//
}
return null;
}
});
//run the thread
new Thread(task).start();
try{
return task.get();
}catch(Exception e){
e.printStackTrace();
}
return null;
}
}
ListAdapter.java
package com.example.myweather;
import java.util.List;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class ListAdapter extends BaseAdapter
{
private Context context;
private List<String> values;
public ListAdapter(Context context , List<String> values)
{
this.context = context;
this.values = values;// get the data of values from main programme
}
@Override
public int getCount()
{
return values.size();
}
@Override
public Object getItem(int position)
{
return values.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
TextView text = new TextView(context);
text.setText(values.get(position));//*************
text.setTextSize(20);
text.setTextColor(Color.BLUE);//set text color
return text;
}
}
MyWeather.java
package com.example.myweather;
import java.util.List;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class ListAdapter extends BaseAdapter
{
private Context context;
private List<String> values;
public ListAdapter(Context context , List<String> values)
{
this.context = context;
this.values = values;// get the data of values from main programme
}
@Override
public int getCount()
{
return values.size();
}
@Override
public Object getItem(int position)
{
return values.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
TextView text = new TextView(context);
text.setText(values.get(position));//*************
text.setTextSize(20);
text.setTextColor(Color.BLUE);//set text color
return text;
}
}
注释:
1.
http://www.webxml.com.cn/zh_cn/weather_icon.aspx
天气预报WEB服务接口说明- 天气现象和图例
// 工具方法,该方法负责把返回的天气图标字符串,转换为程序的图片资源ID。
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;
…..
}
2.
WeatherService接口帮助文档
http://www.webxml.com.cn/files/WeatherWsHelp.pdf
eg:
getRegionProvince方法
获得中国省份、直辖市、地区和与之对应的ID
输入参数:无,返回数据:一维字符串数组 String()
结构:用英文逗号分割的省份(直辖市、地区)和对应ID。示例:
'VB
Array(0)= "上海,31112"
……
……
Array(n)= "城市/地区,ID"
3.
provinceSpinner.setOnItemSelectedListener(newAdapterView.OnItemSelectedListener() {}
首先应用启动的时候,就会自动调用一次OnItemSelectedListener中的事件
4.
测试FutureTask的用法,如果不想分支线程阻塞主线程,又想取得分支线程的执行结果,就用FutureTask。
FutureTask 仅在计算完成时才能获取结果;如果计算尚未完成,则阻塞 get方法。
相关线程类以及接口:
01.FutureTask
public class FutureTask<V>
extends Object
implements RunnableFuture<V>
构造方法摘要
FutureTask(
Callable<
V> callable)
创建一个 FutureTask,一旦运行就执行给定的 Callable。
方法摘要
V get()
如有必要,等待计算完成,然后获取其结果。
boolean isDone()
如果任务已完成,则返回 true。
02. Callable
java.util.concurrent
接口 Callable<V>
返回结果并且可能抛出异常的任务。实现者定义了一个不带任何参数的叫做call的方法。
方法摘要V call()
计算结果,如果无法计算结果,则抛出一个异常。
方法详细信息
call
V call()
hrows Exception
计算结果,如果无法计算结果,则抛出一个异常。
返回:
计算的结果
抛出:
Exception - 如果无法计算结果
eg:
public class CallableAndFuture {
publicstatic void main(String[] args) {
Callable<Integer> callable = new Callable<Integer>() {
publicInteger call() throws Exception {
returnnew Random().nextInt(100);
}
};
FutureTask<Integer> future = newFutureTask<Integer>(callable);
new Thread(future).start();
try{
Thread.sleep(5000);//可能做一些事情
System.out.println(future.get());
}catch (InterruptedException e) {
e.printStackTrace();
}catch (ExecutionException e) {
e.printStackTrace();
}
}
}