调用webservice总计

 

调用webservice总结:
  1.插手第三着的jar包 Ksoap2-android-XXX 
 
2.拜响应的webservice的网站,查看响应的音讯,得到nameSpace,methodName,url,soapAction
  3.若request音信还有带有SoapHander的。那么就使包:依据参数封装
                Element[] header = new Element[1];
                header[0] = new Element().createElement(nameSpace,
“SoapHeader”);
               
                Element userName = new
Element().createElement(nameSpace, “UserID”);
                userName.addChild(Node.TEXT, UserID);
                header[0].addChild(Node.ELEMENT, userName);
               
                Element pass = new Element().createElement(nameSpace,
“PassWord”);
                pass.addChild(Node.TEXT, PassWord);
                header[0].addChild(Node.ELEMENT, pass);
  4.封装request信息的SoapBody
                                 //
指定Web瑟维斯(Service)的命名空间和调用的法门名
                                SoapObject  soapObject=new
SoapObject(nameSpace, methodName);
                                //处理soap12:Body数据有
                               
soapObject.addProperty(“loginName”,username);
                               
soapObject.addProperty(“password”,password);
   5.指定SoapSerializationEnvelope信息
                SoapSerializationEnvelope   envelope=new
SoapSerializationEnvelope(SoapEnvelope.VER11);
                                //SoapEnvelope.VER11
表示用的soap协议的本子号 1.1 或者是1.2
                envelope.headerOut=header;
                envelope.bodyOut=soapObject;
                envelope.dotNet = true;
//指定webservice的类的(java,PHP,dotNet)
                envelope.setOutputSoapObject(soapObject);
        6.指定HttpTransportSE
                    HttpTransportSE ht = new HttpTransportSE(url);
        7.访问webservice服务器
                    ht.call(soapAction, envelope);
        8.点儿种情势拿到服务器重回的信息
                                envelope.getResponse();
                                       
                            envelope.bodyIn;
                               
两者的界别:Webservice开发之早晚一般情状下我们接受webservice服务器再次回到值的时光都是用
                            SoapObject soapObject = (SoapObject)
envelope.getResponse();这多少个来受重临
                                                       
来的值,但这种办法往往会有java.lang.ClassCastException: org.ksoap2.
                                                       
serialization.SoapPrimitive这样的左。
                                                       
在服务器端重返值是String类型的数值的上用SoapObject soapObject =
(SoapObject)
                           
envelope.getResponse()会产生java.lang.ClassCastException: org.ksoap2.
                                                       
serialization.SoapPrimitive这样的错。
                                                        使用SoapObject
result = (SoapObject)envelope.bodyIn和 Object object =
                            envelope.getResponse();就可化解这种不当。
假如服务器再次来到值的色是byte[] 的时候,
                                                        使用Object
object = envelope.getResponse();和SoapObject result =
(SoapObject)envelope.bodyIn;
                            都未谋面来误现象,然而在利用Object object
= envelope.getResponse();
                           
取回来的价在动用base64举行解码和编码的时光会报发出荒唐。假如以SoapObject
result = (SoapObject)envelope.bodyIn;
                           
就可全部的将byte[]进展解码和编码,byte[] ops =
Base64.decode(result.getProperty(0).toString());
                SoapObject result=(SoapObject) envelope.bodyIn;
                String str=result.getProperty(0).toString();
                或者是
           Object  result=        (Object)
reqVo.envelope.getResponse();
       String str=result.toString();
        9.解析字符串str获取客户端想使的信息 

 

 

摘自:http://jnshilang.iteye.com/blog/1628218

相关文章