Web Service
Web Service简介
Web Service用于消除不同平台、不同语言之间的实现差异,将现有的应用程序发布成开放式服务,从而允许互联网上的任何地方、任何平台、任何语言的应用程序来访问该服务。
通过ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar
一、
1、定义Web Service的命名空间
String SERVICE_NS = "http://WebXml.com.cn/";
2、定义Web Service提供服务的URL
String SERVICE_URL = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
3、设置调用的方法
String methodName = "getMobileCodeInfo";
4、创建HttpTransportSE传输对象
HttpTransportSE ht = new HttpTransportSE(SERVICE_URL);
ht.debug = true;
5、使用SOAP1.1协议创建Envelop对象
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
6、实例化SoapObject对象
SoapObject soapObject = new SoapObject(SERVICE_NS, methodName);
7、添加一个请求参数
soapObject.addProperty("mobileCode",mobile.getText().toString());
envelope.bodyOut = soapObject;
8、设置与.Net提供的Web Service保持较好的兼容性
envelope.dotNet = true;
9、调用Web Service
ht.call(SERVICE_NS + methodName, envelope);
10、获取服务器响应返回的SOAP消息
if (envelope.getResponse() != null) {
// 获取服务器响应返回的SOAP消息
SoapObject result = (SoapObject) envelope.bodyIn;
//解析服务器响应的SOAP消息。
return result.getProperty(0).toString();
}
11、解析服务器响应的SOAP消息
result.getProperty(0).toString();
例:
package com.example.haoma;
import java.io.IOException;
private String getServiceMobile(){
// 定义Web Service的命名空间
String SERVICE_NS = "http://WebXml.com.cn/";
// 定义Web Service提供服务的URL
String SERVICE_URL = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
// 调用的方法
String methodName = "getMobileCodeInfo";
// 创建HttpTransportSE传输对象
HttpTransportSE ht = new HttpTransportSE(SERVICE_URL);
ht.debug = true;
// 使用SOAP1.1协议创建Envelop对象
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
// 实例化SoapObject对象
SoapObject soapObject = new SoapObject(SERVICE_NS, methodName);
// 添加一个请求参数
soapObject.addProperty("mobileCode",mobile.getText().toString());
//soapObject.addProperty("userID","");
envelope.bodyOut = soapObject;
// 设置与.Net提供的Web Service保持较好的兼容性
envelope.dotNet = true;
try {
// 调用Web Service
ht.call(SERVICE_NS + methodName, envelope);
if (envelope.getResponse() != null) {
// 获取服务器响应返回的SOAP消息
SoapObject result = (SoapObject) envelope.bodyIn;
// SoapObject detail = (SoapObject) result.getProperty(methodName + "Result");
// 解析服务器响应的SOAP消息。
return result.getProperty(0).toString();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(getApplicationContext(), "shucuo", 1).show();
}catch (XmlPullParserException e) {
// TODO: handle exception
e.printStackTrace();
}
return null;
}
自己实现XML的解析等操作
1、 拷贝Web Service中的SOAP
package com.example.webserviceweather;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.xmlpull.v1.XmlPullParser;
import android.util.Xml;
public class AdressService { //获取手机号归属地
//返回手机号对应的归属地
public static String getAddress(String mobile)throws Exception{
String soap = readSoap();
//替换掉mobile
soap = soap.replaceAll("\\$mobile", mobile);
//得到字节数据
byte[] entity = soap.getBytes();
// 确定要请求的Web Service API所在的路径
String path ="http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
//包装成一个URL对象
URL url = new URL(path);
//打开一个HTTP协议的
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
conn.setRequestProperty("Content-Length", String.valueOf(entity.length));
conn.getOutputStream().write(entity);
if(conn.getResponseCode() == 200){
return parseSOAP(conn.getInputStream());
}
return null;
}
private static String parseSOAP(InputStream xml) throws Exception{
XmlPullParser pullParser = Xml.newPullParser();
pullParser.setInput(xml,"UTF-8");
int event = pullParser.getEventType();
while(event != XmlPullParser.END_DOCUMENT){
switch(event){
case XmlPullParser.START_TAG:
if("getMobileCodeInfoResult".equals(pullParser.getName())){
return pullParser.nextText();
}
break;
}
event = pullParser.next();
}
return null;
}
//读取SOAP协议的内容
private static String readSoap() throws Exception {
InputStream inStream = AdressService.class.getClassLoader().getResourceAsStream("soap12.xml");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while( (len = inStream.read(buffer)) != -1){
outputStream.write(buffer, 0, len);
}
inStream.close();
byte[] data = outputStream.toByteArray();
return new String(data);
}
}