Saturday, October 6, 2012

[Andoird/.NET] Android and WCF template

I've been working a lot with setting up WCF locally. Now it is time to do the true test and connect my android with the sucka!

To set up the WCF, just follow this post:

Setting Up WCF in Virtual Directories

All you need is Android and Eclipse set up. Also, get the KSoap plug-in and add it to your library. Now just add this to your code:


 private static String SERVICE_URI = "http://192.168.1.2/WcfService1/Service1.svc";

 private static final String METHOD_NAME = "GetData";

 private static final String NAMESPACE = "http://tempuri.org/";

 private static final String URL = "http://192.168.1.2/WCFService1/Service1.svc";

 private static final String SOAP_ACTION = "http://tempuri.org/IService1/GetData";

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

request.addProperty("value","3");
            

 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

 envelope.dotNet = true;
 envelope.setOutputSoapObject(request);
           
 HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
 androidHttpTransport.call(SOAP_ACTION,envelope);
 SoapPrimitive result = (SoapPrimitive)envelope.getResponse();
           
 String resultData = result.toString();


EDIT: Btw, the request.addProperty method is a parameter of your method. And if you don't know your namespace, look at the wsdl of your service. This was done to target an Android 2.2 device. If you noticed, I was using the template VS2010 gives when you create a new Web Service.

Also, I tried using HttpResponse and HttpEntity, but I got no response when I tried to do something like http://192.168.1.2/WCFService/Service1.svc/GetData/0. I believe it has something to do with the binding. By default, WCF uses WSHttpBinding I think. I tried changing to BasicHttpBinding, but the configuration had issues finding MetaData. That is it for now.

No comments:

Post a Comment