KSOAP2 访问WebService

private static final String serviceNameSpace = "http://WebXml.com.cn/";  //命名空间
 private static final String getSupportCity = "getSupportCity";  //将调用的WebSerice方法
 private static final String serviceURL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx"; //将访问的WSDL路径

    // 实例化SoapObject对象(如:信)
    SoapObject request = new SoapObject(serviceNameSpace,getSupportCity);

    //为SOAP对象添加参数

    request.addProperty("byProvinceName", "北京");

    // 获得序列化的Envelope(如:信封) (并指定SOAP版本SoapEnvelope.VER11)
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

    //设置要发送的SOAP对象(信)  

    envelope.setOutputSoapObject(request);  或  envelope.bodyOut = request;

    //网络传输

    envelope.dotNet = true;

    // 注册Envelope   (视乎不注册也可)
    (new MarshalBase64()).register(envelope);

    //设置传输对象,并传入要访问的WSDL路径(如:邮局)

    HttpTransportSE ht = new HttpTransportSE(serviceURL);   或 AndroidHttpTransport ht=new AndroidHttpTransport(serviceURL);

    //设置调试打开,在debug时 才可以看到发生的XML和收到的XML具体信息

    ht.debug = true;

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

    ht.call(serviceNameSpace + getSupportCity, envelope);

    //判断是否访问成功

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

         //获得返回的SOAP对象

         try {

          SoapObject sb =(SoapObject)envelope.bodyIn;

           //实现PULL解析对结果进行解析
           XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
           XmlPullParser pullParser = factory.newPullParser();
          StringReader reader = new StringReader(sb.getProperty(0).toString());
          pullParser.setInput(reader);
          int eventType = pullParser.getEventType();

          //开始解析
          while (eventType != XmlPullParser.END_DOCUMENT) {
                  switch (eventType) {
                      case XmlPullParser.TEXT:
                      System.out.println(pullParser.getText());
                      break;
                   }
               eventType = pullParser.next();
           }
      } catch (XmlPullParserException e1) {
       e1.printStackTrace();
      }

    }

你可能感兴趣的:(xml,webservice,String,网络,null,SOAP)