Retrieving data from Firebase database through use of a C# application.
In this post we will be going through the way in which you can go about retrieving updated data in Googles Firebase database using a C# application. Retrieving data has been an extremely large request and so its finally here. Please note that if you are wanting to retrieve data that is being posted or inserted in to Firebase the code will need some tweaking and will need to have some changes made. We will be going through this in the next article.
Please also note that all code we run using Firebase is carried out in test mode, meaning there is no authentication, no password or username.
The retrieval of data in itself can be seen to be something thats pretty straight forward, using a HTTP request and stream reader you can very quickly get hold of the data within Firebase. HOWEVER, the information will always be retrieved in JSON and as seen in the post prior to this we can see that the entire JSON tree is retrieved, click here to read previous post. For those unsure as to what a JSON tree may look like please refer to the below for a very basic example: –
{
"ID": {56383},
"Username": {Gamer_Boy},
"Level": {Legendary},
"Current_Score": {156},
"Time": {00:02:54}
}
The above could represent the data for an online game whereby the data for an individual user is being held and constantly updated in terms of his score and time, information such as the level and username will be changed only when needed. Retrieving the entire piece of information above is useful although cant be processed further until its being parsed or deserialised in to single variables we can then go about making use of. By doing so, the above would turn 5 individual string variables being ID, Username, Level, Current score and Time.
So we will begin now looking at the code in issue and to start with as usual we will focus on the Include Headers being used.
using System;
using System.Windows.Forms;
using System.Net;
using System.IO;
using Newtonsoft.Json.Linq;
In this example we make use of the Newton.Json.Linq namespace. Its use to us is of extreme importance as it enables us the ability to effectively retrieve data in Json, parse it and the go about creating individual strings. All other headings are those that are included in previous posts to do with the inserting and updating of data in to Firebase using C#.
string URL = "https://myfirebaseproject.firebaseio.com/.json";
var HTTPrequest = (HttpWebRequest)WebRequest.Create(URL);
var Response = (HttpWebResponse)HTTPrequest.GetResponse();
var StreamReader = new StreamReader(Response.GetResponseStream()).ReadToEnd();
var Data = JObject.Parse(StreamReader);
string Second = Data["LatestSecond"].ToObject();
Console.WriteLine(Second);
We first go about creating a string named URL which will be used in our HTTP web request, this link should contain the URL of your Firebase project followed by /.json. Please ensure you have entered this in correctly before proceeding onwards. In the next line we then go on to making a request to our Firebase project online via the use of a HTTP web request where the URL string is then put to use. We then create a HTTP response, which by default will give you the entire Json tree of data you have. With the Newton.Json.Linq namespace we can make use of the JObect.Parse class to parse the Json data received. Once parsed we then move on to declaring a new string named “Second”. This “Second” variable will be the equivalent of the node named LatestSecond within Json. Which is then displayed in the console.
If your variable is named something other than LatestSecond which is more than likely be sure to then make the appropriate changes to the second to last line in the above snippet. This should match exactly as that seen in firebase refer to the below for a better illustration.
{
“Username”: {User009},
“Score”: {10123}
}
The above is an example of what your data structure may look like in firebase where the score is a number that is continuously being incremented. And so if wanting to retrieve the updated of the score you would go about replacing the word LatestSecond with Score. If you are having any issues with the do send us a message via the contact page so that we may be able to provide assistance. We will have a comments section coming soon where we expect all to be discussing, asking and answering.
The full source code for this project can be found below, when inserting please be sure you have installed the NetGU Package in visual studio as you will face problems if the package isnt already installed.
using System;
using System.Windows.Forms;
using System.Net;
using System.IO;
using Newtonsoft.Json.Linq;
namespace WindowsFormsApplication
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
while (true)
{
string URL = "https://myfirebaseproject.firebaseio.com/.json";
var HTTPrequest = (HttpWebRequest)WebRequest.Create(URL);
var Response = (HttpWebResponse)HTTPrequest.GetResponse();
var StreamReader = new StreamReader(Response.GetResponseStream()).ReadToEnd();
var Data = JObject.Parse(StreamReader);
string Second = Data["LatestSecond"].ToObject();
Console.WriteLine(Second);
}
}
}
}