This blog is a simple example of Android call a WCF service.
Examples
This example creates a simple notice of a WCF interface as an example about the interaction of Android and WCF. First, the method can be used CreateNotice WcfTestClient WCF interface under test, Java can refer to Part I call a WCF blog. New WCFService call a WCF tool class as follows
* Access WCF Tools * the Created by Carlos ON 2015/11/28. * / Public class { public static Final String WCF_URL = "" ; // class changed their quotes public static Final String NAMESPACE = "" ; // quotes class changed his public static Final String the SOAP = "" ; // quotes classes changed their Private static Final ExecutorService ExecutorService = Executors.newFixedThreadPool ( 3 );
* Call WCF method * @param methodName * @param the Properties * @param wcfServiceCallBack * / public static void callWCFService ( Final String methodName, the Map the Properties, Final WCFServiceCallBack wcfServiceCallBack) { // create HttpTransportSE object, passing WCFService server address Final HttpTransportSE httpTransportSE = new new HttpTransportSE (WCF_URL); soapObject soapObject = new new soapObject (the NAMESPACE, methodName); IF (= Properties! null){ for(Iterator<Map.Entry<String,Object>> it=properties.entrySet().iterator();it.hasNext();){ Map.Entry<String,Object> entry=it.next(); soapObject.addProperty(entry.getKey(),entry.getValue()); } }
// instantiate SoapSerializationEnvelope, the incoming SOAP WebService protocol version Final SoapSerializationEnvelope the SOAPEnvelope = new new SoapSerializationEnvelope (SoapEnvelope.VER11); soapEnvelope.bodyOut = soapObject; // set whether to call a .Net development WCFService soapEnvelope.setOutputSoapObject (soapObject ); soapEnvelope.dotNet = to true ; httpTransportSE.debug = to true ;
Handler // child thread for communicating with the main thread Final Handler mHandler = new new Handler () { public void the handleMessage (the Message MSG) { Super .handleMessage (MSG); // the return value of the callback parameter to callBack wcfServiceCallBack.callBack ((SoapObject) msg.obj); } }; // open thread to access WCFService ExecutorService.submit ( new new the Runnable () { public void RUN () { SoapObject resultSoapObject = null ; the try { httpTransportSE.call (methodName + the SOAP, SoapEnvelope ); IF
(! soapEnvelope.getResponse () = null ) { resultSoapObject = (SoapObject) soapEnvelope.bodyIn; } } the catch (IOException E) { e.printStackTrace (); } the catch (XmlPullParserException E) { e.printStackTrace (); } the finally { // Handler using the acquired message is sent to the main thread mHandler.sendMessage (mHandler.obtainMessage ( 0 , resultSoapObject)); } } }); }
Callback Interface * * / public interface WCFServiceCallBack { public void callBack (SoapObject Result) ; } }
MainActivity class which invokes this method to print out the results returned WCF