Xamarin Simple Web API Service Call To Save Function For Beginners

Introduction

 
Xamarin is one of the best Android application development platforms. It's easy to manage the source code and large-scale applications are easy to develop and support. For example, for building a simple mobile contact application, the resource layout is Main.axml, Class name is Users.cs, Newtonsoft.Json, System.Net, and System.Net.Http is reference. AXML forms the design, and  String.Xml is declares the variable name.
 
Step 1
 
Create a new project
 
 
Step 2
 
Select Android App (Xamarin)
 
 
Step 3
 
Choose Blank App and check your Android Version
 
 
Step 4
 
Create Main.axml and User.cs class file to add references Newtondost.json, System.Net and System.Net.Http.
 
 
Step 5
 
Design the Main.axml
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  3.     android:orientation="vertical"    
  4.     android:layout_width="match_parent"    
  5.     android:layout_height="match_parent">    
  6.  <EditText     
  7.       android:id = "@+id/userId"     
  8.       android:layout_width = "match_parent"     
  9.       android:layout_height = "wrap_content"     
  10.       android:hint = "userId"     
  11.       android:textColorHint = "@android:color/background_dark"     
  12.       android:textColor = "@android:color/background_dark" />    
  13.   <EditText     
  14.       android:id = "@+id/Username"     
  15.       android:layout_width = "match_parent"     
  16.       android:layout_height = "wrap_content"     
  17.       android:hint = "Username"     
  18.       android:textColorHint = "@android:color/background_dark"     
  19.       android:textColor = "@android:color/background_dark" />    
  20.   <EditText     
  21.       android:id = "@+id/MobileNo"     
  22.       android:layout_width = "match_parent"     
  23.       android:layout_height = "wrap_content"     
  24.       android:hint = "Mobile No"     
  25.       android:textColorHint = "@android:color/background_dark"     
  26.       android:textColor = "@android:color/background_dark" />    
  27.        
  28. <Button     
  29.    android:id = "@+id/MyButtonSave"     
  30.    android:layout_width = "fill_parent"     
  31.    android:layout_height = "wrap_content"     
  32.    android:text = "@string/BtnSave" />     
  33.     
  34. </LinearLayout>     
 
Step 6
 
Strings.xml is resources part declaring the variable name.
  1. <resources>  
  2.     <string name="app_name">App9</string>  
  3.     <string name="action_settings">Settings</string>  
  4.   
  5.   <string name = "BtnSave">Save</string>   
  6. </resources>  
Step 7
 
Create a Class User.cs.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. using Android.App;  
  7. using Android.Content;  
  8. using Android.OS;  
  9. using Android.Runtime;  
  10. using Android.Views;  
  11. using Android.Widget;  
  12.   
  13. namespace App9  
  14. {  
  15.     public class Users  
  16.     {  
  17.         public int ID { getset; }  
  18.         public string Username { getset; }  
  19.         public string Mobile { getset; }  
  20.     }  
  21. }  
Step 8
 
Create button click event on MainActivity.cs.
  1. using Android.App;  
  2. using Android.OS;  
  3. using Android.Support.V7.App;  
  4. using Android.Runtime;  
  5. using Android.Widget;  
  6. using System;  
  7. using System.Net;  
  8. using System.Text;  
  9. using System.IO;  
  10. using System.Net.Http;  
  11. using Newtonsoft.Json;  
  12.   
  13. namespace App9  
  14. {  
  15.     [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]  
  16.     public class MainActivity : AppCompatActivity  
  17.     {  
  18.         protected override void OnCreate(Bundle savedInstanceState)  
  19.         {  
  20.             base.OnCreate(savedInstanceState);  
  21.             // Set our view from the "main" layout resource  
  22.             SetContentView(Resource.Layout.Main);  
  23.             Button btnInsert = FindViewById<Button>(Resource.Id.MyButtonSave);  
  24.   
  25.             TextView UserId = FindViewById<TextView>(Resource.Id.userId);  
  26.             TextView UserName = FindViewById<TextView>(Resource.Id.Username);  
  27.             TextView Mobileno = FindViewById<TextView>(Resource.Id.MobileNo);  
  28.   
  29.             btnInsert.Click += async delegate  
  30.             {  
  31.                 using (var client = new HttpClient())  
  32.                 {  
  33.                     Users objProduct = new Users();  
  34.                     objProduct.Username = UserName.Text;  
  35.                     objProduct.Mobile = Mobileno.Text;  
  36.                     string json = JsonConvert.SerializeObject(objProduct);  
  37.   
  38.                     var baseAddress = "http://localhost:2514/api/values/GetMobileUpdate?ID=" + UserId.Text + "&Username=" + UserName.Text + "&Mobile=" + Mobileno.Text + "";  
  39.   
  40.                     var http = (HttpWebRequest)WebRequest.Create(new Uri(baseAddress));  
  41.                     http.Accept = "application/json";  
  42.                     http.ContentType = "application/json";  
  43.                     http.Method = "PUT";  
  44.   
  45.                     string parsedContent = json;  
  46.                     ASCIIEncoding encoding = new ASCIIEncoding();  
  47.                     Byte[] bytes = encoding.GetBytes(parsedContent);  
  48.   
  49.                     Stream newStream = http.GetRequestStream();  
  50.                     newStream.Write(bytes, 0, bytes.Length);  
  51.                     newStream.Close();  
  52.   
  53.                     var response = http.GetResponse();  
  54.   
  55.                     var stream = response.GetResponseStream();  
  56.                       
  57.                 }  
  58.             };  
  59.         }  
  60.     }  
  61. }  

Summary

 
C# developer is easy to handle on Xamarin application development. The Same method override helps to make the on click event response. 
 
I have "Put" option as the only one created. Try to carry out  "Get", "Post" and "Delete" options. I hope this method helps you to make API Web service calls in Xamarin Android application development.


Similar Articles