1. 程式人生 > >Android 非同步呼叫websevice介面

Android 非同步呼叫websevice介面

在libs下新增
  ksoap2-android-assembly-3.4.0-jar-with-dependencies.jar


 //非同步呼叫Web Service,獲取資料
public class JsonAsynTaskXml extends AsyncTask<Void, Void, String> {

	private static final String TAG = "JsonAsynTaskXml";
    //獲取返回值後的回撥介面
	private WebServiceListenerXml lisener;
	//獲取資料開始到獲取到資料中間時間段的轉圈動畫
	private CustomProgressDialog progressDialog;
	
	private String param = "";
	private String arg0;
	private String arg1;

	private boolean iSprogressShow;
	private boolean isSheep;
	private Context context;
	private String endPoint;

	
	/**
	 * 處理新的webservice
	 * @param endPoint
	 */
	public void setEndPoint(String endPoint) {
		this.endPoint = endPoint;
	}
   //設定引數
	public void setParams(Map<String, String> map) {
		for (String key : map.keySet()) {
			param += key+"="+ map.get(key)+";";
		}
	}

	public void setArg0(String arg0) {
		this.arg0 = arg0;
	}

	public void setArg1(String arg1) {
		this.arg1 = arg1;
	}

	public JsonAsynTaskXml(WebServiceListenerXml lisener, Context context,boolean iSprogressShow,boolean isSheep) {
		super();
		this.iSprogressShow = iSprogressShow;
		this.isSheep = isSheep;
		this.context = context;
		//如果要實現轉圈動畫,就例項化CustomProgressDialog
		if(iSprogressShow){
			if (context != null) {
				progressDialog = new CustomProgressDialog(context, R.style.progressDialog);
				progressDialog.setCanceledOnTouchOutside(false);
				progressDialog.show();
			}
		}
		
		
		this.lisener = lisener;
	}
/*	public JsonAsynTaskXml(WebServiceListenerXml lisener){
		super();
		this.lisener = lisener;
	}*/
	@Override
	protected void onPostExecute(String result) {
		if(iSprogressShow){
			if (progressDialog != null) {
				progressDialog.dismiss();
			}
		}
		LogUtils.e(TAG, result+"");
		lisener.onComplete(result);
	}

	@Override
	protected String doInBackground(Void... params) {
		if(isSheep){
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		建立SoapObject物件,
		引數Constants.nameSpace對應WebService名稱空間,	
		引數Constants.methodName對應WebService方法名	··
		SoapObject request = new SoapObject(Constants.nameSpace, Constants.methodName);
         

        //呼叫SoapObject物件的addProperty方法來設定引數
		//引數名不一定要和伺服器端的引數名相同,只要引數順序相同就可以
		request.addProperty("arg0", arg0);
		request.addProperty("arg1", arg1);
		request.addProperty("arg2", param);
		request.addProperty("arg3", "json");

       //建立某一版本的SoapSerializationEnvelope物件
		SoapSerializationEnvelope envelope = new 
        SoapSerializationEnvelope(SoapEnvelope.VER10);

	    //將SoapObject建立的物件,
		//設定為SoapSerializationEnvelope傳出的SOAP訊息體
		//envelope.setOutputSoapObject(request);也可以這樣設定	
		envelope.bodyOut = request;

		LogUtils.e("param", param);
		String mAccounts = PrefsUtils.readPrefs(context, "mAccounts");
		Element[] header = SoapHeader.getElement(mAccounts);

        //封裝下SoapHeader的引數名和引數值賦值給SoapSerializationEnvelope物件的headerOut屬性
		envelope.headerOut = header;
		
		if(endPoint ==null){
			endPoint = Constants.endPoint;
			LogUtils.e("endPoint", endPoint);
		}
		LogUtils.e("TAG", endPoint);
		//建立HttpTransportSE物件,該物件用於呼叫WebService操作
		HttpTransportSE ts = new HttpTransportSE(endPoint,20000);
		try {
            //呼叫call方法,並以SoapSerializationEnvelope物件為引數,呼叫遠端的Web Service
			ts.call(null, envelope);

            //直接呼叫SoapSerializationEnvelope物件的getResponse方法獲取返回值
			//獲取返回值還可以以((SoapObject) envelope.bodyIn).getProperty(0).toString()方式獲取
			return envelope.getResponse().toString();
		} catch (IOException e) {
			e.printStackTrace();
			if(iSprogressShow){
				if (progressDialog != null) {
					progressDialog.dismiss();
				}
			}
			return null;
		} catch (XmlPullParserException e) {
			e.printStackTrace();
			if(iSprogressShow){
				if (progressDialog != null) {
					progressDialog.dismiss();
				}
			}
			return null;
		}
	};
}


//回撥介面
public interface WebServiceListenerXml {
	
	void onComplete(String result);

}



//封裝SoapHeader的類
public class SoapHeader {
	public static Element[] getElement(String mAccounts) {
		try {
			Element[] header = new Element[1];
			header[0] = new Element().createElement("MySoapHeader", "MySoapHeader");
			Element username = new Element().createElement("username", "username");
			username.addChild(Node.TEXT, encode(mAccounts));
			header[0].addChild(Node.ELEMENT, username);
		/*	Element pass = new Element().createElement("vcode", "vcode");
			pass.addChild(Node.TEXT, encode(VcodeUtil.createVcode()));
			header[0].addChild(Node.ELEMENT, pass); */
			return header;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}