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
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
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <EditText
- android:id = "@+id/userId"
- android:layout_width = "match_parent"
- android:layout_height = "wrap_content"
- android:hint = "userId"
- android:textColorHint = "@android:color/background_dark"
- android:textColor = "@android:color/background_dark" />
- <EditText
- android:id = "@+id/Username"
- android:layout_width = "match_parent"
- android:layout_height = "wrap_content"
- android:hint = "Username"
- android:textColorHint = "@android:color/background_dark"
- android:textColor = "@android:color/background_dark" />
- <EditText
- android:id = "@+id/MobileNo"
- android:layout_width = "match_parent"
- android:layout_height = "wrap_content"
- android:hint = "Mobile No"
- android:textColorHint = "@android:color/background_dark"
- android:textColor = "@android:color/background_dark" />
-
- <Button
- android:id = "@+id/MyButtonSave"
- android:layout_width = "fill_parent"
- android:layout_height = "wrap_content"
- android:text = "@string/BtnSave" />
-
- </LinearLayout>
Step 6
Strings.xml is resources part declaring the variable name.
- <resources>
- <string name="app_name">App9</string>
- <string name="action_settings">Settings</string>
-
- <string name = "BtnSave">Save</string>
- </resources>
Step 7
Create a Class User.cs.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
-
- using Android.App;
- using Android.Content;
- using Android.OS;
- using Android.Runtime;
- using Android.Views;
- using Android.Widget;
-
- namespace App9
- {
- public class Users
- {
- public int ID { get; set; }
- public string Username { get; set; }
- public string Mobile { get; set; }
- }
- }
Step 8
Create button click event on MainActivity.cs.
- using Android.App;
- using Android.OS;
- using Android.Support.V7.App;
- using Android.Runtime;
- using Android.Widget;
- using System;
- using System.Net;
- using System.Text;
- using System.IO;
- using System.Net.Http;
- using Newtonsoft.Json;
-
- namespace App9
- {
- [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
- public class MainActivity : AppCompatActivity
- {
- protected override void OnCreate(Bundle savedInstanceState)
- {
- base.OnCreate(savedInstanceState);
-
- SetContentView(Resource.Layout.Main);
- Button btnInsert = FindViewById<Button>(Resource.Id.MyButtonSave);
-
- TextView UserId = FindViewById<TextView>(Resource.Id.userId);
- TextView UserName = FindViewById<TextView>(Resource.Id.Username);
- TextView Mobileno = FindViewById<TextView>(Resource.Id.MobileNo);
-
- btnInsert.Click += async delegate
- {
- using (var client = new HttpClient())
- {
- Users objProduct = new Users();
- objProduct.Username = UserName.Text;
- objProduct.Mobile = Mobileno.Text;
- string json = JsonConvert.SerializeObject(objProduct);
-
- var baseAddress = "http://localhost:2514/api/values/GetMobileUpdate?ID=" + UserId.Text + "&Username=" + UserName.Text + "&Mobile=" + Mobileno.Text + "";
-
- var http = (HttpWebRequest)WebRequest.Create(new Uri(baseAddress));
- http.Accept = "application/json";
- http.ContentType = "application/json";
- http.Method = "PUT";
-
- string parsedContent = json;
- ASCIIEncoding encoding = new ASCIIEncoding();
- Byte[] bytes = encoding.GetBytes(parsedContent);
-
- Stream newStream = http.GetRequestStream();
- newStream.Write(bytes, 0, bytes.Length);
- newStream.Close();
-
- var response = http.GetResponse();
-
- var stream = response.GetResponseStream();
-
- }
- };
- }
- }
- }
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.