大家好,我是奶綠茶
今天來分享如何使用 Flash 及 Android 呼叫 ASP.NET 的 WebService
WebService 的好處, 就是可以把專案所有要用到的方法, 寫在一隻程式裡, 方便日後管理。
傳接的格式, 都使用 JSON
.net 傳 WebService程式
使用方法如下:
service.要呼叫的方法.send( 參數1, 參數2 );
參考了這二篇的教學
http://composedcrap.blogspot.com/2009/08/connecting-to-net-web-service-from.html
http://blog.yam.com/wewa85/article/32877801
轉載請註明出處
今天來分享如何使用 Flash 及 Android 呼叫 ASP.NET 的 WebService
WebService 的好處, 就是可以把專案所有要用到的方法, 寫在一隻程式裡, 方便日後管理。
傳接的格式, 都使用 JSON
.net 傳 WebService程式
[WebService(Namespace = "http://milkmidi.com/")] //注意這個 Namespace [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.Web.Script.Services.ScriptService] public class WebService : System.Web.Services.WebService { private static JavaScriptSerializer serializer = new JavaScriptSerializer(); public WebService () { } [WebMethod] public string getArray() { return serializer.Serialize(new string[] { "A", "B", "c" }); } [WebMethod] public string getObject() { var json = new { RS = "OK" }; return serializer.Serialize(json); } ///flash端, 使用 FlexSDK 裡的 Webservice 類別/// Client 端傳 JSON 格式, Server 端解 /// [WebMethod] public string setObject(string json) { JsonObject obj = JsonConvert.Import(typeof(JsonObject), json) as JsonObject; string name; string age; try { name = obj["name"].ToString(); age = obj["age"].ToString(); } catch (Exception) { throw; } var rs = new { RS = "OK" , name = name, age = age }; return serializer.Serialize(rs); } }
使用方法如下:
service.要呼叫的方法.send( 參數1, 參數2 );
/** * @author milkmidi */ package { import com.adobe.serialization.json.JSON; import flash.display.Sprite; import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent; import mx.rpc.soap.LoadEvent; import mx.rpc.soap.WebService; import mx.utils.ObjectUtil; public class MXWebService extends Sprite { public var service:WebService = new WebService(); public function MXWebService() { service.wsdl = "http://localhost:61490/webService/WebService.asmx?wsdl"; service.addEventListener(LoadEvent.LOAD, serviceLoadHandler); service.loadWSDL(); //getArray(); //getObject(); setObject( JSON.encode( { name:"milkmidi", age:"30" } ) ); // 使用 // service.要呼叫的方法.send( 參數1, 參數2 ); } private function getArray():void { service.getArray.addEventListener(ResultEvent.RESULT, resultHandler); service.getArray.addEventListener(FaultEvent.FAULT, faultHandler); service.getArray.send(); } private function getObject():void { service.getObject.addEventListener(ResultEvent.RESULT, resultHandler); service.getObject.addEventListener(FaultEvent.FAULT, faultHandler); service.getObject.send(); } private function setObject(json:String):void { service.setObject.addEventListener(ResultEvent.RESULT, resultHandler); service.setObject.addEventListener(FaultEvent.FAULT, faultHandler); service.setObject.send(json); } private function faultHandler(e:FaultEvent):void { trace(e); } private function resultHandler(e:ResultEvent):void { trace(e); trace(ObjectUtil.toString(e.result)); } private function serviceLoadHandler(e:LoadEvent):void { trace(e); } } }Android 端
參考了這二篇的教學
http://composedcrap.blogspot.com/2009/08/connecting-to-net-web-service-from.html
http://blog.yam.com/wewa85/article/32877801
public class SampleCallWebService extends Activity { // ASP.NET 端, 宣告的 Namespace private static final String NAMESPACE = "http://milkmidi.com/"; // asmx 路徑 private static final String URL = "http://localhost:61490/webService/WebService.asmx"; //要呼叫的方法 private static final String MemberLogin_SOAP_ACTION = NAMESPACE+"setObject"; private static final String METHOD_NAME2 = "setObject"; private Button mBtn; private TextView mMsgTxt; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mBtn = (Button) findViewById(R.id.btn); mMsgTxt = (TextView) findViewById(R.id.btn); mBtn.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { try { mMsgTxt.setText( doSetObject() ); } catch (JSONException e) { e.printStackTrace(); } } }); } public String doSetObject() throws JSONException { Log.i("[WebService]", "doSetObject"); String rs = ""; SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME2); JSONObject json = new JSONObject(); json.put("name", "milkmidi"); json.put("age", "30"); request.addProperty("json", json.toString()); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); envelope.dotNet = true; envelope.setOutputSoapObject(request); HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); try { androidHttpTransport.call(MemberLogin_SOAP_ACTION, envelope); SoapPrimitive result = (SoapPrimitive) envelope.getResponse(); rs = result.toString(); } catch (Exception e) { mMsgTxt.setText(e.toString()); } JSONObject rsJson = new JSONObject(rs); return rsJson.getString("name")+","+rsJson.getString("age"); } }
轉載請註明出處
留言