Android调用web Service

---恢复内容开始---

继总结过web service后,再总结一下android中使用web service。

在android中使用web service需要使用到一个第三方开源的类库——ksoap2。他是一个SOAP Web Service客户端开发包。是android平台上高效、轻量级的SOAP开发包。

使用SOAP需要引入第三方的jar包:ksoap2-android-assembly-2.5.4-jar-with-dependencies.jar。如何加入jar包就不在赘述。下面讲一下soap使用web service的步骤。

调用web service的步骤:

1、实例化SOAP对象,指定soap的命名空间。

//目标域名空间(即提供Web Service服务的域名空间)
    private static final String TARGETNAMESPACE = "http://WebXml.com.cn/";
//需要调用的方法名(获得本天气预报Web Services支持的洲、国内外省份和城市信息)
    private static final String GETSUPPORTPROVINCE = "getSupportProvince";
//实例化SOAP对象,指定命名空间和调用的方法
    SoapObject soapObject = new SoapObject(TARGETNAMESPACE,GETSUPPORTPROVINCE);

2、如果方法有参数,添加参数及参数值。

//设置方法需要使用的参数,以及参数值
        soapObject.addProperty("theRegionCode",province);

3、设置SOAP请求信息,参数为SOAP的版本(版本只能低于服务端指定的版本)。

//使用SOAP1.1协议创建Envelop对象
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
//设置与.Dot提供的Web Service服务保持良好的兼容性
    envelope.dotNet = true;
//将初始化的soapObject放入(序列化)使用SOAP1.1协议创建Envelop对象中
    envelope.setOutputSoapObject(soapObject);

4、构建传输对象,指定WSDL文档中给定的URL。

//WSDL文档中给定的URL
    private static final String WSDL      = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx";
//构建传输对象,指定URL
    HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL);
//AndroidHttpTransport transport=new AndroidHttpTransport(serviceURL);
// transport.debug=true;

5、调用web service(参数1:命名空间+方法名;参数2:envelop对象)

httpTransportSE.call(TARGETNAMESPACE+GETSUPPORTPROVINCE,soapSerializationEnvelope);

6、处理返回结果

SoapObject result = (SoapObject) soapSerializationEnvelope.getResponse();
                int count = result.getPropertyCount();
                for (int i = 0;i < count;i++){
                    privinces.add(result.getProperty(i).toString());
                    System.out.println(result.getProperty(i).toString());
                }

以下就是本人写的调用天气预报服务的代码:

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 java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangyan on 2016/1/17.
 */
public class WebServiceHelper {

    //目标域名空间(即提供Web Service服务的域名空间)
    private static final String TARGETNAMESPACE = "http://WebXml.com.cn/";
    //WSDL文档中给定的URL
    private static final String WSDL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx";
    //需要调用的方法名(获得本天气预报Web Services支持的洲、国内外省份和城市信息)
    private static final String GETSUPPORTPROVINCE = "getSupportProvince";
    //需要调用的方法名(获得本天气预报Web Services支持的城市信息,根据省份查询城市集合:带参数)
    private static final String GETSUPPORTCITY = "getSupportCity";
    //根据城市或地区名称查询获得未来三天内天气情况、现在的天气实况、天气和生活指数
    private static final String GETWEATHERBYCITYNAME = "getWeatherbyCityName";
    /**
     * 获得洲、国内外省份和某些城市
     * @return
     */
        public List getProvince(){
            List<String> privinces = new ArrayList<String>();
            //实例化SOAP对象,指定命名空间和调用的方法
            SoapObject soapObject = new SoapObject(TARGETNAMESPACE,GETSUPPORTPROVINCE);
            //使用SOAP1.1协议创建Envelop对象
            SoapSerializationEnvelope soapSerializationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
            //设置与.Dot提供的Web Service服务保持良好的兼容性
            soapSerializationEnvelope.dotNet = true;
            //将初始化的soapObject放入(序列化)使用SOAP1.1协议创建Envelop对象中
            soapSerializationEnvelope.setOutputSoapObject(soapObject);
            //构建传输对象,指定URL
            HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL);
            try {
                httpTransportSE.call(TARGETNAMESPACE+GETSUPPORTPROVINCE,soapSerializationEnvelope);
                SoapObject result = (SoapObject) soapSerializationEnvelope.getResponse();
                int count = result.getPropertyCount();
                for (int i = 0;i < count;i++){
                    privinces.add(result.getProperty(i).toString());
                    System.out.println(result.getProperty(i).toString());
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (XmlPullParserException e) {
                e.printStackTrace();
            }

            return privinces;
        }

    /**
     * 根据省得到城市
     * @param province
     * @return
     */
    public List<String> getCitysByProvince(String province){
        List<String> citylist = new ArrayList<String>();

        SoapObject soapObject = new SoapObject(TARGETNAMESPACE,GETSUPPORTCITY);
        //设置方法需要使用的参数,以及参数值
        soapObject.addProperty("theRegionCode",province);
        SoapSerializationEnvelope soapSerializationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
        soapSerializationEnvelope.dotNet = true;
        soapSerializationEnvelope.setOutputSoapObject(soapObject);

        HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL);
        try {
            httpTransportSE.call(TARGETNAMESPACE + GETSUPPORTCITY,soapSerializationEnvelope);
            SoapObject result = (SoapObject) soapSerializationEnvelope.getResponse();
            int count = result.getPropertyCount();
            if (result != null && count > 0){
                for (int i = 0; i < count; i++){
                    citylist.add(result.getProperty(i).toString());
                    System.out.println(result.getProperty(i).toString());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
        return citylist;
    }

    /**
     * 根据城市获得天气信息
     * @param city
     * @return
     */
    public List<String> getWeatherByCity(String city){
        List<String> weatherlist = new ArrayList<String>();

        SoapObject soapObject = new SoapObject(TARGETNAMESPACE,GETWEATHERBYCITYNAME);
        soapObject.addProperty("theCityName", city);
        SoapSerializationEnvelope soapSerializationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
        soapSerializationEnvelope.dotNet = true;
        soapSerializationEnvelope.setOutputSoapObject(soapObject);

        HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL);
        try {
            httpTransportSE.call(TARGETNAMESPACE + GETWEATHERBYCITYNAME, soapSerializationEnvelope);
            SoapObject result = (SoapObject) soapSerializationEnvelope.getResponse();
            if (result != null){
                int count = result.getPropertyCount();
                for (int i = 0; i < count;i++){
                    //可以用天气类来更好的处理得到的天气信息
                    weatherlist.add(result.getProperty(i).toString());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
        return weatherlist;
    }
}

---恢复内容结束---

继总结过web service后,再总结一下android中使用web service。

在android中使用web service需要使用到一个第三方开源的类库——ksoap2。他是一个SOAP Web Service客户端开发包。是android平台上高效、轻量级的SOAP开发包。

使用SOAP需要引入第三方的jar包:ksoap2-android-assembly-2.5.4-jar-with-dependencies.jar。如何加入jar包就不在赘述。下面讲一下soap使用web service的步骤。

调用web service的步骤:

1、实例化SOAP对象,指定soap的命名空间。

//目标域名空间(即提供Web Service服务的域名空间)
    private static final String TARGETNAMESPACE = "http://WebXml.com.cn/";
//需要调用的方法名(获得本天气预报Web Services支持的洲、国内外省份和城市信息)
    private static final String GETSUPPORTPROVINCE = "getSupportProvince";
//实例化SOAP对象,指定命名空间和调用的方法
    SoapObject soapObject = new SoapObject(TARGETNAMESPACE,GETSUPPORTPROVINCE);

2、如果方法有参数,添加参数及参数值。

//设置方法需要使用的参数,以及参数值
        soapObject.addProperty("theRegionCode",province);

3、设置SOAP请求信息,参数为SOAP的版本(版本只能低于服务端指定的版本)。

//使用SOAP1.1协议创建Envelop对象
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
//设置与.Dot提供的Web Service服务保持良好的兼容性
    envelope.dotNet = true;
//将初始化的soapObject放入(序列化)使用SOAP1.1协议创建Envelop对象中
    envelope.setOutputSoapObject(soapObject);

4、构建传输对象,指定WSDL文档中给定的URL。

//WSDL文档中给定的URL
    private static final String WSDL      = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx";
//构建传输对象,指定URL
    HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL);
//AndroidHttpTransport transport=new AndroidHttpTransport(serviceURL);
// transport.debug=true;

5、调用web service(参数1:命名空间+方法名;参数2:envelop对象)

httpTransportSE.call(TARGETNAMESPACE+GETSUPPORTPROVINCE,soapSerializationEnvelope);

6、处理返回结果

SoapObject result = (SoapObject) soapSerializationEnvelope.getResponse();
                int count = result.getPropertyCount();
                for (int i = 0;i < count;i++){
                    privinces.add(result.getProperty(i).toString());
                    System.out.println(result.getProperty(i).toString());
                }

以下就是本人写的调用天气预报服务的代码:

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 java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangyan on 2016/1/17.
 */
public class WebServiceHelper {

    //目标域名空间(即提供Web Service服务的域名空间)
    private static final String TARGETNAMESPACE = "http://WebXml.com.cn/";
    //WSDL文档中给定的URL
    private static final String WSDL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx";
    //需要调用的方法名(获得本天气预报Web Services支持的洲、国内外省份和城市信息)
    private static final String GETSUPPORTPROVINCE = "getSupportProvince";
    //需要调用的方法名(获得本天气预报Web Services支持的城市信息,根据省份查询城市集合:带参数)
    private static final String GETSUPPORTCITY = "getSupportCity";
    //根据城市或地区名称查询获得未来三天内天气情况、现在的天气实况、天气和生活指数
    private static final String GETWEATHERBYCITYNAME = "getWeatherbyCityName";
    /**
     * 获得洲、国内外省份和某些城市
     * @return
     */
        public List getProvince(){
            List<String> privinces = new ArrayList<String>();
            //实例化SOAP对象,指定命名空间和调用的方法
            SoapObject soapObject = new SoapObject(TARGETNAMESPACE,GETSUPPORTPROVINCE);
            //使用SOAP1.1协议创建Envelop对象
            SoapSerializationEnvelope soapSerializationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
            //设置与.Dot提供的Web Service服务保持良好的兼容性
            soapSerializationEnvelope.dotNet = true;
            //将初始化的soapObject放入(序列化)使用SOAP1.1协议创建Envelop对象中
            soapSerializationEnvelope.setOutputSoapObject(soapObject);
            //构建传输对象,指定URL
            HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL);
            try {
                httpTransportSE.call(TARGETNAMESPACE+GETSUPPORTPROVINCE,soapSerializationEnvelope);
                SoapObject result = (SoapObject) soapSerializationEnvelope.getResponse();
                int count = result.getPropertyCount();
                for (int i = 0;i < count;i++){
                    privinces.add(result.getProperty(i).toString());
                    System.out.println(result.getProperty(i).toString());
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (XmlPullParserException e) {
                e.printStackTrace();
            }

            return privinces;
        }

    /**
     * 根据省得到城市
     * @param province
     * @return
     */
    public List<String> getCitysByProvince(String province){
        List<String> citylist = new ArrayList<String>();

        SoapObject soapObject = new SoapObject(TARGETNAMESPACE,GETSUPPORTCITY);
        //设置方法需要使用的参数,以及参数值
        soapObject.addProperty("theRegionCode",province);
        SoapSerializationEnvelope soapSerializationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
        soapSerializationEnvelope.dotNet = true;
        soapSerializationEnvelope.setOutputSoapObject(soapObject);

        HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL);
        try {
            httpTransportSE.call(TARGETNAMESPACE + GETSUPPORTCITY,soapSerializationEnvelope);
            SoapObject result = (SoapObject) soapSerializationEnvelope.getResponse();
            int count = result.getPropertyCount();
            if (result != null && count > 0){
                for (int i = 0; i < count; i++){
                    citylist.add(result.getProperty(i).toString());
                    System.out.println(result.getProperty(i).toString());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
        return citylist;
    }

    /**
     * 根据城市获得天气信息
     * @param city
     * @return
     */
    public List<String> getWeatherByCity(String city){
        List<String> weatherlist = new ArrayList<String>();

        SoapObject soapObject = new SoapObject(TARGETNAMESPACE,GETWEATHERBYCITYNAME);
        soapObject.addProperty("theCityName", city);
        SoapSerializationEnvelope soapSerializationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
        soapSerializationEnvelope.dotNet = true;
        soapSerializationEnvelope.setOutputSoapObject(soapObject);

        HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL);
        try {
            httpTransportSE.call(TARGETNAMESPACE + GETWEATHERBYCITYNAME, soapSerializationEnvelope);
            SoapObject result = (SoapObject) soapSerializationEnvelope.getResponse();
            if (result != null){
                int count = result.getPropertyCount();
                for (int i = 0; i < count;i++){
                    //可以用天气类来更好的处理得到的天气信息
                    weatherlist.add(result.getProperty(i).toString());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
        return weatherlist;
    }
}

---恢复内容开始---

继总结过web service后,再总结一下android中使用web service。

在android中使用web service需要使用到一个第三方开源的类库——ksoap2。他是一个SOAP Web Service客户端开发包。是android平台上高效、轻量级的SOAP开发包。

使用SOAP需要引入第三方的jar包:ksoap2-android-assembly-2.5.4-jar-with-dependencies.jar。如何加入jar包就不在赘述。下面讲一下soap使用web service的步骤。

调用web service的步骤:

1、实例化SOAP对象,指定soap的命名空间。

//目标域名空间(即提供Web Service服务的域名空间)
    private static final String TARGETNAMESPACE = "http://WebXml.com.cn/";
//需要调用的方法名(获得本天气预报Web Services支持的洲、国内外省份和城市信息)
    private static final String GETSUPPORTPROVINCE = "getSupportProvince";
//实例化SOAP对象,指定命名空间和调用的方法
    SoapObject soapObject = new SoapObject(TARGETNAMESPACE,GETSUPPORTPROVINCE);

2、如果方法有参数,添加参数及参数值。

//设置方法需要使用的参数,以及参数值
        soapObject.addProperty("theRegionCode",province);

3、设置SOAP请求信息,参数为SOAP的版本(版本只能低于服务端指定的版本)。

//使用SOAP1.1协议创建Envelop对象
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
//设置与.Dot提供的Web Service服务保持良好的兼容性
    envelope.dotNet = true;
//将初始化的soapObject放入(序列化)使用SOAP1.1协议创建Envelop对象中
    envelope.setOutputSoapObject(soapObject);

4、构建传输对象,指定WSDL文档中给定的URL。

//WSDL文档中给定的URL
    private static final String WSDL      = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx";
//构建传输对象,指定URL
    HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL);
//AndroidHttpTransport transport=new AndroidHttpTransport(serviceURL);
// transport.debug=true;

5、调用web service(参数1:命名空间+方法名;参数2:envelop对象)

httpTransportSE.call(TARGETNAMESPACE+GETSUPPORTPROVINCE,soapSerializationEnvelope);

6、处理返回结果

SoapObject result = (SoapObject) soapSerializationEnvelope.getResponse();
                int count = result.getPropertyCount();
                for (int i = 0;i < count;i++){
                    privinces.add(result.getProperty(i).toString());
                    System.out.println(result.getProperty(i).toString());
                }

以下就是本人写的调用天气预报服务的代码:

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 java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangyan on 2016/1/17.
 */
public class WebServiceHelper {

    //目标域名空间(即提供Web Service服务的域名空间)
    private static final String TARGETNAMESPACE = "http://WebXml.com.cn/";
    //WSDL文档中给定的URL
    private static final String WSDL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx";
    //需要调用的方法名(获得本天气预报Web Services支持的洲、国内外省份和城市信息)
    private static final String GETSUPPORTPROVINCE = "getSupportProvince";
    //需要调用的方法名(获得本天气预报Web Services支持的城市信息,根据省份查询城市集合:带参数)
    private static final String GETSUPPORTCITY = "getSupportCity";
    //根据城市或地区名称查询获得未来三天内天气情况、现在的天气实况、天气和生活指数
    private static final String GETWEATHERBYCITYNAME = "getWeatherbyCityName";
    /**
     * 获得洲、国内外省份和某些城市
     * @return
     */
        public List getProvince(){
            List<String> privinces = new ArrayList<String>();
            //实例化SOAP对象,指定命名空间和调用的方法
            SoapObject soapObject = new SoapObject(TARGETNAMESPACE,GETSUPPORTPROVINCE);
            //使用SOAP1.1协议创建Envelop对象
            SoapSerializationEnvelope soapSerializationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
            //设置与.Dot提供的Web Service服务保持良好的兼容性
            soapSerializationEnvelope.dotNet = true;
            //将初始化的soapObject放入(序列化)使用SOAP1.1协议创建Envelop对象中
            soapSerializationEnvelope.setOutputSoapObject(soapObject);
            //构建传输对象,指定URL
            HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL);
            try {
                httpTransportSE.call(TARGETNAMESPACE+GETSUPPORTPROVINCE,soapSerializationEnvelope);
                SoapObject result = (SoapObject) soapSerializationEnvelope.getResponse();
                int count = result.getPropertyCount();
                for (int i = 0;i < count;i++){
                    privinces.add(result.getProperty(i).toString());
                    System.out.println(result.getProperty(i).toString());
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (XmlPullParserException e) {
                e.printStackTrace();
            }

            return privinces;
        }

    /**
     * 根据省得到城市
     * @param province
     * @return
     */
    public List<String> getCitysByProvince(String province){
        List<String> citylist = new ArrayList<String>();

        SoapObject soapObject = new SoapObject(TARGETNAMESPACE,GETSUPPORTCITY);
        //设置方法需要使用的参数,以及参数值
        soapObject.addProperty("theRegionCode",province);
        SoapSerializationEnvelope soapSerializationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
        soapSerializationEnvelope.dotNet = true;
        soapSerializationEnvelope.setOutputSoapObject(soapObject);

        HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL);
        try {
            httpTransportSE.call(TARGETNAMESPACE + GETSUPPORTCITY,soapSerializationEnvelope);
            SoapObject result = (SoapObject) soapSerializationEnvelope.getResponse();
            int count = result.getPropertyCount();
            if (result != null && count > 0){
                for (int i = 0; i < count; i++){
                    citylist.add(result.getProperty(i).toString());
                    System.out.println(result.getProperty(i).toString());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
        return citylist;
    }

    /**
     * 根据城市获得天气信息
     * @param city
     * @return
     */
    public List<String> getWeatherByCity(String city){
        List<String> weatherlist = new ArrayList<String>();

        SoapObject soapObject = new SoapObject(TARGETNAMESPACE,GETWEATHERBYCITYNAME);
        soapObject.addProperty("theCityName", city);
        SoapSerializationEnvelope soapSerializationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
        soapSerializationEnvelope.dotNet = true;
        soapSerializationEnvelope.setOutputSoapObject(soapObject);

        HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL);
        try {
            httpTransportSE.call(TARGETNAMESPACE + GETWEATHERBYCITYNAME, soapSerializationEnvelope);
            SoapObject result = (SoapObject) soapSerializationEnvelope.getResponse();
            if (result != null){
                int count = result.getPropertyCount();
                for (int i = 0; i < count;i++){
                    //可以用天气类来更好的处理得到的天气信息
                    weatherlist.add(result.getProperty(i).toString());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
        return weatherlist;
    }
}

---恢复内容结束---

继总结过web service后,再总结一下android中使用web service。

在android中使用web service需要使用到一个第三方开源的类库——ksoap2。他是一个SOAP Web Service客户端开发包。是android平台上高效、轻量级的SOAP开发包。

使用SOAP需要引入第三方的jar包:ksoap2-android-assembly-2.5.4-jar-with-dependencies.jar。如何加入jar包就不在赘述。下面讲一下soap使用web service的步骤。

调用web service的步骤:

1、实例化SOAP对象,指定soap的命名空间。

//目标域名空间(即提供Web Service服务的域名空间)
    private static final String TARGETNAMESPACE = "http://WebXml.com.cn/";
//需要调用的方法名(获得本天气预报Web Services支持的洲、国内外省份和城市信息)
    private static final String GETSUPPORTPROVINCE = "getSupportProvince";
//实例化SOAP对象,指定命名空间和调用的方法
    SoapObject soapObject = new SoapObject(TARGETNAMESPACE,GETSUPPORTPROVINCE);

2、如果方法有参数,添加参数及参数值。

//设置方法需要使用的参数,以及参数值
        soapObject.addProperty("theRegionCode",province);

3、设置SOAP请求信息,参数为SOAP的版本(版本只能低于服务端指定的版本)。

//使用SOAP1.1协议创建Envelop对象
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
//设置与.Dot提供的Web Service服务保持良好的兼容性
    envelope.dotNet = true;
//将初始化的soapObject放入(序列化)使用SOAP1.1协议创建Envelop对象中
    envelope.setOutputSoapObject(soapObject);

4、构建传输对象,指定WSDL文档中给定的URL。

//WSDL文档中给定的URL
    private static final String WSDL      = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx";
//构建传输对象,指定URL
    HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL);
//AndroidHttpTransport transport=new AndroidHttpTransport(serviceURL);
// transport.debug=true;

5、调用web service(参数1:命名空间+方法名;参数2:envelop对象)

httpTransportSE.call(TARGETNAMESPACE+GETSUPPORTPROVINCE,soapSerializationEnvelope);

6、处理返回结果

SoapObject result = (SoapObject) soapSerializationEnvelope.getResponse();
                int count = result.getPropertyCount();
                for (int i = 0;i < count;i++){
                    privinces.add(result.getProperty(i).toString());
                    System.out.println(result.getProperty(i).toString());
                }

以下就是本人写的调用天气预报服务的代码:

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 java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangyan on 2016/1/17.
 */
public class WebServiceHelper {

    //目标域名空间(即提供Web Service服务的域名空间)
    private static final String TARGETNAMESPACE = "http://WebXml.com.cn/";
    //WSDL文档中给定的URL
    private static final String WSDL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx";
    //需要调用的方法名(获得本天气预报Web Services支持的洲、国内外省份和城市信息)
    private static final String GETSUPPORTPROVINCE = "getSupportProvince";
    //需要调用的方法名(获得本天气预报Web Services支持的城市信息,根据省份查询城市集合:带参数)
    private static final String GETSUPPORTCITY = "getSupportCity";
    //根据城市或地区名称查询获得未来三天内天气情况、现在的天气实况、天气和生活指数
    private static final String GETWEATHERBYCITYNAME = "getWeatherbyCityName";
    /**
     * 获得洲、国内外省份和某些城市
     * @return
     */
        public List getProvince(){
            List<String> privinces = new ArrayList<String>();
            //实例化SOAP对象,指定命名空间和调用的方法
            SoapObject soapObject = new SoapObject(TARGETNAMESPACE,GETSUPPORTPROVINCE);
            //使用SOAP1.1协议创建Envelop对象
            SoapSerializationEnvelope soapSerializationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
            //设置与.Dot提供的Web Service服务保持良好的兼容性
            soapSerializationEnvelope.dotNet = true;
            //将初始化的soapObject放入(序列化)使用SOAP1.1协议创建Envelop对象中
            soapSerializationEnvelope.setOutputSoapObject(soapObject);
            //构建传输对象,指定URL
            HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL);
            try {
                httpTransportSE.call(TARGETNAMESPACE+GETSUPPORTPROVINCE,soapSerializationEnvelope);
                SoapObject result = (SoapObject) soapSerializationEnvelope.getResponse();
                int count = result.getPropertyCount();
                for (int i = 0;i < count;i++){
                    privinces.add(result.getProperty(i).toString());
                    System.out.println(result.getProperty(i).toString());
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (XmlPullParserException e) {
                e.printStackTrace();
            }

            return privinces;
        }

    /**
     * 根据省得到城市
     * @param province
     * @return
     */
    public List<String> getCitysByProvince(String province){
        List<String> citylist = new ArrayList<String>();

        SoapObject soapObject = new SoapObject(TARGETNAMESPACE,GETSUPPORTCITY);
        //设置方法需要使用的参数,以及参数值
        soapObject.addProperty("theRegionCode",province);
        SoapSerializationEnvelope soapSerializationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
        soapSerializationEnvelope.dotNet = true;
        soapSerializationEnvelope.setOutputSoapObject(soapObject);

        HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL);
        try {
            httpTransportSE.call(TARGETNAMESPACE + GETSUPPORTCITY,soapSerializationEnvelope);
            SoapObject result = (SoapObject) soapSerializationEnvelope.getResponse();
            int count = result.getPropertyCount();
            if (result != null && count > 0){
                for (int i = 0; i < count; i++){
                    citylist.add(result.getProperty(i).toString());
                    System.out.println(result.getProperty(i).toString());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
        return citylist;
    }

    /**
     * 根据城市获得天气信息
     * @param city
     * @return
     */
    public List<String> getWeatherByCity(String city){
        List<String> weatherlist = new ArrayList<String>();

        SoapObject soapObject = new SoapObject(TARGETNAMESPACE,GETWEATHERBYCITYNAME);
        soapObject.addProperty("theCityName", city);
        SoapSerializationEnvelope soapSerializationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
        soapSerializationEnvelope.dotNet = true;
        soapSerializationEnvelope.setOutputSoapObject(soapObject);

        HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL);
        try {
            httpTransportSE.call(TARGETNAMESPACE + GETWEATHERBYCITYNAME, soapSerializationEnvelope);
            SoapObject result = (SoapObject) soapSerializationEnvelope.getResponse();
            if (result != null){
                int count = result.getPropertyCount();
                for (int i = 0; i < count;i++){
                    //可以用天气类来更好的处理得到的天气信息
                    weatherlist.add(result.getProperty(i).toString());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
        return weatherlist;
    }
}
时间: 2024-10-06 02:25:07

Android调用web Service的相关文章

Android平台调用Web Service:线程返回值

接上文 前文中的遗留问题 对于Java多线程的理解,我以前仅仅局限于实现Runnable接口或者继承Thread类,然后重写run()方法,最后start()调用就算完事,但是一旦涉及死锁以及对共享资源的访问和随时监控线程的状态和执行顺序和线程返回值等就不行了. Callable 和 Future 简介 Callable接口代表一段可以调用并返回结果的代码;Future接口表示是执行异步任务时的状态.返回值等信息.所以说Callable用于产生结果,Future用于获取结果. 1. Callab

Android平台调用Web Service:引入线程

接上文 遗留问题 MainActivity的onCreate方法中如果没有有这段代码: // 强制在UI线程中操作 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .detectDiskReads().detectDiskWrites().detectNetwork() .penaltyLog().build()); StrictMode.setVmPolicy(new StrictMode.VmPolicy.Bu

Android平台调用Web Service:演示样例

近期在学习Android,随着移动设备的流行,当软件走上商业化的道路.为了争夺市场,肯定须要支持Android的,所以開始接触了Android,只是仅仅了解皮毛就好,由于我们要做管理者嘛.懂点Android.管理起来easy些. Android学起来也简单,封装的更好了,一个个的控件,像是又回到了VB的赶脚. 以下将通过一个演示样例解说怎样在Android平台调用Web Service. 我们使用互联网现成的Webservice.供查询手机号码归属地的Web service,它的WSDL为htt

Android---56---Android应用调用Web Service/号码归属地查询

使用Android应用调用Web Service 需要工具: ksoap2-android 下载地址:http://pan.baidu.com/s/1jGL6b10 build path将ksoap2-android 添加到项目工程中 先将ksoap2-android 包导入 libs目录下,右键build path -> add to build path 点击项目工程名,右键 build path -> configure build path 在ksoap2-android这个包前面打上

通过ksoap2-android来调用Web Service操作的实例

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;

Java实现与调用Web Service

一. Web Service 1. web service 就是应用程序之间跨语言的调用 例如,天气预报Web Service:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx 2. wsdl: web service description language(web服务描述语言) 通过xml格式说明调用的地址方法如何调用,可以看作webservice的说明书 例如,天气预报的:http://www.webxml.com.cn

Android平台调用Web Service:示例

最近在学习Android,随着移动设备的流行,当软件走上商业化的道路,为了争夺市场,肯定需要支持Android的,所以开始接触了Android,不过只了解皮毛就好,因为我们要做管理者嘛,懂点Android,管理起来容易些. Android学起来也简单,封装的更好了,一个个的控件,像是又回到了VB的赶脚. 下面将通过一个示例讲解如何在Android平台调用Web Service.我们使用互联网现成的Webservice,供查询手机号码归属地的Web service,它的WSDL为http://we

VB.NET,C#.NET调用Web Service,利用visual studio 的实现方法

在VB.NET调用Web Service提供的服务 技术qq交流群:JavaDream:251572072 下面是一篇文章比较详细,其实具体操作很简单,把Web Service服务地址,利用工具(VS2010),通过添加引用的形式,添加到项目中来就可以应用了. 大家如果这个地方不会操场的话,可以问问我QQ:1606841559 当Web Service已经处于对外提供服务状态,VB.NET就可以通过HTTP"调用"来使用这些服务了.当然前提是要了解Web Service对外提供服务所对

SPServices介绍之三:使用SPSerivces对象调用Web Service

SPServices介绍之三:使用SPSerivces对象调用Web Service 分类: SPSerivces2014-09-21 04:23 304人阅读 评论(0) 收藏 举报 在上一篇文章SPServices介绍之二中,介绍了一些SPServices的基本方法,除了这些方法,SPServices的主要用处是使用Ajax方法调用SharePoint提供的Web Service. 先看一下语法: [javascript] view plaincopy $().SPServices({ op