`

Axis调用webservice基础

阅读更多
最近会用到axis访问外部服务,简单学习了下基本用法,在这里备份一份代码。



import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
 

public class LeanAxis {

	public static void main(String[] args) {
		try {

			String endpoint = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx";
			String namespace = "http://WebXml.com.cn/";
			String methodName = "getSupportCity";
			String arg = "byProvinceName";

			Service service = new Service();
			Call call = (Call) service.createCall();
			
			call.setTargetEndpointAddress(new java.net.URL(endpoint));
			call.setOperationName(methodName);
			call.setUseSOAPAction(true);
			call.setSOAPActionURI(namespace + methodName);
 
			//需要使用QName包含namespace 使用单纯的字符串会报错
			call.setOperationName(new javax.xml.namespace.QName(namespace,
					methodName));
			call.addParameter(new javax.xml.namespace.QName(namespace, arg),
					org.apache.axis.encoding.XMLType.XSD_STRING, String.class,
					javax.xml.rpc.ParameterMode.IN);
			//设置返回值类型 不设置的话在遇到复杂类型会出现反序列化的错误
			call.setReturnClass(String[].class);
			
			String[] result = (String[]) call.invoke(new Object[] { "四川" });
			System.out.println("省会" + result[0]);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}







相关资料:
http://ontheroad-luckhouge.iteye.com/blog/796180
http://www.ascenttech.cn/html/book/part/step3/xx12.jsp
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics