android WebService

mac2022-06-30  35

适合手机的WebService客户端SDK   KSOAP2:

http://code.google.com/p/ksoap2-android/downloads/list

http://files.cnblogs.com/xiaobuild/KSOAP2_SDK.rar

将下载后的jar文件复制到工程的lib目录中(如果没有该目录,可以新建一个,当然,也可以放在其他的目录中)。并在工程中引用这个jar包

如何调用WebService

打开webServic链接,如http://macau.sanvio.net/ReadOrderWebService/wsReadOrder.asmx?op=GetNews

POST /ReadOrderWebService/wsReadOrder.asmx HTTP/1.1Host: macau.sanvio.netContent-Type: text/xml; charset=utf-8Content-Length: lengthSOAPAction: "http://tempuri.org/GetNews"<?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><GetNews xmlns="http://tempuri.org/"><newsid>string</newsid></GetNews></soap:Body></soap:Envelope>

根据该xml确定命名空间  ,调用方法, webServer地址调用参数等

// 命名空间 private static final String ServiceNameSpace = "http://tempuri.org/";// webServer地址 private static final String webServiceUrl = "http://macau.sanvio.net/ReadOrderWebService/wsReadOrder.asmx";//webServer调用方法名 private static final String GetNews = "GetNews";

具体代码如下:

// 实例化SoapObject对象 SoapObject request = new SoapObject(ServiceNameSpace, GetNews);// 有参数的话,设置调用方法参数 request.addProperty("参数名称","参数值"); request.addProperty("newsid", "0001");// 获得序列化的Envelope 注册Envelope SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); envelope.dotNet = true; envelope.setOutputSoapObject(request);// envelope.bodyOut=request;// HttpTransportSE传输对象 HttpTransportSE transport = new HttpTransportSE(webServiceUrl); transport.debug = true;// 调用 try { transport.call(ServiceNameSpace + GetNews, envelope);if (envelope.getResponse() != null) { System.out.println("Get Info is finisth!");// System.out.println(envelope.bodyIn.toString());// parseNews(envelope.bodyIn.toString());//write(envelope.bodyIn.toString(), "001.txt");               //解析xml parseNews(envelope); } } catch (IOException e) {// TODO Auto-generated catch block e.printStackTrace(); } catch (XmlPullParserException e) {// TODO Auto-generated catch block e.printStackTrace(); }

一层一层遍历信息:

/** * * SoapSerializationEnvelope准换为SoapObject 遍历SoapObject 解析xml * * @param envelope * @throws IOException*/public void parseNews(SoapSerializationEnvelope envelope) { strResult = ""; SoapObject soapObject = (SoapObject) envelope.bodyIn;for (int i = 0; i < soapObject.getPropertyCount(); i++) { SoapObject soapChilds = (SoapObject) soapObject.getProperty(i);for (int j = 1; j < soapChilds.getPropertyCount(); j++) { SoapObject soapChilds2 = (SoapObject) soapChilds.getProperty(j);for (int k = 0; k < soapChilds2.getPropertyCount(); k++) { SoapObject soapChilds3 = (SoapObject) soapChilds2 .getProperty(k);for (int l = 0; l < soapChilds3.getPropertyCount(); l++) { SoapObject soapChilds4 = (SoapObject) soapChilds3 .getProperty(l); strResult += soapChilds4.getPropertyCount()+" "+soapChilds4.getAttributeCount()+ "\n========================================\n";for (int n = 0; n<soapChilds4.getPropertyCount(); n++) { strResult += "\n"+soapChilds4.getProperty(n); } } } } } showTextView.setText(strResult); }

转载于:https://www.cnblogs.com/xiaobuild/archive/2011/08/30/2159638.html

相关资源:android webservice 教材,包含几十个文件
最新回复(0)