java 調用axis2 webservice樣本:、
package cn.well.step.webservice.kiosk;import java.util.Date;import java.util.Iterator;import org.apache.axiom.om.OMAbstractFactory;import org.apache.axiom.om.OMElement;import org.apache.axiom.om.OMFactory;import org.apache.axiom.om.OMNamespace;import org.apache.axis2.addressing.EndpointReference;import org.apache.axis2.client.Options;import org.apache.axis2.AxisFault;import org.apache.axis2.Constants;import org.apache.axis2.client.ServiceClient;/** * 清華一體機 * @author mypc * */public class KioskClient{private final static String BASEPATH="http://192.168.0.150:8080/Server_Huier/services/History"; public static boolean isExist(String uid) throws Exception {EndpointReference targetEPR = new EndpointReference(BASEPATH);OMFactory fac = OMAbstractFactory.getOMFactory();OMNamespace omNs = fac.createOMNamespace("http://webservice.kiosk.tsinghua.edu", "xs");OMElement method = fac.createOMElement("validate", omNs);OMElement value = fac.createOMElement("uid", omNs);value.addChild(fac.createOMText(value, uid));method.addChild(value);Options options = new Options();options.setTo(targetEPR);options.setTransportInProtocol(Constants.TRANSPORT_HTTP);ServiceClient sender = new ServiceClient();sender.setOptions(options);OMElement result = sender.sendReceive(method);String response = result.getFirstElement().getText();if(("true").equals(response))return true;return false;}/** *擷取使用者基本資料 * @param personToGreet * @return 不存在返回null * @throws Exception */public static String getUserInfo(String uid) throws Exception {EndpointReference targetEPR = new EndpointReference(BASEPATH);OMFactory fac = OMAbstractFactory.getOMFactory();OMNamespace omNs = fac.createOMNamespace("http://webservice.kiosk.tsinghua.edu", "xs");OMElement method = fac.createOMElement("getUserInfo", omNs);OMElement value = fac.createOMElement("uid", omNs);value.addChild(fac.createOMText(value, uid));method.addChild(value);Options options = new Options();options.setTo(targetEPR);options.setTransportInProtocol(Constants.TRANSPORT_HTTP);ServiceClient sender = new ServiceClient();sender.setOptions(options);OMElement result = sender.sendReceive(method);String response = result.getFirstElement().getText();if("NoUser".equals(response)) return null;String username,ethnic,sex,birthday,address;username=response.substring(response.indexOf("<username>")+10, response.indexOf("</username>"));ethnic=response.substring(response.indexOf("<ethnic>")+8, response.indexOf("</ethnic>"));sex=response.substring(response.indexOf("<sex>")+5, response.indexOf("</sex>"));birthday=response.substring(response.indexOf("<birthday>")+10, response.indexOf("</birthday>"));address=response.substring(response.indexOf("<address>")+9, response.indexOf("</address>"));return username+","+ethnic+","+sex+","+birthday+","+address;}//http://192.168.0.45:8080/Server_Huier/services/History/getTypeResult?uid=330501198812062255&type=weight&dateStart=2011-11-12 /** *擷取使用者基本資料 * @param personToGreet * @param dt 日期格式:2011-11-12 11:05:33 * @return 不存在返回null * @throws Exception */public static String getWeight(String uid,String dt) throws Exception {EndpointReference targetEPR = new EndpointReference(BASEPATH);OMFactory fac = OMAbstractFactory.getOMFactory();OMNamespace omNs = fac.createOMNamespace("http://webservice.kiosk.tsinghua.edu", "xs");OMElement method = fac.createOMElement("getTypeResult", omNs);OMElement uidE = fac.createOMElement("uid", omNs);uidE.addChild(fac.createOMText(uidE, uid));OMElement typeE = fac.createOMElement("type", omNs);typeE.addChild(fac.createOMText(typeE, "weight"));OMElement dateStartE = fac.createOMElement("dateStart", omNs);dateStartE.addChild(fac.createOMText(dateStartE, dt));method.addChild(uidE);method.addChild(typeE);method.addChild(dateStartE);Options options = new Options();options.setTo(targetEPR);options.setTransportInProtocol(Constants.TRANSPORT_HTTP);ServiceClient sender = new ServiceClient();sender.setOptions(options);OMElement result = sender.sendReceive(method);String response = result.getFirstElement().getText();if("NoUser".equals(response)) return null;String username,ethnic,sex,birthday,address;return response;}public static void main(String[] args) {try {//System.out.println(KioskClient.isExist("330501198812062255"));//System.out.println(KioskClient.getUserInfo("330501198812062255"));System.out.println(KioskClient.getWeight("330501198812062255","2011-11-12 11:05:33"));} catch (Exception e) { e.printStackTrace();System.out.println(e.toString());}}}
調用結果如下:
<data><type value='weight'><WEIGHT begin_time='2013-06-22 18:09:34'><weight value='86.2' /><height value='172.0' /><waistline value='null' /></WEIGHT><WEIGHT begin_time='2013-06-22 19:43:53'><weight value='86.2' /><height value='173.0' /><waistline value='null' /></WEIGHT><WEIGHT begin_time='2013-06-22 19:59:01'><weight value='86.2' /><height value='173.0' /><waistline value='null' /></WEIGHT><WEIGHT begin_time='2013-06-22 20:29:15'><weight value='86.4' /><height value='171.0' /><waistline value='null' /></WEIGHT><WEIGHT begin_time='2013-06-22 20:29:52'><weight value='86.5' /><height value='172.5' /><waistline value='null' /></WEIGHT><WEIGHT begin_time='2013-06-22 20:30:09'><weight value='86.4' /><height value='165.5' /><waistline value='null' /></WEIGHT><WEIGHT begin_time='2013-06-22 20:30:20'><weight value='86.4' /><height value='173.5' /><waistline value='null' /></WEIGHT><WEIGHT begin_time='2013-06-22 20:30:36'><weight value='86.4' /><height value='173.0' /><waistline value='null' /></WEIGHT><WEIGHT begin_time='2013-06-22 20:30:50'><weight value='86.5' /><height value='172.5' /><waistline value='null' /></WEIGHT><WEIGHT begin_time='2013-06-22 20:31:31'><weight value='86.5' /><height value='174.0' /><waistline value='null' /></WEIGHT></type></data>