An app for windows phone

What I wanted to create was an app that parsed an html site. This because this specific site doesn’t has an official api, so I needed to work around this :mrgreen:

First of all, I needed to get back “into programming”, the last time I’ve programmed was like ages ago ^^ So, back into c#, jej :mrgreen:

After some googling I learned that silverlight in .net worked with an asynchronous call for internet-activities. This ofcourse because a cellular network connection is slow, and you don’t want to stall the GUI.

The method to do this is by using a class called “WebRequest”.
The method is started by using “BeginGetRequestStream“, which starts the fetch in another thread besides the GUI.
When the data is downloaded a callback function is called, so you need to create this as well…

This resulted in some code like the one below:


private void login(string username, string password) {
//the url where we want to go, here no postdata is specified, because the site only uses the postdata in the url
string url = "https://crm.schedom-europe.net/?op=login&username=" + username + "&password=" + password + "&new_language=english&submit=login";

//the initation of a request to a webpage
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
//something for cookies between different httprequests, later more
request.CookieContainer=cookies;
//webrequest are handled syncronous, on completion the callback is called
IAsyncResult result = (IAsyncResult)request.BeginGetResponse(new AsyncCallback(GetStatusesCallBack), request);
}

//callback thread
private void GetStatusesCallBack(IAsyncResult result)
{
string returnValue;
//get the state of the request
var request = (HttpWebRequest)result.AsyncState;
//if completed, put respons in variable
var response = request.EndGetResponse(result);
using (var stream = response.GetResponseStream())
//a streamreader to put the html in a string
using (var reader = new StreamReader(stream))
{
returnValue = reader.ReadToEnd();
}
response.Close();
//a third function to update the gui :-)
getinfo();
}

For my application I needed a method to store some information (email and password). Appearantly each application is given 1MB of datastorage in a database on your phone. More info on this site: http://msdn.microsoft.com/en-us/library/cc221360%28v=VS.95%29.aspx

//calling the default constructor
private IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;
//add a mail of a user to the windows phone database
appSettings.Add("email", "someone@contoso.com");
//to retrieve data from this db
tbResults.Text = (string)appSettings["email"];

I ran into an issue, because I used 2 different WebRequest because I need to fetch 2 different pages (1 to login, a second to get the information from). But when getting the 2nd page, the login was already gone. Probably a cookie issue. So I ran into this site http://cloudstore.blogspot.com/2010/09/clearing-cookies-on-windows-phone-7-or.html which solved my problem.

//first we define a cookie container: a context where our cookies are stored:
private CookieContainer cookies = new CookieContainer();
//in the first request we recall this container
request.CookieContainer=cookies;
//and again in the second request we recall the same container
secondrequest.CookieContainer=cookies;

Then, my second application I wanted to download an image from a website. After some googling found the following method to work: http://stackoverflow.com/questions/2455195/silverlight-windows-phone-7-load-images-from-url

//just to easy, placing an uri as an image source
Uri uri = new Uri("http://locationofimage.jpg", UriKind.Absolute)
image1.Source = new BitmapImage(uri);

And to create a nice and simple overview I wanted to use the default windows “listbox”, but I didn’t know how to use it. Thankfully this website helped me out: http://weblogs.asp.net/psheriff/archive/2010/10/27/windows-phone-list-box-with-images.aspx

Advertenties
3 reacties

Geef een reactie

Vul je gegevens in of klik op een icoon om in te loggen.

WordPress.com logo

Je reageert onder je WordPress.com account. Log uit / Bijwerken )

Twitter-afbeelding

Je reageert onder je Twitter account. Log uit / Bijwerken )

Facebook foto

Je reageert onder je Facebook account. Log uit / Bijwerken )

Google+ photo

Je reageert onder je Google+ account. Log uit / Bijwerken )

Verbinden met %s

%d bloggers liken dit: