Android with WCF service using JSON
In this article I will show you simple example how to:
- Create simple .NET WCF service
- Connect Android application with .NET WCF service, get some data in JSON format, and show data in Android's ListView
1. Create simple .NET WCF service
Service.svc
With function GetPersons() we get data from SQL 2008 database, and return them as generic list.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Web.Script.Serialization;
using MB.AndroidWCF.DataAccessLayer;
namespace MB.AndroidWCF
{
public class Service : IService
{
public List<spGetPersonsResult> GetPersons()
{
// Get data with DLINQ
return new dbAndroidWCFDataContext().spGetPersons().ToList();
}
}
}
IService.cs
Note that the spGetPersonsResult is SQL stored procedure accessed with DLINQ.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using MB.AndroidWCF.DataAccessLayer;
namespace MB.AndroidWCF
{
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "GetPersonsJSON")]
List<spGetPersonsResult> GetPersons();
}
}
The WCF service returns data in JSON format like this:
{"GetPersonsResult":[{"ID":1,"FullName":"Matija Božičević"},
{"ID":2,"FullName":"Al Pacino"},
{"ID":3,"FullName":"Clint Eastwood"}]}
2. Connect Android application with .NET WCF service
Get some data in JSON format, and show data in Android's ListView.
MainActivity.java
package AndroidWCFApp.package;
import android.app.Activity;
import android.view.View;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONObject;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GetPersons();
}
public void GetPersons() {
ArrayList<String> persons = FromJSONtoArrayList();
ListView listView1 = (ListView)findViewById(R.id.ListView01);
listView1.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, persons));
}
public ArrayList<String> FromJSONtoArrayList() {
ArrayList<String> listItems = new ArrayList<String>();
try {
// Replace it with your own WCF service path
URL json = new URL("http://192.168.1.1:9020/Service.svc/GetPersonsJSON");
URLConnection jc = json.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(jc.getInputStream()));
String line = reader.readLine();
JSONObject jsonResponse = new JSONObject(line);
JSONArray jsonArray = jsonResponse.getJSONArray("GetPersonsResult");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jObject = (JSONObject)jsonArray.get(i);
// "FullName" is the property of .NET object spGetPersonsResult,
// and also the name of column in SQL Server 2008
listItems.add(jObject.getString("FullName"));
}
reader.close();
} catch(Exception e){
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
}
return listItems;
}
}
And thats it! Plain and simple!