android 访问 webservice 获取信息

1.其实这个也没什么说的,webservice经常用到的 直接把代码贴出来了。

前面说的那么多,应用程序嘛,现在都和网络打交道的,特此贴出来 android 和 webservice打交道= =, http协议也是一定要学好的呢。

需要用到1个jar 包  ksoap2-android-assembly-3.0.0-jar-with-dependencies,链接    http://download.csdn.net/detail/kongbaidepao/6603281

这个遵循的是 soap协议,至于soap协议是什么,想详细了解下的就搜搜把,资料一大把。

简单说一下就是 :简单对象访问协议(simple object  access protocol),可以让不同的应用程序透过 http 协定好,进行数据的交换。就好比 你要访问我的webservice

我不管你的前端是用什么 语言写的 只要你遵循我的规定,给我一个xml,这样我就可以按照你的要求给你相应的生成数据,返回1个数据给你。 至于怎么解析是你自己语言事。

smtp 和 http协议都可以传输 soap消息。

public class AndroidWebService{
	
	/**
	 * @param nameSpace  命名空间
	 * @param wsurl      访问的网址
	 * @param doMethod   方法名
	 * @param List       需要给的参数list
	 * @return
	 */
	public static SoapObject GetWebServiceData(String nameSpace,String wsurl, String doMethod,  List<WebParameter> List){
		// String resultInfo = "";
		// 1.定义必要的常量
		SoapObject result = null;
		String soapAction = nameSpace + doMethod;
		// 2.得到KSOAP2的核心对象,并将参数传递给SoapObject
		String wsurl1 = wsurl + "?wsdl";
		SoapObject request = new SoapObject(nameSpace, doMethod);
		if (List != null){
			for (WebParameter WebParameter : List){
				request.addProperty(WebParameter.parameter, WebParameter.value);
			}
		}
		// 3.造信封
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.bodyOut = request;// 将含有参数的SoapObject装到信封里面
		//声明一下服务器是.net做的
		envelope.dotNet = true;
		//4造一条通向目的地的大路                   设置超时时间 10秒
		HttpTransportSE se = new HttpTransportSE(wsurl1,1000 * 10);
		se.debug = true;
		try{
			/**
			 *   5.进行通信
			 *   将信封发送出去,并且将对方返回的信息同样装到信封里面,然后寄回,
			 *   此时信封里面携带了需要的结果数据
			 */
			se.call(soapAction, envelope);
			if (envelope.getResponse() != null){
				
				// 判断一下返回的信息是否到达本地,如果到达,则从信封里面取出结果
				result = (SoapObject) envelope.getResponse();// 取出结果,放的时候是SoapObject对象,取的时候同样是SoapObject对象
			}
		} catch (Exception e){
			e.printStackTrace();
		}
		return result;
	}

}

WebParameter.java

//参数
public class WebParameter{
	public String parameter;
	public String value;
	
	public WebParameter(String parameter, String value){
		this.parameter  =  parameter;
		this.value = value;
	}	

}

返回来1个 soap 对象然后就可以自己解析到数据了

public class FormatGetCheckTable {

	public static object format(SoapObject object) {


		for (int i = 0; i < object.getPropertyCount(); i++) {

			SoapObject property = (SoapObject) object.getProperty(i);
                       //.....................................................................
		}
		return object;
	}
	
}


2. 当你不用jar的时候也可以。那就我上面解释说的了,要自己按照人家的要求给生成一个相应的 xml文件,人家看到了解析了才能给你返回相应的数据。



好了代码:

你自己这边的xml。照着上面我的截图直接 copy过来就好了  加个占位符。

<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>

获取手机号的代码:

其实就是把 上面的xml的占位符 换成 你的手机号,然后把xml传过去。然后解析人家给你返回来的xml数据

public class GetAddressService {
	public static String getAddress(String mobileNum) throws Exception{
		String soap = readSoap();
		//                特殊字符 要转义一下
		soap = soap.replaceAll("\\$mobile", mobileNum);
		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));
		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();
		//这里是 pull 解析
		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;
	}

	private static String readSoap() throws Exception{
		InputStream inStream = GetAddressService.class.getClassLoader().getResourceAsStream("soap.xml");
		byte[] data = StreamRead.read(inStream);
		return new String(data);
	}
	
}
//这就是把数据读到。
public class StreamRead {
	public static byte[] read(InputStream inStream) throws Exception{
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		byte[] bb = new byte[1024];
		int len = 0;
		while( (len = inStream.read(bb)) != -1){
			outputStream.write(bb, 0, len);
		}
		inStream.close();
		return outputStream.toByteArray();
	}

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

空白的泡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值