This post assumes you have basic knowledge about web services but have no idea how to make one using the .NET framework. To create a web service in .NET in Visual Studio 2005,

Go to File > New > Web Service , choose ASP.NET Website and name the Project WebService1 (although you can name this any way you want).

Visual Studio 2005 will automatically create the necessary files for you. Note that since it created a .NET web service file called Service.asmx, a code behind file is also created called Service.cs (assuming your language is C#) under the App_Code folder.

Notice in Service.cs, a function that returns a string called HelloWorld is pre-made for you. Now, just compile your project and the IE browser will pop up loading the list of web services found. You can then see your HelloWorld web service there.

Click on the HelloWorld web service link and click the INVOKE button to check if the web service is running perfectly. Once you click on the INVOKE button, you will see an XML response similar to the one below

That’s it. You can see that your web service is running perfectly. What is a web service without having something to use them right? We shall now create a client to access and use the web service. This is pretty important so be careful how you add a new application to your existing project. To create an application within the same project,

Go to File > Add > New Project , choose Windows Application and name it WSClient. Once added, you will see that your Application and Web Service souce files are now in the same project.

To make use of the HelloWorld web service, right click WSClient in the Solution Explorer and choose Add Web Reference.

Select Web Services in this solution and it will look for available web services.

Choose the Services labeled Service and type in localhost as the web reference name (the web reference name is entirely up to you).

Now we head on to the Program.cs file so we can add code within the Main() method to access the web service. Add the following code.

Right click WSClient in the Solution Explorer again and select Set as Startup Project. This ensures that your application will be run and accesses the web service. If you know debugging, place a breakpoint on Console.ReadlLine(). Click Debug from the menu and choose Start Debug. Now, see the Hello World string outputted to the console.

Currently Google’s Android is under version m5 rc15. Instead of dabbling on converting all my java applet games to possibly android’s format, I focused on checking if Android is capable of calling web services or not. The answer is no. Android’s API doesn’t contain classes that support this capability.

However, a 3rd party library called KSoap2 is available for use. From their sourceforge website, “kSOAP is a SOAP web service client library for constrained Java environments such as Applets or J2ME applications (CLDC / CDC / MIDP)”. And how lucky that Android uses Java as its developmental language.

I am not sure if it is possible to build an Android application and call a remote web service by compiling it using a builder app like Ant. Most posts in the mailing list do cover making it work using the Eclipse IDE, as does most of Android’s tutorial focuses on.

I kept swarming the net looking for possible ways and solutions with using just Ant and not an IDE as i abhor using it. In the end, I had no choice but to use Eclipse to be able to at least verify that the sample code and solution provided by someone in the mailing list does indeed work.

Download the KSoap2 library. Extract the jar file library and add it in your Eclipse project. Create a lib folder and import the jar file there. Next, right click the project name and select properties. Under the Java Build Path, add the KSoap2 jar so that it will be included in the classpath during compilation and deployment. Click OK.

The following 2 files are needed for web service communication

The Android app code is pretty self explanatory. Just fill in the your values to the pre-defined variables. The request.addProperty() method is where you place your parameters in case the webservice method you want to call needs parameters supplied to it.

Requests will still be passed as XML. I hope this can help those who would want their Android apps to call webservices. I pretty had was stuck with this for probably a week browsing through the web, mailing lists or anything that I could come upon that would give me a hint as to how to make this work.

These codes were from someone in a mailing list who posted a solution. But I modified parts of it because they didn’t work in my case. Hit the run in the RUN menu of Eclipse to deploy your Android app. If you wish to see the System.out.println() messages in the console, run logcat. You won’t see those messages in the Eclipse console.

Related Posts Plugin for WordPress, Blogger...