Download Code

Xamarin Web Services Example Android

In this tutorial you will learn about Xamarin Web Services as a Xamarin Web Services Example android application.You will learn about REST Services with practical example.

Let me give you some basic knowledge first.Most of the time people think that what actually web service is?

Web Services

Web Services are services that help client,server applications to communicate with each other using some standard like XML,SOAP via WWW.

REST

Representational State Transfer (REST) is an architectural style for building web services. REST requests are made over HTTP using the same HTTP verbs that web browsers use to retrieve web pages and to send data to servers. The verbs are:

  • GET – this operation is used to retrieve data from the web service.
  • POST – this operation is used to create a new item of data on the web service.
  • PUT – this operation is used to update an item of data on the web service.
  • PATCH – this operation is used to update an item of data on the web service by describing a set of instructions about how the item should be modified. This verb is not used in the sample application.
  • DELETE – this operation is used to delete an item of data on the web service.

Sticking to this tutorial you will just use GET method.Let’s talk about some classes which you will use to make HTTP Request etc.

WebRequest

WebRequest makes a request to URI(Uniform Resource Identifier) means url of any website/server.

HttpWebRequest

HttpWebRequest Provides an HTTP-specific implementation of the WebRequest class.

WebResponse

WebResponse provides a response from URI(Uniform Resource Identifier) means url of any website/server.

HttpWebResponse

HttpWebResponse provides HTTP-specific implementation of WebResponse class.

Stream Reader

This class is used to read byte stream in particular encoding.

Assert

This class has multiple methods but here you will use NotNull to check whether string is null or not.

Create new android application project.Before going to Main.axml file go to References right click and add following references.

  • System.Net
  • NUnit.Framework

Now go to Main.axml file and add Scroll View,TextView.

xamarin web services android, xamarin web servies example, xamarin web services example android, web services example android, how to send get request to server in xamarin android, xamarin HttpWebRequest android, Xamarin HttpWebResponse android, xamarin Assert android, xamarin streamreader,

MainActivity.cs

using Android.App;
using Android.Widget;
using Android.OS;
using System.Net;
using System;
using System.IO;
using NUnit.Framework;

namespace XamarinHTTP
{
	[Activity(Label = "XamarinHTTP", MainLauncher = true, Icon = "@mipmap/icon")]
	public class MainActivity : Activity
	{
		TextView txt;

		protected override void OnCreate(Bundle savedInstanceState)
		{
			base.OnCreate(savedInstanceState);

			// Set our view from the "main" layout resource
			SetContentView(Resource.Layout.Main);
			txt = (TextView)FindViewById(Resource.Id.display);

			var content = "";
			// setting url to which you will send request using HttpWebRequest
			var request = HttpWebRequest.Create(string.Format(@"http://www.androidcodec.com"));    

			request.ContentType = "application/json";    // setting content type of received stream
			request.Method = "GET";                 // request method "Get"

			// getting response from Server/URL using HttpWebResponse class
			using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
			{
				// check data fetching is going well or not
				if (response.StatusCode != HttpStatusCode.OK)
					Console.Out.WriteLine("Error fetching data. Server returned status code: {0}", response.StatusCode);
				// read stream from response header and stored in streamreader object 
				using (StreamReader reader = new StreamReader(response.GetResponseStream()))
				{
					//converting content to string and stored in content varibale
					content = reader.ReadToEnd();
					if (string.IsNullOrWhiteSpace(content))
					{
						Console.Out.WriteLine("Response contained empty body...");
					}
					else {
						Console.Out.WriteLine("Response Body: \r\n {0}", content);
					}
					// checking whether string is notnull
					Assert.NotNull(content);
				}
			}
			txt.Text = content;

		}
	}
}

At last go to Properties > Android Manifest.xml file and add INTERNET permission.

Run your android application project.

xamarin web services android, xamarin web servies example, xamarin web services example android, web services example android, how to send get request to server in xamarin android, xamarin HttpWebRequest android, Xamarin HttpWebResponse android, xamarin Assert android, xamarin streamreader,

That’s it for this tutorial.

For Xamarin Web Services Example Android like tutorial in Java visit Android Codec.

Thanks