How To Send Data From Android To SQL Server Using RESTful API

Introduction

Most of the Android applications use an API to send and receive data from server to client. API is very useful when applications need to send or receive data.

In this tutorial I will show you how to create a Restful JSON based API for Android application using ASP.NET and Microsoft SQL Server database.

Overview of the tutorial

This tutorial shows you the basic implementation of the RESTful API and will let you know ow to create a Restful API in Visual Studio with ASP.NET using C#.

How to connect android application with SQL Server Database

In this tutorial I am using Visual Studio 2015 (you can use any version) and Android 1.5.1 version with minimum SDK API 18: Android 2.2 (Froyo).

First we create an API

Step I:

Download required resources:

Download three JSON service DLL files from the link....

Step II:

Open Visual Studio and create ASP.NET Empty Web Application project.

For this:

Go to File, then New. Click Project. From new project window select web category. Then select ASP.NET Empty Web Application and give it a name. Select project location from Browse. Make sure Create Directory for Solution is checked. Click Ok.

Now add DLL files in the .Net web application.

  1. Right click on the project in Solution Explorer
  2. Form the menu, click on Add Reference to open the Reference Manager
  3. Click on Browse from left panel
  4. Then click browse button at bottom of Reference Manager
  5. Go to location where save the download files
  6. Select three .dll files(which you have is download) and click on add button
  7. Click ok

Now you need to add a handler class. For this,

  1. Go to project menu then click Add New Item.
  2. From Add New Item, select web category from left panel.
  3. From center select Generic Handler.
  4. Give it a name or left it default and click on Add button.

Now create three new classes. To add class:

  1. Go to project menu and click on Add New Item.
  2. From the code category select class.
  3. Give class name as SAPI.cs.
  4. Click Add button.

In the same way add two more classes named ISAPI and DBConnect.

Step III:

SQL Server Database Creation

Open SQL Server Management Studio and connect with server.

Create a database:

  1. Right click on Database folder in Object Explorer and select new database.
  2. Give database a name (I have given serverdb) and click OK.

Now create tables. To create table:

  1. Click on created database and click on New Query.
  2. SQLQuery window will open.
  3. Now execute the following SQL Query to create table,

    Sql query to create table

    1. CREATE TABLE UserData (  
    2. Name VARCHAR(50),  
    3. userName VARCHAR(50),  
    4. password VARCHAR(50),  
    5. PhoneNumber VARCHAR(50),  
    6. CNIC VARCHAR(50),  

Step IV:

Make API and Connect Database with this and go back to visual studio.

Now we programmatically save information in database. To configure a connection open web.config file in Solution Explorer. Add the following line between <configuratio> and </configuration> tag
  1. <connectionStrings>  
  2.     <add name="ConString" connectionString="Data Source=DESKTOP-AU9BNOB;Initial Catalog=serverProjectdb;Integrated Security=True" providerName="System.Data.SqlCliesnt" />  
  3. </connectionStrings>  
In these lines change data source value to your Server Name. In the initial catalog change the database name with yours.

Now open DBConnect.cs class. Copy the following code into it and change the namespace with your project name.
  1. using System.Configuration;  
  2. using System.Data.SqlClient;  
  3.   
  4. namespace TestProjectAPI  
  5. {  
  6.     /// This class is used to connect to sql server database  
  7.     public class DBConnect   
  8.     {  
  9.         private static SqlConnection NewCon;  
  10.         private static stringconStr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;  
  11.         public static SqlConnection getConnection()  
  12.         {  
  13.             NewCon = new SqlConnection(conStr);  
  14.             return NewCon;  
  15.         }  
  16.         public DBConnect()  
  17.         {  
  18.   
  19.         }  
  20.     }  
  21. }  
Step V:

Configuring the API

To configure API interface open Handler.ashx file and copy the following code into it. Use correct namespace in your code.
  1. using JsonServices;  
  2. using JsonServices.Web;  
  3.   
  4. namespace TestProjectAPI  
  5. {  
  6.     public classHandler1: JsonHandler  
  7.     {  
  8.         public Handler1()   
  9.         {  
  10.             this.service.Name = " TestProjectAPI ";  
  11.             this.service.Description = "JSON API for android appliation";  
  12.             InterfaceConfiguration IConfig = new InterfaceConfiguration("RestAPI"typeof(ISAPI), typeof(SAPI));  
  13.             this.service.Interfaces.Add(IConfig);  
  14.         }  
  15.     }  
  16. }  
Now we need to change ISAPI.cs class to an interface class. To do this open ISAPI.cs and the class keyword to interface. And extend the SAPI.cs class with this Interface ISAPI.cs.
 
Here's the code to modify both classes.
 
ISAPI.cs
  1.  
  2. namespace TestProjectAPI  
  3. {  
  4.     public interface ISAPI   
  5.     {  
  6.   
  7.     }  
 SAPI.cs
  1. namespace TestProjectAPI    
  2. {    
  3.     public interface SAPI: ISAPI    
  4.     {    
  5.     
  6.     }    
  7. }   
Now open ISAPI.cs and copy the following code. You can change methods according to your requirements.
  1. using System.Data;  
  2.   
  3. namespace TestProjectAPI  
  4. {  
  5.     /// This interface declare the methods need to be implement.  
  6.     public interface ISAPI  
  7.     {  
  8.         void CreateNewAccount(string Name, string userName, string password, string PhoneNumber, string CNIC);  
  9.     }  
  10. }  
Now open SAPI.cs and add the following code.
  1. using System;  
  2. using System.Data;  
  3. using System.Data.SqlClient;  
  4.   
  5. namespace TestProjectAPI  
  6. {  
  7.     public classSAPI: ISAPI  
  8.       
  9.    {  
  10.         SqlConnection dbConnection;  
  11.         public ServiceAPI()  
  12.         {  
  13.             dbConnection = DBConnect.getConnection();  
  14.         }  
  15.         public void CreateNewAccount(string Name, string userName, string password, string PhoneNumber, string CNIC) {  
  16.             if (dbConnection.State.ToString() == "Closed") {  
  17.                 dbConnection.Open();  
  18.             }  
  19.   
  20.             string query = "INSERT INTO UserDetails VALUES ('" + Name + "','" + userName + "','" + password + "','" + PhoneNumber + "','" + CNIC + "');";  
  21.   
  22.             SqlCommand command = new SqlCommand(query, dbConnection);  
  23.             command.ExecuteNonQuery();  
  24.   
  25.             dbConnection.Close();  
  26.         }  
  27.     }  
Now we have successful code API.

Step VI:

Now run the API in your browser. At first the page gives you the error.

error

To solve this problem, simply add “/Handler1.ashx” to the end of URL. In my case complete URL is “localhost:49702/Handler.ashx”. If you use another name of Handler class then add that name instead of Handler1.ashx.

After adding this, if you see below type of screen it means your API is working.

error

Now at the end of URL add “?ANDROID” and complete URL becomes “localhost:49702/Handler.ashx?ANDROID”.

Press enter and it will prompt you to save a zipped file.

Now extract that zip file and start coding with Android Studio.

Step I:

Creating Android project.

Name the project Navigation
 
Only one activity which is Main Activity.

Step II:

Open extracted file, then copy and paste the RestAPI.java file into your project. Change the package value with your package name.

Step 3:

Add the following permission into AndroidMenifest.xml

<uses-permissionandroid:name="android.permission.INTERNET"/>

Step 4:

Open the activity_main.xml file and add the following code into it.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.zubariaashraf.navigation.CreateUserActivity" android:background="@drawable/imag">  
  3.   
  4.     <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/create_user_account" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" />  
  5.   
  6.     <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textPersonName" android:ems="10" android:id="@+id/name" android:layout_below="@+id/textView1" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" />  
  7.   
  8.     <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/uname" android:layout_below="@+id/name" android:layout_alignRight="@+id/name" android:layout_alignEnd="@+id/name" android:layout_alignLeft="@+id/name" android:layout_alignStart="@+id/name" />  
  9.   
  10.     <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textPassword" android:ems="10" android:id="@+id/upassword" android:layout_below="@+id/uname" android:layout_alignRight="@+id/uname" android:layout_alignEnd="@+id/uname" />  
  11.   
  12.     <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textPassword" android:ems="10" android:id="@+id/uRepassword" android:layout_below="@+id/upassword" android:layout_alignRight="@+id/upassword" android:layout_alignEnd="@+id/upassword" />  
  13.   
  14.     <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="phone" android:ems="10" android:id="@+id/phonenum" android:layout_below="@+id/uRepassword" android:layout_alignRight="@+id/uRepassword" android:layout_alignEnd="@+id/uRepassword" />  
  15.   
  16.     <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="number" android:ems="10" android:id="@+id/cnic" android:layout_below="@+id/phonenum" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" />  
  17.   
  18.     <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Name:" android:id="@+id/textView" android:layout_alignParentLeft="true" android:layout_above="@+id/uname" />  
  19.   
  20.     <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="User Name:" android:id="@+id/textView5" android:layout_above="@+id/upassword" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" />  
  21.   
  22.     <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Password:" android:id="@+id/textView6" android:layout_alignBottom="@+id/upassword" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" />  
  23.   
  24.     <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Re-Password:" android:id="@+id/textView7" android:layout_above="@+id/phonenum" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" />  
  25.   
  26.     <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Phone #:" android:id="@+id/textView8" android:layout_alignBottom="@+id/phonenum" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" />  
  27.   
  28.     <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="CNIC:" android:id="@+id/textView9" android:layout_alignBottom="@+id/cnic" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" />  
  29.   
  30.     <Button android:id="@+id/btn_createuser" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/create_user_account" android:layout_alignParentBottom="true" android:layout_toRightOf="@+id/textView8" android:layout_toEndOf="@+id/textView8" android:layout_marginBottom="42dp" />  
  31.   
  32.     <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/viewid" android:layout_above="@+id/btn_createuser" android:layout_alignRight="@+id/cnic" android:layout_alignEnd="@+id/cnic" />  
  33.   
  34.     <!-- <TextView  
  35. android:id="@+id/textView3"  
  36. android:layout_width="wrap_content"  
  37. android:layout_height="wrap_content"  
  38. android:layout_alignLeft="@+id/textView2"  
  39. android:layout_below="@+id/textView2"  
  40. android:layout_marginTop="22dp"  
  41. android:text="@string/lastname"  
  42. android:textAppearance="?android:attr/textAppearanceMedium" />  
  43.   
  44. <TextView  
  45. android:id="@+id/textView4"  
  46. android:layout_width="wrap_content"  
  47. android:layout_height="wrap_content"  
  48. android:layout_alignRight="@+id/textView3"  
  49. android:layout_below="@+id/textView3"  
  50. android:layout_marginTop="20dp"  
  51. android:text="@string/username"  
  52. android:textAppearance="?android:attr/textAppearanceMedium" />  
  53.   
  54. <TextView  
  55. android:id="@+id/textView5"  
  56. android:layout_width="wrap_content"  
  57. android:layout_height="wrap_content"  
  58. android:layout_alignRight="@+id/textView4"  
  59. android:layout_below="@+id/textView4"  
  60. android:layout_marginTop="20dp"  
  61. android:text="@string/password"  
  62. android:textAppearance="?android:attr/textAppearanceMedium" />  
  63.   
  64. <TextView  
  65. android:id="@+id/textView2"  
  66. android:layout_width="wrap_content"  
  67. android:layout_height="wrap_content"  
  68. android:text="@string/firstname"  
  69. android:textAppearance="?android:attr/textAppearanceMedium"  
  70. android:layout_below="@+id/textView1"  
  71. android:layout_alignParentLeft="true"  
  72. android:layout_alignParentStart="true" />  
  73.   
  74. <EditText  
  75. android:id="@+id/et_fisrtname"  
  76. android:layout_width="wrap_content"  
  77. android:layout_height="wrap_content"  
  78. android:layout_alignBaseline="@+id/textView2"  
  79. android:layout_alignBottom="@+id/textView2"  
  80. android:layout_alignParentRight="true"  
  81. android:ems="10"  
  82. android:layout_toRightOf="@+id/textView6"  
  83. android:layout_toEndOf="@+id/textView6">  
  84.   
  85. <requestFocus />  
  86. </EditText>  
  87.   
  88. <EditText  
  89. android:id="@+id/et_lastname"  
  90. android:layout_width="wrap_content"  
  91. android:layout_height="wrap_content"  
  92. android:layout_alignBaseline="@+id/textView3"  
  93. android:layout_alignBottom="@+id/textView3"  
  94. android:layout_alignLeft="@+id/et_fisrtname"  
  95. android:layout_alignRight="@+id/et_fisrtname"  
  96. android:ems="10" />  
  97.   
  98. <EditText  
  99. android:id="@+id/et_cu_username"  
  100. android:layout_width="wrap_content"  
  101. android:layout_height="wrap_content"  
  102. android:layout_alignBaseline="@+id/textView4"  
  103. android:layout_alignBottom="@+id/textView4"  
  104. android:layout_alignLeft="@+id/et_lastname"  
  105. android:layout_alignRight="@+id/et_lastname"  
  106. android:ems="10" />  
  107.   
  108. <EditText  
  109. android:id="@+id/et_cu_password"  
  110. android:layout_width="wrap_content"  
  111. android:layout_height="wrap_content"  
  112. android:layout_alignBaseline="@+id/textView5"  
  113. android:layout_alignBottom="@+id/textView5"  
  114. android:layout_alignLeft="@+id/et_cu_username"  
  115. android:layout_alignRight="@+id/et_cu_username"  
  116. android:ems="10" />  
  117.   
  118. <Button  
  119. android:id="@+id/btn_createuser"  
  120. android:layout_width="wrap_content"  
  121. android:layout_height="wrap_content"  
  122. android:text="@string/create_user_account"  
  123. android:layout_alignParentBottom="true"  
  124. android:layout_centerHorizontal="true" />  
  125.   
  126. <EditText  
  127. android:layout_width="wrap_content"  
  128. android:layout_height="wrap_content"  
  129. android:inputType="textPassword"  
  130. android:ems="10"  
  131. android:id="@+id/editText"  
  132. android:layout_below="@+id/et_cu_password"  
  133. android:layout_alignParentRight="true"  
  134. android:layout_alignParentEnd="true"  
  135. android:layout_alignLeft="@+id/et_cu_password"  
  136. android:layout_alignStart="@+id/et_cu_password" />  
  137.   
  138. <TextView  
  139. android:layout_width="wrap_content"  
  140. android:layout_height="wrap_content"  
  141. android:text="Re-password:"  
  142. android:id="@+id/textView"  
  143. android:layout_alignBottom="@+id/editText"  
  144. android:layout_alignLeft="@+id/textView5"  
  145. android:layout_alignStart="@+id/textView5" />  
  146.   
  147. <EditText  
  148. android:layout_width="wrap_content"  
  149. android:layout_height="wrap_content"  
  150. android:inputType="phone"  
  151. android:ems="10"  
  152. android:id="@+id/editText2"  
  153. android:layout_below="@+id/editText"  
  154. android:layout_alignParentRight="true"  
  155. android:layout_alignParentEnd="true"  
  156. android:layout_alignLeft="@+id/editText"  
  157. android:layout_alignStart="@+id/editText" />  
  158.   
  159. <TextView  
  160. android:layout_width="wrap_content"  
  161. android:layout_height="wrap_content"  
  162. android:textAppearance="?android:attr/textAppearanceMedium"  
  163. android:text="Phone #:"  
  164. android:id="@+id/textView6"  
  165. android:layout_alignBottom="@+id/editText2"  
  166. android:layout_alignLeft="@+id/textView"  
  167. android:layout_alignStart="@+id/textView" />  
  168.   
  169. <TextView  
  170. android:layout_width="wrap_content"  
  171. android:layout_height="wrap_content"  
  172. android:textAppearance="?android:attr/textAppearanceMedium"  
  173. android:text="CNIC:"  
  174. android:id="@+id/textView7"  
  175. android:layout_below="@+id/editText2"  
  176. android:layout_alignLeft="@+id/textView4"  
  177. android:layout_alignStart="@+id/textView4"  
  178. android:layout_alignParentStart="false" />-->  
  179.   
  180.   
  181.   
  182. </RelativeLayout>  
Step 5

Add two more classes named

UserDetailsTable.java and JSONParser.java.
UserDetailsTable.java class map database objects to java objects.
JSONParser.java class used to parse JSONObjects

Step 6

Now open UserDetailsTable.java and add the following code into it.
  1. package com.example.zubariaashraf.navigation;  
  2.   
  3. import android.content.Intent;  
  4.   
  5. /** 
  6.  * Created by Zubaria Ashraf on 8/2/2016. 
  7.  */  
  8. public class UserDetailsTable  
  9. {  
  10.     String Name, userName, password, PhoneNumber, CNIC;  
  11.     int id;  
  12.   
  13.     public UserDetailsTable(String Name, String userName,  
  14.         String password, String PhoneNumber, String CNIC)  
  15.     {  
  16.         super();  
  17.         this.id = id;  
  18.         this.Name = Name;  
  19.         this.userName = userName;  
  20.         this.password = password;  
  21.         this.PhoneNumber = PhoneNumber;  
  22.         this.CNIC = CNIC;  
  23.     }  
  24.   
  25.     public UserDetailsTable()  
  26.     {  
  27.         super();  
  28.         this.id = 0;  
  29.         this.Name = null;  
  30.         this.userName = null;  
  31.         this.password = null;  
  32.         this.PhoneNumber = null;  
  33.         this.CNIC = null;  
  34.   
  35.   
  36.     }  
  37.   
  38.     public int getId()  
  39.     {  
  40.         return id;  
  41.     }  
  42.     public void setId(int id)  
  43.     {  
  44.         this.id = id;  
  45.     }  
  46.   
  47.     public String getName()  
  48.     {  
  49.         return Name;  
  50.     }  
  51.   
  52.     public void setName(String Name)   
  53.     {  
  54.         this.Name = Name;  
  55.     }  
  56.   
  57.     public String getUserName()  
  58.     {  
  59.         return userName;  
  60.     }  
  61.   
  62.     public void setUserName(String userName)  
  63.     {  
  64.         this.userName = userName;  
  65.     }  
  66.   
  67.     public String getPassword()  
  68.     {  
  69.         return password;  
  70.     }  
  71.   
  72.     public void setPassword(String password)  
  73.     {  
  74.         this.password = password;  
  75.     }  
  76.   
  77.     public String getPhoneNumber()  
  78.     {  
  79.         return PhoneNumber;  
  80.     }  
  81.   
  82.     public void setPhoneNumber(String PhoneNumber)  
  83.     {  
  84.         this.PhoneNumber = PhoneNumber;  
  85.     }  
  86.   
  87.     public String getCNIC()  
  88.     {  
  89.         return CNIC;  
  90.     }  
  91.   
  92.     public void setCNIC(String CNIC)   
  93.     {  
  94.         this.CNIC = CNIC;  
  95.     }  
  96.   
  97. }  
Now open JSONParser.java and add the following code into it. You can modify this class according to your requirements.
  1. package com.example.zubariaashraf.navigation;  
  2.   
  3. import org.json.JSONArray;  
  4. import org.json.JSONException;  
  5. import org.json.JSONObject;  
  6.   
  7. import android.annotation.SuppressLint;  
  8. import android.util.Log;  
  9.   
  10. /** 
  11.  * Created by Zubaria Ashraf on 8/2/2016. 
  12.  */  
  13. public class JSONParser  
  14. {  
  15.   
  16.     public JSONParser()  
  17.     {  
  18.         super();  
  19.     }  
  20. }  
Now open Main Activity.java and add the following code into it.
  1. package com.example.zubariaashraf.navigation;  
  2.   
  3. import android.content.Context;  
  4. import android.os.AsyncTask;  
  5. import android.os.Bundle;  
  6. import android.app.Activity;  
  7. import android.util.Log;  
  8. import android.view.Menu;  
  9. import android.view.MenuItem;  
  10. import android.view.View;  
  11. import android.widget.Button;  
  12. import android.widget.EditText;  
  13. import android.support.v4.app.NavUtils;  
  14. import android.annotation.TargetApi;  
  15. import android.content.Intent;  
  16. import android.os.Build;  
  17. import android.widget.TextView;  
  18. import android.widget.Toast;  
  19.   
  20. import org.json.JSONObject;  
  21.   
  22. import java.sql.SQLException;  
  23.   
  24. public class CreateUserActivity extends Activity  
  25. {  
  26.   
  27.     EditText etName, etUsername, etPassword, rPassword, pNumber, uCnic;  
  28.     Button btnCreateUser;  
  29.     Context context;  
  30.     boolean value;  
  31.     String username;  
  32.   
  33.     @Override  
  34.     protected void onCreate(Bundle savedInstanceState)  
  35.     {  
  36.         super.onCreate(savedInstanceState);  
  37.         setContentView(R.layout.activity_create_user);  
  38.         context = this;  
  39.         Show the Up button in the action bar.  
  40.         setupActionBar();  
  41.   
  42.         etName = (EditText) findViewById(R.id.name);  
  43.         etUsername = (EditText) findViewById(R.id.uname);  
  44.         etPassword = (EditText) findViewById(R.id.upassword);  
  45.         rPassword = (EditText) findViewById(R.id.uRepassword);  
  46.         pNumber = (EditText) findViewById(R.id.phonenum);  
  47.         uCnic = (EditText) findViewById(R.id.cnic);  
  48.   
  49.   
  50.         btnCreateUser = (Button) findViewById(R.id.btn_createuser);  
  51.   
  52.   
  53.         btnCreateUser.setOnClickListener(  
  54.             new View.OnClickListener()  
  55.           {  
  56.   
  57.                 @Override  
  58.                 public void onClick(View v)  
  59.                 {  
  60.                     // TODO Auto-generated method stub  
  61.                     String firstname, password, rePassword, Pnumber, ucnic;  
  62.                     firstname = etName.getText().toString();  
  63.                     username = etUsername.getText().toString();  
  64.                     password = etPassword.getText().toString();  
  65.                     rePassword = rPassword.getText().toString();  
  66.                     Pnumber = pNumber.getText().toString();  
  67.                     ucnic = uCnic.getText().toString();  
  68.   
  69.                     UserDetailsTable userDetail = new UserDetailsTable(firstname, username, password, Pnumber, ucnic);  
  70.                     new AsyncCreateUser().execute(userDetail);  
  71.   
  72.                 }  
  73.             });  
  74.     }  
  75.   
  76.     ////create user account  
  77.   
  78.     protected class AsyncCreateUser extends  
  79.     AsyncTask < UserDetailsTable, Void, Void >   
  80.     {  
  81.   
  82.         @Override  
  83.         protected Void doInBackground(UserDetailsTable...params)  
  84.         {  
  85.   
  86.             RestAPI api = new RestAPI();  
  87.             try   
  88.             {  
  89.   
  90.                 api.CreateNewAccount(params[0].getName(),  
  91.                     params[0].getUserName(),  
  92.                     params[0].getPassword(),  
  93.                     params[0].getPhoneNumber(),  
  94.                     params[0].getCNIC());  
  95.   
  96.             } catch (Exception e)  
  97.             {  
  98.                 // TODO Auto-generated catch block  
  99.                 Log.d("AsyncCreateUser", e.getMessage());  
  100.   
  101.             }  
  102.             return null;  
  103.         }  
  104.   
  105.         @Override  
  106.         protected void onPostExecute(Void result)  
  107.         {  
  108.             store(username);  
  109.             Intent i = new Intent(CreateUserActivity.this, LoginActivity.class);  
  110.             startActivity(i);  
  111.         }  
  112.   
  113.     }  
  114.   
  115.   
  116.     @Override  
  117.     public boolean onOptionsItemSelected(MenuItem item)  
  118.     {  
  119.         switch (item.getItemId())  
  120.         {  
  121.             case android.R.id.home:  
  122.   
  123.                 NavUtils.navigateUpFromSameTask(this);  
  124.                 return true;  
  125.         }  
  126.         return super.onOptionsItemSelected(item);  
  127.     }  
  128.   
  129. }  
Step 7

Now run the application

To run this, the application should know the correct URL of the API. So to add this open the RestAPI class and here you will see an attribute called urlString with "http://DESKTOP-AU9BNOB/Handler1.ashx" URL as in my RestAPI.

So to run this application on emulator you need to host your API on local server. But if you want to run application on real android device then you have to host API in public hosting site.

Run the application and enjoy.

After hosting API on local or public hosting server change the URL from the RestAPI.
 
Read more articles on Android: