android 调用webservice(两种方法)

   首先介绍下网上常用的webservice调用方法,例子很多,我就不详细介绍了,简单说下流程:


// 创建soapObject对象,参数为命名空间和调用方法名,也就是soap_action. 这个可以在WSDL中获取.
SoapObject object = new SoapObject(NAMESPACE, METHOD_NAME);
object.addProperty("theCityName", cityName);// 设置属性

  如果属性过多则一一设置.

// 根据版本号创建SoapSerializationEnvelope
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.bodyOut = object;
envelope.dotNet = true;
envelope.setOutputSoapObject(object);


 

//创建HttpTransportSE 
HttpTransportSE httpTransportSE = new HttpTransportSE(URL);
try {
httpTransportSE.call(SOAP_ACTION, envelope);


      //soapObject = (SoapObject) envelope.getResponse();(1)

soapObject = (SoapObject) envelope.bodyIn;(2)

                         上面两中效果都可以获取soapObject对象,但是获取的不一样.

                          第一个获取到的是除去根节点,第二个则没有除去根节点,不过可以这样处理

                      soapObject = (SoapObject) envelope.bodyIn;

                     (SoapObject)soapObject.getProperty(0);//这时候获取到的对象相当于1步获取到的对象。


} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}

SoapObject object2=(SoapObject)soapObject.getProperty(0);
String ss = object2.getProperty(5).toString();
Log.v(tag, ss);
return object2.getProperty(5).toString();

          

           上面这是一种方式,我感觉这种方式很简单,解析数据也比较简单,下面我介绍另外一种方式,是我之前经理用的,感觉比较繁琐,不过理解起来会比较容易,

           首先我们要按照这几步执行:

/***
* 拼接请求webservice
*     
* @return
*/
private static StringEntity getEntity(String thecity) {
StringEntity entity = null;

                //可以参照WSDL...
String str = String
.format("<?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>"
+ "<getWeatherbyCityName xmlns=\"http://WebXml.com.cn/\">"
+ "<theCityName>%1$s</theCityName>"
+ "</getWeatherbyCityName>" + "</soap:Body>"
+ "</soap:Envelope>", thecity);
try {
entity = new StringEntity(str);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return entity;
}


    /***
* webservice 请求
*/
public static HttpPost httpPost(StringEntity entity, String SOAP_ACTION) {


HttpPost request = new HttpPost(URL);
request.addHeader("SOAPAction", SOAP_ACTION);
request.addHeader("Host","www.webxml.com.cn");
request.addHeader("Content-Type", "text/xml; charset=utf-8");
request.setEntity(entity);
return request;
}


/***
* 获取响应webservice HttpResponse
*/
public static HttpResponse getHttpResponse(HttpPost httpPost) {
HttpClient client = new DefaultHttpClient();
client.getParams()
.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,
TimeoutBySecond * 1000);
HttpResponse httpResponse = null;


try {
httpResponse = client.execute(httpPost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


return httpResponse;
}




     /***
* 获取内容 解析httpresponse
*/
public static String getContent(HttpResponse httpResponse) {


InputStream inputStream;
BufferedReader in = null;
StringBuffer sb = new StringBuffer();
try {
inputStream = httpResponse.getEntity().getContent();


in = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));


String inputLine;
while ((inputLine = in.readLine()) != null) {
sb.append(inputLine);
sb.append("\n");// 换行
}
in.close();


} catch (IllegalStateException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}


return sb.toString();
}


       最后我们获取到的字符串是一个完整的xml文件,所以我们可以根据dom,sax,pull进行解析,这里就不再述说.

       这种很像我们之前经常用的请求---响应,不过有点太麻烦了,不建议使用,第一种方式已经帮我们把能封装的都封装好了,所以会比较简单,总之只要能达到效果就ok,

        最后说一个问题:也是纠结我好久了,就是解析xml问题,不知道有没有同学遇到过没:就是在你请求webservice获取到的是一个完整的RSS文件,也就是xml.如果用第一种方法,我想应该没有问题,但是如果用第二种,就会遇到个问题,也就是最外层是一个xml文件,但里面又包含了一个xml,这时候你用dom,sax,解析肯定出现bug,后来发现,因为这个就不是一个正确的xml文件。,所以我的解决办法,先String截取到里面的完整RSS文件,这样就可以进行解析。不过现在想想自己饶的太远了.

       

 

    demo下载

        

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值