package com.activity.chaoran;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import com.sys.SysData;
import com.util.IoUtil;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

public class RunYmupThread extends Thread {
	private String param;
	private Handler handler;
	private String methodName;
	private int urlTy;// 0表示访问ServiceInterface,1表示访问offLineInventoryInterface

	public RunYmupThread(String param, Handler handler, String methodName,
			int urlTy) {
		this.param = param;
		this.handler = handler;
		this.methodName = methodName;
		this.urlTy = urlTy;
	}

	public void run() {
		super.run();
		Log.v("SearchThread", "run执行");
		try {
			String url = null;
			if (urlTy == 0) {
				url = SysData.url + "/webservice/ServiceInterface?wsdl";
			} else if (urlTy == 1) {
				url = SysData.url
						+ "/webservice/offLineInventoryInterface?wsdl";
			}
			String method = methodName;
			SoapObject rpc = new SoapObject("", method);
			if (param != null) {
				rpc.addProperty("param", param);
			}
			HttpTransportSE ht = new HttpTransportSE(url, SysData.timeout);
			ht.debug = true;
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
			envelope.bodyOut = rpc;
			envelope.dotNet = true;
			envelope.setOutputSoapObject(rpc);
			ht.call("", envelope);
			if (envelope.getResponse() != null) {// 判断是否返回结果，因为我这个是一个查询操作，是带有返回值的。
				Message msg = new Message();
				if (methodName.equals("saveDj")) {
					msg.arg1 = 3;
					msg.obj = envelope.getResponse().toString();
				} else if (methodName.equals("pdaRegister")) {
					msg.obj = envelope.getResponse().toString();
				} else {
					byte[] bb = org.kobjects.base64.Base64.decode(envelope
							.getResponse().toString());
					msg.obj = IoUtil.byte_obj(bb);
					if (methodName.equals("runYmup")) {
						msg.arg1 = 2;
					} else if (methodName.equals("runTqfa")) {
						msg.arg1 = 4;
					}
				}
				handler.sendMessage(msg);
			} else {
				Message msg = new Message();
				msg.obj = null;
				handler.sendMessage(msg);
			}
		} catch (Exception e) {
			Message msg = new Message();
			if (methodName.equals("runYmup")) {
				msg.what = -2;
			} else if (methodName.equals("saveDj")) {
				msg.what = -3;
			} else if (methodName.equals("runTqfa")) {
				msg.what = -4;
			}else{
				msg.what = -1;
			}
			msg.obj = e.toString();
			handler.sendMessage(msg);
		}
	}
}
