为Android写的WebService调用组件

java类

package com.net.ksoap2;

import java.io.IOException;
import java.util.Hashtable;
import java.util.Iterator;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

public class KsoapBase {

	protected Object execute(WebServiceParam param) throws IOException, XmlPullParserException {
		Object object = null;
		SoapObject request = new SoapObject(param.getNameSpace(), param.getMethodName());
		for (Iterator<String> it2 = param.getParams().keySet().iterator(); it2.hasNext();) {
			String _mKey = it2.next();
			Object _mValue = param.getParams().get(_mKey);
			request.addProperty(_mKey, _mValue);
		}
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		HttpTransportSE transport = new HttpTransportSE(param.getServerUrl());
		transport.call(param.getSoapAction(), envelope);
		object = envelope.getResponse();
		return object;
	}

	protected WebServiceParam getParam(String url, String nameSpace, String methodname) {
		WebServiceParam param = new WebServiceParam();
		param.setServerUrl(url);
		param.setNameSpace(nameSpace);
		param.setMethodName(methodname);
		param.setSoapAction(nameSpace + methodname);
		Hashtable<String, Object> has = new Hashtable<String, Object>();
		param.setParams(has);
		return param;
	}
}
package com.net.ksoap2;

import com.android.presell.AppApplication;

import android.util.Log;

public class WebService extends KsoapBase {

	private static String URL = "http://xxx/xxx.asmx";
	private static final String NAMESPACE = "http://xxxxxxx/";
	private WebServiceParam param;
	private static WebService webservice;

	private WebService(AppApplication application) {
		param = getParam(url, NAMESPACE, "方法");
	}

	public static WebService getWebService(AppApplication application) {
		if (webservice == null) {
			webservice = new WebService(application);
		}
		return webservice;
	}
}
package com.net.ksoap2;

import java.util.Hashtable;

public class WebServiceParam {
	private String nameSpace;
	private String methodName;
	private String serverUrl;
	private String soapAction;
	private Hashtable<String, Object> params;

	public String getMethodName() {
		return methodName;
	}

	public String getNameSpace() {
		return nameSpace;
	}

	public Hashtable<String, Object> getParams() {
		return params;
	}

	public String getServerUrl() {
		return serverUrl;
	}

	public String getSoapAction() {
		return soapAction;
	}

	public void setMethodName(String methodName) {
		this.methodName = methodName;
	}

	public void setNameSpace(String nameSpace) {
		this.nameSpace = nameSpace;
	}

	public void setParams(Hashtable<String, Object> params) {
		this.params = params;
	}

	public void setServerUrl(String serverUrl) {
		this.serverUrl = serverUrl;
	}

	public void setSoapAction(String soapAction) {
		this.soapAction = soapAction;
	}

}

你可能感兴趣的:(android,webservice)