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();
上面这是一种方式,我感觉这种方式很简单,解析数据也比较简单,下面我介绍另外一种方式,是我之前经理用的,感觉比较繁琐,不过理解起来会比较容易,
首先我们要按照这几步执行:www.
/***
* 拼接请求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.\" xmlns:xsd=\"http://www.\" xmlns:soap=\"http://schemas.\">"
+ "<soap:Body>"
+ "<getWeatherbyCityName xmlns=\"http://\">"
+ "<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.);
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进行解析,这里就不再述说.www.
这种很像我们之前经常用的请求---响应,不过有点太麻烦了,不建议使用,第一种方式已经帮我们把能封装的都封装好了,所以会比较简单,总之只要能达到效果就ok,
最后说一个问题:也是纠结我好久了,就是解析xml问题,不知道有没有同学遇到过没:就是在你请求webservice获取到的是一个完整的RSS文件,也就是xml.如果用第一种方法,我想应该没有问题,但是如果用第二种,就会遇到个问题,也就是最外层是一个xml文件,但里面又包含了一个xml,这时候你用dom,sax,解析肯定出现bug,后来发现,因为这个就不是一个正确的xml文件。,所以我的解决办法,先String截取到里面的完整RSS文件,这样就可以进行解析。不过现在想想自己饶的太远了.