调用webservice

利用webservice查询手机号码归属地
实现方法:
Android客户端可以通过在Http的基础上使用SOAP调用webservice服务器上的API


第一步:在src下新建一个xml文件.
(1)从网站http://WebXml.com.cn/找到需要的API
(2)把从网上拷贝的API即soap协议粘贴进刚才新建的xml文件
(3)注意:在xml中使用占位符$mobile来代替还未输入的手机号码
soap.xml文件如下:
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                 xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <getMobileCodeInfo xmlns="http://WebXml.com.cn/">
      <mobileCode>$mobile</mobileCode>
      <userID></userID>
    </getMobileCodeInfo>
  </soap12:Body>
</soap12:Envelope>

第二步:发送和解析SOAP协议
注意:
(1)在读取soap.xml文件后,要将其中的占位符替换成需要查询的手机号码.
   因为$是正则表达式的特殊字符所以需要用\转义,但是\也是Java中的特殊字符所以还要对\进转义.于是为\\$mobile
(2)传递给parseSOAP(InputStream xml)方法的参数是conn的输入流即parseSOAP(conn.getInputStream());

发送SOAP协议:
public class MobileService {
	public static String getAddress(String mobile) throws Exception {
		InputStream inStream = MobileService.class.getClassLoader().getResourceAsStream("soap.xml");
		byte[] data = StreamTool.read(inStream);//read(inStream)方法见下
		String content = new String(data, "UTF-8");
		String soap = content.replaceAll("\\$mobile", mobile);//替换占位符,形成真正的soap协议
		byte[] entity = soap.getBytes();		
		String path = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
		HttpURLConnection conn = (HttpURLConnection) new URL(path).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));
		OutputStream outStream = conn.getOutputStream();
		outStream.write(entity);
		if(conn.getResponseCode() == 200){
			return parseSOAP(conn.getInputStream());//参数为输入流
		}
		return null;
	}

webservice服务器返回的soap协议如下:
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                 xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
                 xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/">
      <getMobileCodeInfoResult>string</getMobileCodeInfoResult>
    </getMobileCodeInfoResponse>
  </soap12:Body>
</soap12:Envelope>
注意:
(1)<getMobileCodeInfoResult>string</getMobileCodeInfoResult>中的String是我们正需要得到的
(2)主要是利用XmlPullParser解析XML文件
解析SOAP协议:
private static String parseSOAP(InputStream xml) throws Exception{
		XmlPullParser parser = Xml.newPullParser();
		parser.setInput(xml, "UTF-8");
		int event = parser.getEventType();
		while(event != XmlPullParser.END_DOCUMENT){
			switch (event) {
			case XmlPullParser.START_TAG:
				if("getMobileCodeInfoResult".equals(parser.getName())){
					return parser.nextText();
				}
				break;
			}
			event = parser.next();
		}
		return null;
	}
}


//read(inStream)方法:
public class StreamTool {
	public static byte[] read(InputStream inStream) throws Exception{
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = 0;
		while( (len= inStream.read(buffer)) != -1 ){
			outStream.write(buffer, 0, len);
		}
		outStream.close();
		inStream.close();
		return outStream.toByteArray();
	}
}

你可能感兴趣的:(调用webservice)