package com.example.utils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

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

import android.os.Handler;
import android.os.Message;

/**
 * ·ÃÎÊWebServiceµÄ¹¤¾ßÀà,
 * 
 * @see http://blog.csdn.net/xiaanming
 * 
 * @author xiaanming
 * 
 */
public class WebServiceUtils {
	public static final String WEB_SERVER_URL = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx";
	// º¬ÓÐ3¸öÏß³ÌµÄÏß³Ì³Ø
	private static final ExecutorService executorService = Executors
			.newFixedThreadPool(3);
	// ÃüÃû¿Õ¼ä
	private static final String NAMESPACE = "http://WebXml.com.cn/";

	/**
	 * 
	 * @param url
	 *            WebService·þÎñÆ÷µØÖ·
	 * @param methodName
	 *            WebServiceµÄµ÷ÓÃ·½·¨Ãû
	 * @param properties
	 *            WebServiceµÄ²ÎÊý
	 * @param webServiceCallBack
	 *            »Øµ÷½Ó¿Ú
	 */
	public static void callWebService(String url, final String methodName,
			HashMap<String, String> properties,
			final WebServiceCallBack webServiceCallBack) {
		// ´´½¨HttpTransportSE¶ÔÏó£¬´«µÝWebService·þÎñÆ÷µØÖ·
		final HttpTransportSE httpTransportSE = new HttpTransportSE(url);
		// ´´½¨SoapObject¶ÔÏó
		SoapObject soapObject = new SoapObject(NAMESPACE, methodName);

		// SoapObjectÌí¼Ó²ÎÊý
		if (properties != null) {
			for (Iterator<Map.Entry<String, String>> it = properties.entrySet()
					.iterator(); it.hasNext();) {
				Map.Entry<String, String> entry = it.next();
				soapObject.addProperty(entry.getKey(), entry.getValue());
			}
		}

		// ÊµÀý»¯SoapSerializationEnvelope£¬´«ÈëWebServiceµÄSOAPÐ­ÒéµÄ°æ±¾ºÅ
		final SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		// ÉèÖÃÊÇ·ñµ÷ÓÃµÄÊÇ.Net¿ª·¢µÄWebService
		soapEnvelope.setOutputSoapObject(soapObject);
		soapEnvelope.dotNet = true;
		httpTransportSE.debug = true;

		// ÓÃÓÚ×ÓÏß³ÌÓëÖ÷Ïß³ÌÍ¨ÐÅµÄHandler
		final Handler mHandler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				// ½«·µ»ØÖµ»Øµ÷µ½callBackµÄ²ÎÊýÖÐ
				webServiceCallBack.callBack((SoapObject) msg.obj);
			}

		};

		// ¿ªÆôÏß³ÌÈ¥·ÃÎÊWebService
		executorService.submit(new Runnable() {

			@Override
			public void run() {
				SoapObject resultSoapObject = null;
				try {
					httpTransportSE.call(NAMESPACE + methodName, soapEnvelope);
					if (soapEnvelope.getResponse() != null) {
						// »ñÈ¡·þÎñÆ÷ÏìÓ¦·µ»ØµÄSoapObject
						resultSoapObject = (SoapObject) soapEnvelope.bodyIn;
					}
				} catch (HttpResponseException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} catch (XmlPullParserException e) {
					e.printStackTrace();
				} finally {
					// ½«»ñÈ¡µÄÏûÏ¢ÀûÓÃHandler·¢ËÍµ½Ö÷Ïß³Ì
					mHandler.sendMessage(mHandler.obtainMessage(0,
							resultSoapObject));
				}
			}
		});
	}

	/**
	 * 
	 * 
	 * @author xiaanming
	 * 
	 */
	public interface WebServiceCallBack {
		public void callBack(SoapObject result);
	}

}
