Introduction
As many of you know, Web Services are a great way to establish communication between distant and independent platforms.
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 .
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
| [WebMethod]
public String[] GetClient(int Number)
{
String[] Clients = null;if (Number > 0 && Number <= 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:
1
2
3
4
5
6
7
8
9
10
11
12
| <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout> |
Create new xml file named list_item.xml in folder res/layout to define layout list item in your application.
1
2
3
4
5
6
7
| <?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="20dp"
android:textSize="16sp" >
</TextView> |
And the last thing is editing file .java in your project in folder src/yourpackagename/yourjavaclass.java And this is my full code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
| 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;
}
} |
