对象服务器Webservices获取天气

工作之余抽点时间出来写写博文,希望对新接触的朋友有帮助。今天在这里和大家一起学习一下对象服务器

    对象和服务器

    // 定义Web Service的命名空间

    static final String SERVICE_NS = "http://WebXml.com.cn/";

    // 定义Web Service供给务服的URL

    static final String SERVICE_URL = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx";

    /**

    * 获得省的列表

    * @return

    */

    public static List<String> getProvinceList() {

    // 用调的法方

    final String methodName = "getRegionProvince";

    // 建创HttpTransportSE传输对象

    final HttpTransportSE ht = new HttpTransportSE(SERVICE_URL);

    ht.debug = true;

    // 应用SOAP1.1议协建创Envelop对象

    final SoapSerializationEnvelope envelope =

    new SoapSerializationEnvelope(SoapEnvelope.VER11);

    // 实例化SoapObject对象

    SoapObject soapObject = new SoapObject(SERVICE_NS, methodName);

    envelope.bodyOut = soapObject;

    // 设置与.Net供给的Web Service持保较好的兼容性

    envelope.dotNet = true;

    FutureTask<List<String>> task = new FutureTask<List<String>>(

    new Callable<List<String>>(){

    @Override

    public List<String> call()throws Exception{

    // 用调Web Service

    ht.call(SERVICE_NS + methodName, envelope);

    if (envelope.getResponse() != null){

    // 获得务服器响应返回的SOAP消息

    SoapObject result = (SoapObject) envelope.bodyIn;

    SoapObject detail = (SoapObject) result.getProperty(

    methodName + "Result");

    // 析解务服器响应的SOAP消息。

    return parseProvinceOrCity(detail);

    }

    return null;

    }

    });

    new Thread(task).start();

    try{

    return task.get();

    }

    catch (Exception e){

    e.printStackTrace();

    }

    return null;

    }

    /**

    * 根据省获得市列表

    * @param province

    * @return

    */

    public static List<String> getCityListByProvince(String province) {

    // 用调的法方

    final String methodName = "getSupportCityString";

    // 建创HttpTransportSE传输对象

    final HttpTransportSE ht = new HttpTransportSE(SERVICE_URL);

    ht.debug = true;

    // 实例化SoapObject对象

    SoapObject soapObject = new SoapObject(SERVICE_NS, methodName);

    // 添加一个请求参数

    soapObject.addProperty("theRegionCode", province);

    // 应用SOAP1.1议协建创Envelop对象

    final SoapSerializationEnvelope envelope =

    每日一道理
成功的花朵开放在啊勤劳的枝头,失败的苦果孕育在懒惰的温床之中。

    new SoapSerializationEnvelope(SoapEnvelope.VER11);

    envelope.bodyOut = soapObject;

    // 设置与.Net供给的Web Service持保较好的兼容性

    envelope.dotNet = true;

    FutureTask<List<String>> task = new FutureTask<List<String>>(

    new Callable<List<String>>(){

    @Override

    public List<String> call() 

    throws Exception

    {

    // 用调Web Service

    ht.call(SERVICE_NS + methodName, envelope);

    if (envelope.getResponse() != null)

    {

    // 获得务服器响应返回的SOAP消息

    SoapObject result = (SoapObject) envelope.bodyIn;

    SoapObject detail = (SoapObject) result.getProperty(

    methodName + "Result");

    // 析解务服器响应的SOAP消息。

    return parseProvinceOrCity(detail);

    }

    return null;

    }

    });

    new Thread(task).start();

    try

    {

    return task.get();

    }

    catch (Exception e)

    {

    e.printStackTrace();

    }

    return null;

    }

    /**

    * 获得气天

    * @param cityName

    * @return

    */

    public static SoapObject getWeatherByCity(String cityName){

    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);

    envelope.bodyOut = soapObject;

    // 设置与.Net供给的Web Service持保较好的兼容性

    envelope.dotNet = true;

    FutureTask<SoapObject> task = new FutureTask<SoapObject>(

    new Callable<SoapObject>(){

    @Override

    public SoapObject call()throws Exception{

    ht.call(SERVICE_NS + methodName, envelope);

    SoapObject result = (SoapObject) envelope.bodyIn;

    SoapObject detail = (SoapObject) result.getProperty(

    methodName + "Result");

    return detail;

    }

    });

    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;

    }

文章结束给大家分享下程序员的一些笑话语录: 一位程序员去海边游泳,由于水性不佳,游不回岸了,于是他挥着手臂,大声求.救:“F1,F1!”

你可能感兴趣的:(WebServices)