
Web Service
With .NET technology everyone can build .NET Web services with easy step and less code. It its very easy consuming .NET web service with any application with .NET Framework system. In this tutorial we use android to consume .NET web service, with android java application. First thing we need library in java environment to call web service. Ksoap2 will provide this facility with open source way.
you can download Ksoap2 here .
As many of you know, Web Services are a great way to establish communication between distant and independent platforms.
In this article demonstrate how to create simple list view in android using value that we get from web service.
Using the code
Create .NET web service
In .NET web service we create simple web method that return array value of client name. the code is like this:
[WebMethod] public String[] GetClient(int Number) { String[] Clients = null;if (Number &gt; 0 &amp;&amp; Number &lt;= 10) { Clients = <a href="http://www.google.com/search?q=new+msdn.microsoft.com">new</a> String[Number]; for (int i = 0; i &lt; Number; i++) { Clients[i] = "Client: " + i.ToString(); } } return Clients; }
Create Android application
Create new android project and insert library Ksoap2 in your project properties. Then in folder res/layout/main.xml edit main layout, the code should be like this:
<!--?xml version="1.0" encoding="utf-8"?-->Create new xml file named list_item.xml in folder res/layout to define layout list item in your application.
<!--?xml version="1.0" encoding="utf-8"?-->And the last thing is editing file .java in your project in folder src/yourpackagename/yourjavaclass.java And this is my full code:
package com.latihan.layout.listview; import android.app.ListActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener;import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE; public class displaylist extends ListActivity { /** Called when the activity is first created. */ private static final <a href="http://www.google.com/search?hl=en&q=allinurl%3Astring+java.sun.com&btnI=I%27m%20Feeling%20Lucky">String</a> SOAP_ACTION = “http://tempuri.org/GetClient”; private static final <a href="http://www.google.com/search?hl=en&q=allinurl%3Astring+java.sun.com&btnI=I%27m%20Feeling%20Lucky">String</a> METHOD_NAME = “GetClient”; private static final <a href="http://www.google.com/search?hl=en&q=allinurl%3Astring+java.sun.com&btnI=I%27m%20Feeling%20Lucky">String</a> NAMESPACE = “http://tempuri.org/”; private static final <a href="http://www.google.com/search?hl=en&q=allinurl%3Astring+java.sun.com&btnI=I%27m%20Feeling%20Lucky">String</a> <a href="http://www.google.com/search?hl=en&q=allinurl%3Aurl+java.sun.com&btnI=I%27m%20Feeling%20Lucky">URL</a> = “http://10.0.2.2/wshello/Service.asmx”; TextView tv; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Call function web service <a href="http://www.google.com/search?hl=en&q=allinurl%3Astring+java.sun.com&btnI=I%27m%20Feeling%20Lucky">String</a>[] countries3 = call(); //Create list view setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, countries3)); <a href="http://www.google.com/search?hl=en&q=allinurl%3Alistview+java.sun.com&btnI=I%27m%20Feeling%20Lucky">ListView</a> lv = getListView(); lv.setTextFilterEnabled(true); lv.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, <a href="http://www.google.com/search?hl=en&q=allinurl%3Aview+java.sun.com&btnI=I%27m%20Feeling%20Lucky">View</a> view, int position, long id) { // When clicked, show a toast with the TextView text Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show(); } }); } public <a href="http://www.google.com/search?hl=en&q=allinurl%3Astring+java.sun.com&btnI=I%27m%20Feeling%20Lucky">String</a>[] call() { <a href="http://www.google.com/search?hl=en&q=allinurl%3Astring+java.sun.com&btnI=I%27m%20Feeling%20Lucky">String</a>[] ret = null; try { SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); // Send parameter to web service request.addProperty(“<a href="http://www.google.com/search?hl=en&q=allinurl%3Anumber+java.sun.com&btnI=I%27m%20Feeling%20Lucky">Number</a>”, 6); // Create envelope SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.dotNet=true; envelope.setOutputSoapObject(request); // Call web service HttpTransportSE androidHttpTransport = new HttpTransportSE(<a href="http://www.google.com/search?hl=en&q=allinurl%3Aurl+java.sun.com&btnI=I%27m%20Feeling%20Lucky">URL</a>); androidHttpTransport.call(SOAP_ACTION, envelope); // Retrieve response object <a href="http://www.google.com/search?hl=en&q=allinurl%3Aobject+java.sun.com&btnI=I%27m%20Feeling%20Lucky">Object</a> o = (<a href="http://www.google.com/search?hl=en&q=allinurl%3Aobject+java.sun.com&btnI=I%27m%20Feeling%20Lucky">Object</a>)envelope.getResponse(); // it’s SoapObject only for arrays SoapObject obj = (SoapObject)o; // Collect object and store in array variable ret = new <a href="http://www.google.com/search?hl=en&q=allinurl%3Astring+java.sun.com&btnI=I%27m%20Feeling%20Lucky">String</a>[obj.getPropertyCount()]; for (int i = 0; i < ret.length; i++) { ret[i] = obj.getProperty(i).toString(); } } catch (<a href="http://www.google.com/search?hl=en&q=allinurl%3Aexception+java.sun.com&btnI=I%27m%20Feeling%20Lucky">Exception</a> e) { Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show(); } return ret; } }
RSS Feed
Twitter


Posted in
Tags: 
Hi, I found your website by Yahoo search engine. figured I should take a look around. your website is really interesting and has lots of great info. Thanks for sharing.
Your’s is the intelgleint approach to this issue.
Hi, Quality related information! I have been searching for everything like this for a while currently. Thank you!
Hello, I’m an android user, do you have more posts about Android for cell phones or tablets?. Anyway will search your web. thanks you.
I’m continue studying from you, and I’m bettering myself. I really love reading everything that’s written on your site. Keep the articles coming. Really enjoy it!
Great post, I conceive blog owners should learn a lot from this weblog its rattling user friendly. So much wonderful information on here
.