JG Vimalan's Blog

It is all about coding!

Converting json string to C# object and vice versa

In order to convert a C# generic list to json string and vice versa,

Add reference to System.Web.Extensions in your project.

//Assume that, you have a generic list as shown below,
List<Patient> patientList = GetPatients();

Use Serialize method in JavaScriptSerializer class for serializing generic list to Json string.

System.Web.Script.Serialization.JavaScriptSerializer serializer =  new System.Web.Script.Serialization.JavaScriptSerializer();

//use Serialize method to convert patient list to json string

string JStr = serializer.Serialize(patientList);

//Now, the json string can be set to generic list of type patient as shown below,

List<Patient> nList = new List<Patient>();

nList = serializer.Deserialize<List<Patient> >(JStr);

DOWNLOAD the working sample here https://docs.google.com/file/d/0By3at1G2EGgOal9IMW5pQjNTNXM/edit (File->Download)

Advertisements

February 22, 2011 Posted by | Android, C#.NET, JSON | 7 Comments

Consuming WCF service in Android application

In order to consume WCF service in an Android application, the WCF Service must send and receive data in JSON format. So, you need to set the RequestFormat and ResponseFormat to JSON, in the WebInvoke attribute as shown below,

using System.ServiceModel;
using System.ServiceModel.Web;

    [ServiceContract]
    public interface IService
    {      
        [OperationContract]
        [WebInvoke(Method = “POST”,
        UriTemplate = “/GetEmployee”,
        RequestFormat = WebMessageFormat.Json, 
        ResponseFormat = WebMessageFormat.Json)]
        Employee GetEmployee(EmployeeIdentity request);

I used wsHttpBinding in my WCF service.  

Now, to consume the WCF Service in your Android application, use httpClient to process the request and get the response. A sample code is given below,

HttpClient httpClient = null;
HttpUriRequest httpUriRequest = null
HttpResponse httpResponse = null;

try {
      
httpUriRequest = //build request
httpClient = new DefaultHttpClient();
httpResponse = httpClient.execute(httpUriRequest);

October 29, 2010 Posted by | Android, JSON, WCF | 1 Comment

Android requires .class compatibility set to 5.0. Please fix project properties

I got this error message when I created my first android application. Right after creating the application, when I tried to run, the error message was displayed. I fixed this error message by selecting the android sdk version in the java build path. That is, select the project and goto its properties. Select Java Build Path, check the android sdk version in the ‘order and export’ tab. See the image below,

this will fix the error message. Now you will be able to run your android application.



October 27, 2010 Posted by | Android | 2 Comments

Setup Android development environment

In this post, I am going to explain the steps involved in setting up Android development environment.

Android Development Environment for Windows 

REF: http://developer.android.com/sdk/index.html

REF: http://developer.android.com/sdk/installing.html

Android SDK

–goto developer.android.com & SDK (Android SDK 2.2)

–download SDK

Eclipse

–goto www.eclipse.org/downloads

–download Eclipse IDE for Java Developers for Windows (Eclipse IDE for Java Developers Windows 32 bit)

JDK

–goto java.sun.com/javase/downloads

–download JDK 6

-Install JDK first

-Extract Android to D:\AndroidSDK. Follow the instructions given in the SDK Readme.txt file.

-Extract Eclipse package

-Open Eclipse

-Choose Workspace

Configure Android Development Tools Plug-in

—goto Help->Install New Software

—Click on Add button

—Type Google Android as the name in the popup

—TYpe https://dl-ssl.google.com/android/eclipse in the Location and click OK

–Check the checkbox & click next

–Accept the Terms & Conditions and click on Finish
 

–Restart Eclipse

–Goto Window -> Preferences.

–Select Android from left pane

–Select the Android SDK location and Apply

AVD
You must create an Android Virtual Device (AVD).
An AVD defines the system image and device settings used by the emulator.

-Create an AVD:

—In Eclipse, choose Window > Android SDK and AVD Manager.
—Select Virtual Devices in the left panel.
—Click New. The Create New AVD dialog appears.
—Keyin the name of the AVD, such as “demoavd”.
—Choose a target platform (say, android 2.2) you want to run on the emulator.
—Click Create AVD.

—————————–

Create a new project

—————————–

-File>New>Project

-Choose an Android project

-Specify Project Name

Thats all! Enjoy creating Android applications!

October 25, 2010 Posted by | Android | 4 Comments