Wednesday, June 22, 2011

All about developing an android app !

I have planned to share all the information that i came across while developing the ANDROID application, in that way i can keep a log of the new things that i have learnt so far. This may be interesting for the newbies !

Here we go ..
The first interesting thing that i came across was the ksoap2 library provided by google. Well, we need to know the purpose of this external jar file first ! My application had to connect to a web-service application hosted in a test server. For an android client to communicate with the web-server (.Net) we need add this ksoap2 library to our project properties (java build path).
download link : http://code.google.com/p/ksoap2-android/wiki/HowToUse?tm=2

The first noticeable thing with ksoap2 is to pick the right version from the google code page. I detected some issues with the 2.4 version which i downloaded initially. Later on, i downloaded the 2.5.5 from the download page and i had no example code to work on and the older example codes were not compatible with the latest version. So i decided to pick an intermediate version 2.5.2 from the google code page using tortoise svn from windows. (bcoz repo is maintained in svn).

Once you have obtained the 2.5.2, you can use this example code to work on.

public void GetResults()
{
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("lastName", "sm");
request.addProperty("middleName", "");
request.addProperty("firstName", "");
request.addProperty("pageSize", 25);
request.addProperty("pageNumber", 1);

SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(request);

//AndroidHttpTransport aht = new AndroidHttpTransport(URL);
HttpTransportSE aht = new HttpTransportSE(URL);

try
{
aht.setXmlVersionTag("");
aht.call(SOAP_ACTION, soapEnvelope);

//SoapObject result = (SoapObject)soapEnvelope.bodyIn;
SoapObject result = (SoapObject)soapEnvelope.getResponse();
//SoapPrimitive result = (SoapPrimitive) soapEnvelope.getResponse();
//Object result = soapEnvelope.getResponse();

Log.d("WS", String.valueOf(result));
}
catch(Exception e)
{
e.printStackTrace();
}
}
you may be wondering, what to import since ctrl+shift+O does not automatically fix the project settings. So here is the import list required :
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
import org.ksoap2.transport.HttpTransportSE;
See I faced a lot of issues with the import statements when i used the latest 2.5.5. So be careful with the import statements.
Now.. In the above code u might see somethings like NAMESPACE, METHOD_NAME ,URL etc. What are these ? and why are they important ??
this is the place where we are going to specify the webservice, the method exposed and its corresponding address.
private static final String SOAP_ACTION = "http://tempuri.org/MethodName";
private static final String METHOD_NAME = "MethodName";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://your_ip_where_service_running/Service1.asmx";

Many of you might ignore the last four lines of code, but believe me a small issue in the namespace took me 2 days to fix a certain deadly issue. When ever i passed a property / parameter to the web service - i was getting a null parameter there. I tried all the possible ways of sending the params, nothing worked. Finally found that the namespace name needs to end with a "/" else you will face with this kinda issues. There is one more notable thing here, we need to tell the soapEnvelope that this is going to communicate with a dotNet service. So soapenv.dotNet = true; is a compulsory thing !!

Once you have taken care about all these things, hope u can get it to work ;) more posts on its way !! If you have doubts post it here.

No comments:

Post a Comment