首先要使用soap与服务器通信,需要下载KSOAP包:ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar包(或者版本更高的)。

接着,先整体感受下如何调用服务的:

public String getTaskSoap(String userCode, String taskType, String userType) {// 命名空间String nameSpace = "vaecer";// 调用方法的名称String methodName = "getTaskList";String endPoint = "http://asddsyws/services/TaskService.TaskServiceHttpSoap11Endpoint/";// SOAP ActionString soapAction = "http://asddsyws/services/TaskService/";// 指定WebService的命名空间和调用方法SoapObject soapObject = new SoapObject(nameSpace, methodName);// 设置需要调用WebService接口的参数soapObject.addProperty("taskType", taskType);soapObject.addProperty("type", userType);soapObject.addProperty("clerkNo", userCode);// 生成调用WebService方法调用的soap信息,并且指定Soap版本SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);envelope.bodyOut = soapObject;// 是否调用DotNet开发的WebServiceenvelope.dotNet = true;envelope.setOutputSoapObject(soapObject);HttpTransportSE transport = new HttpTransportSE(endPoint);try {transport.call(soapAction, envelope);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (XmlPullParserException e) {// TODO Auto-generated catch blocke.printStackTrace();}// 获取返回的数据SoapObject object = (SoapObject) envelope.bodyIn;// 获取返回的结果String result = object.getProperty(0).toString();Log.i("soap", result);return result;}



整体感觉是还蛮简单的,但是参数一定要配对。接下来介绍下细节(参考大神的文章):

第一:实例化SoapObject 对象,指定webService的命名空间(从相关WSDL文档中可以查看命名空间),以及调用方法名称。如:

//命名空间    private static final String serviceNameSpace="http://WebXml.com.cn/";    //调用方法(获得支持的城市)    private static final String getSupportCity="getSupportCity";//实例化SoapObject对象        SoapObject request=new SoapObject(serviceNameSpace, getSupportCity);

第二步:假设方法有参数的话,设置调用方法参数

request.addProperty("参数名称","参数值");

第三步:设置SOAP请求信息(参数部分为SOAP协议版本号,与你要调用的webService中版本号一致):

//获得序列化的Envelope        SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);        envelope.bodyOut=request;

第四步:注册Envelope,

(new MarshalBase64()).register(envelope);

第五步:构建传输对象,并指明WSDL文档URL:

//请求URL    private static final String serviceURL="http://www.webxml.com.cn/webservices/weatherwebservice.asmx";//Android传输对象        AndroidHttpTransport transport=new AndroidHttpTransport(serviceURL);        transport.debug=true;

第六步:调用WebService(其中参数为1:命名空间+方法名称,2:Envelope对象):

transport.call(serviceNameSpace+getWeatherbyCityName, envelope);
第七步:解析返回数据:

if(envelope.getResponse()!=null){                return parse(envelope.bodyIn.toString());            }/**************     * 解析XML     * @param str     * @return     */    private static List<String> parse(String str){        String temp;        List<String> list=new ArrayList<String>();        if(str!=null && str.length()>0){            int start=str.indexOf("string");            int end=str.lastIndexOf(";");            temp=str.substring(start, end-3);            String []test=temp.split(";");                         for(int i=0;i<test.length;i++){                 if(i==0){                     temp=test[i].substring(7);                 }else{                     temp=test[i].substring(8);                 }                 int index=temp.indexOf(",");                 list.add(temp.substring(0, index));             }        }        return list;    }
总体的步骤都就介绍完全了。

另外,提供两个soap的服务接口:

a。天气服务:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx


b。电话号码归属服务:http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx



参考文献:http://www.cnblogs.com/zhangdongzi/archive/2011/04/19/2020688.html

更多相关文章

  1. 采用Scroller实现任意布局的上下左右弹性效果
  2. 【Android(安卓)开发教程】Spinner
  3. Android(安卓)数据库初窥
  4. android 发送短信和打电话的方法
  5. Android(安卓)逐帧动画isRunning 一直返回true的问题
  6. Android(安卓)Webkit简单用例
  7. 使用googleMap 需要获取的apikey方法
  8. 【Android(安卓)开发教程】使用Intent发送短信
  9. Android将“.c”后缀名改为“.cpp”时java调用native失败及“err

随机推荐

  1. Android(安卓)设置EditText光标位置、粗
  2. Android(安卓)利用Annotation Processing
  3. android 双卡手机发短信/判断手机是否为
  4. Android(安卓)OpenGL入门
  5. Android中style和theme的区别
  6. (转)Android(安卓)7种CPU架构 介绍
  7. Android(安卓)获取系统或SDCARD剩余空间
  8. Android(安卓)GridView的使用
  9. Android(安卓)Studio 上local path doesn
  10. Android(安卓)SDK开发报错NoClassDefFoun