Xamarin Android: Create Login With WEB API Using Azure SQL Server - Part Two

First, create Web API using Azure SQL Server. Refer to part one:

Step 1

Open Visual Studio, New Project, Templates, Visual C#, Android, then select Blank App (Android).

Select Blank App. Then give Project Name and Project Location.

 

Step 2

Next go to Solution Explorer-> Project Name-> Resouces->layout then Right Click to Add then open new Dialog box. Then select Android Layout give name for Reg.axml after that Add New Layout Page.

Step 3

Next go to Solution Explorer-> Project Name then Right Click to Add then open new Dialog box. Then select Activity give name for NewUserActivity.cs after that Add the New Activity Class.

Step 4

Next we need add one Data Layer Class. Go to Solution Explorer-> Project Name then Right Click to Add then open new Dialog box. Then select Class give name for Login.cs after that Add New Class.

 

Step 5

Next go to Solution Explorer-> Project Name-> References then Right Click to Manage Nuget Packages then open new Dialog box. This dialog box to search the Json then Install the Newtonsoft.Json Packages.

 

Step 6

Next go to Open Solution Explorer-> Project Name->Resources->layout->Main.axml Click Open Design View. Here create Large Text, Two Plain Text and Two Buttons.

 

Step 7

Next to Open Solution Explorer-> Project Name->Resources->layout->Reg.axml Click Open Design View. Here create Large Text, Two Plain Text and One Buttons.

 

Step 8

Next to Open Solution Explorer-> Project Name->Login.cs Click Open CS code Page View then create two class name Username and Password.

  1. public string username { getset; }  
  2. public string password { getset; }  

 

Step 9

Next to Open Solution Explorer-> Project Name->MainActivity.cs Click Open CS code Page view then add below namespaces. Next to write given below code.

  1. using System.Net.Http;  
  2. using System.Net.Http.Headers;  

 

C# Code

  1. public class MainActivity: Activity  
  2. {  
  3.     EditText txtusername;  
  4.     EditText txtPassword;  
  5.     Button btncreate;  
  6.     Button btnsign;  
  7.     protected override void OnCreate(Bundle bundle)  
  8.     {  
  9.         base.OnCreate(bundle);  
  10.         // Set our view from the "main" layout resource  
  11.         SetContentView(Resource.Layout.Main);  
  12.         // Get our button from the layout resource,  
  13.         // and attach an event to it  
  14.         btnsign = FindViewById < Button > (Resource.Id.btnloglogin);  
  15.         btncreate = FindViewById < Button > (Resource.Id.btnlogreg);  
  16.         txtusername = FindViewById < EditText > (Resource.Id.txtlogusername);  
  17.         txtPassword = FindViewById < EditText > (Resource.Id.txtlogpassword);  
  18.         btncreate.Click += Btncreate_Click;  
  19.         btnsign.Click += Btnsign_Click;  
  20.     }  
  21.     private async void Btnsign_Click(object sender, EventArgs e)  
  22.     {  
  23.         HttpClient client = new HttpClient();  
  24.         var uri = new Uri(string.Format("http://xamarinlogin.azurewebsites.net/api/Login?username=" + txtusername.Text + "&password=" + txtPassword.Text));  
  25.         HttpResponseMessage response;;  
  26.         client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));  
  27.         response = await client.GetAsync(uri);  
  28.         if (response.StatusCode == System.Net.HttpStatusCode.Accepted)  
  29.         {  
  30.             var errorMessage1 = response.Content.ReadAsStringAsync().Result.Replace("\\", "").Trim(new char[1]  
  31.             {  
  32.                 '"'  
  33.             });  
  34.             Toast.MakeText(this, errorMessage1, ToastLength.Long).Show();  
  35.         }  
  36.         else  
  37.         {  
  38.             var errorMessage1 = response.Content.ReadAsStringAsync().Result.Replace("\\", "").Trim(new char[1]  
  39.             {  
  40.                 '"'  
  41.             });  
  42.             Toast.MakeText(this, errorMessage1, ToastLength.Long).Show();  
  43.         }  
  44.     }  
  45.     private void Btncreate_Click(object sender, EventArgs e)  
  46.     {  
  47.         StartActivity(typeof(NewUserActivity));  
  48.     }  
  49. }   

Step 10

Next to Open Solution Explorer-> Project Name->NewUserActivity.cs Click Open CS code Page view then add below namespaces. Next to write given below code.

  1. using System.Net.Http;    
  2. using System.Net.Http.Headers;    
  3. using Newtonsoft.Json;  
 

C# Code

  1. public class NewUserActivity: Activity  
  2. {  
  3.     EditText txtusername;  
  4.     EditText txtPassword;  
  5.     Button btncreate;  
  6.     protected override void OnCreate(Bundle savedInstanceState)  
  7.     {  
  8.         base.OnCreate(savedInstanceState);  
  9.         // Create your application here  
  10.         SetContentView(Resource.Layout.Reg);  
  11.         txtusername = FindViewById < EditText > (Resource.Id.txtsaveusername);  
  12.         txtPassword = FindViewById < EditText > (Resource.Id.txtsavepassword);  
  13.         btncreate = FindViewById < Button > (Resource.Id.btnsavecreate);  
  14.         btncreate.Click += Btncreate_Click;  
  15.     }  
  16.     private async void Btncreate_Click(object sender, EventArgs e)  
  17.     {  
  18.         Login log = new Login();  
  19.         log.username = txtusername.Text;  
  20.         log.password = txtPassword.Text;  
  21.         HttpClient client = new HttpClient();  
  22.         string url = "http://xamarinlogin.azurewebsites.net/api/Login/";  
  23.         var uri = new Uri(url);  
  24.         client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));  
  25.         HttpResponseMessage response;  
  26.         var json = JsonConvert.SerializeObject(log);  
  27.         var content = new StringContent(json, Encoding.UTF8, "application/json");  
  28.         response = await client.PostAsync(uri, content);  
  29.         if (response.StatusCode == System.Net.HttpStatusCode.Accepted)  
  30.         {  
  31.             var errorMessage1 = response.Content.ReadAsStringAsync().Result.Replace("\\", "").Trim(new char[1]  
  32.             {  
  33.                 '"'  
  34.             });  
  35.             Toast.MakeText(this, errorMessage1, ToastLength.Long).Show();  
  36.         }  
  37.         else  
  38.         {  
  39.             var errorMessage1 = response.Content.ReadAsStringAsync().Result.Replace("\\", "").Trim(new char[1]  
  40.             {  
  41.                 '"'  
  42.             });  
  43.             Toast.MakeText(this, errorMessage1, ToastLength.Long).Show();  
  44.         }  
  45.     }  
  46. }   

Step 11

Press F5 or Build and Run the Application, first image to register the new user and then next image to login the user.

 
 

Finally, we have successfully created Login with WEB API Using Azure SQL Server.


Similar Articles