Pages

Thursday, May 24, 2012

Accessing Web Services from a Mobile Application


Web Services today have became a common technique. Windows CE-based devices, as usual, were several steps behind desktop systems. A couple of months ago, Microsoft finally re-released SP2 for CE.NET, promising significant performance improvements. Thus, recently mobile developers are also able to use most of Web Services' cake. I have no intention to discuss the theory or concept of this technology here; you may freely find a lot of articles on the Web. Also, we won't touch Web Services or Visual Studio.NET basics. What we'll be focused on is what does it all mean in the case of Windows CE.

Creation of a C# Sample Project

As a test sample, we will create a simple client application that will use Amazon's Web Services to query information about available books. So, let's create a new "Smart Device Application" using Visual Studio.NET. The only option actually relevant for Windows CE is to create a C# project. That's the simpliest way to develop Web Services-related applications. If you've used C++, you may either consider using the SOAP toolkit available in WinCE 4.x or, as the most complicated case, write a similar wrapper for SOAP classes for previous versions of Windows CE to assemble SOAP messages manually. The last case may become relevant when you need to achieve high performance benchmarks; for example, when developing an application that performs barcode scanning and then gets item details from a remote server. Our sample is simple, so all such details will be left out of this article's scope.
Well, to be ready to start using Amazon Web Services, you should download Amazon Web Service Developer's Kit from http://www.amazon.com/webservices. The last thing is to apply fora Developer's token, and you're completely set up. So, now, let's add a Web Reference to our project to get a wrapper class for the Web Service. A WSDL file describing its API can be found here. Finally, we have a form to enter the author name and display search results Until now, all we've done is absolutely similar to desktop application development. Now, it's time to take a look at the actual search code.
  1. private void button1_Click(object sender, System.EventArgs e)
  2. {
  3. Cursor.Current = Cursors.WaitCursor;
  4.  
  5. AmazonSearchService srvc = new AmazonSearchService();
  6. WebProxy objProxy = new WebProxy (proxyAddr,proxyPort);
  7. objProxy.BypassProxyOnLocal = true;
  8. objProxy.Credentials = new NetworkCredential (user, password,
  9. domain);
  10. srvc.Proxy = objProxy;
  11. GlobalProxySelection.Select = objProxy;
  12.  
  13. AuthorRequest req = new AuthorRequest();
  14. req.author = SearchText.Text;
  15. req.devtag = DEVTAG;
  16. req.mode = "books";
  17. req.type = "lite";
  18. req.page = "1";
  19.  
  20. try
  21. {
  22. ProductInfo pi = srvc.AuthorSearchRequest(req);
  23. for (int i = 0; i < pi.Details.Length; i++)
  24. {
  25. string s = pi.Details[i].ProductName;
  26. AuthorsLB.Items.Add(s);
  27. }
  28. }
  29. catch(WebException ex)
  30. {
  31. string message = ex.Message;
  32. HttpWebResponse response = (HttpWebResponse)ex.Response;
  33. if(null != response)
  34. {
  35. message = response.StatusDescription;
  36. response.Close();
  37. }
  38. AuthorsLB.Items.Add(message);
  39. }
  40.  
  41. Cursor.Current = Cursors.Default;
  42. }
As you see, first we should create an instance of a service object. If you're connecting to the Internet or an intranet via a proxy, you should also define proxy settings. There was a bug in the CE.NET Web Client; it doesn't look into the credentials of a global WebProxy object to authenticate it to the proxy server. For a workaround, you may define it directly, as shown in the above sample. All the rest of the code proceeds an actual call to Web Service and gets the search result from the remote server. If you were developing Web Service consuming applications for a PC, that's the same story here. Like at Microsoft's presentations, just do a Copy/Paste, and it'll work.
Let's now consider another important aspect of online applications. Your program should verify that the network connection is alive; in other words, requested network resources are available during the application's lifetime. CE.NET framework has no classes to notify applications in response to changes in the network connectivity status. Therefore, mobile apps need to implement their own algorithms of polling active connections and possibly providing caching functionality. The simplest way to verify whether a device is connected to the network is to use a System.Theading.Timer object. When Tick Event occurs, you may use WebRequest and HttpWebResponse objects to send a HTTP GET request to the URL where the Web Service is located and receive an answer. If the status code of the response is equal to HttpStatusCode.OK, the connection to remote server is valid:
  1. // Somewhere in the code
  2. timer1.Interval = 1000;
  3. timer1.Enabled = true;
  4. .........................
  5. // In responase to Tick event of timer
  6. WebRequest req = WebRequest.Create(url);
  7. HttpWebResponse result = (HttpWebResponse)req.GetResponse();
  8. if ( result.StatusCode != HttpStatusCode.OK )
  9. {
  10. // Network connection was lost or remote server is
  11. // unavailable or some other HTTP error occurred
  12. ....
  13. }
Actually that's all you need to start successfully using Web Services on CE.NET mobile devices. If performance issues aren't your headaches, you may just move your C# code to a mobile environment, obviously taking into account all existing restictions. For those of you who develop mobile business applications working with heavy data, C# can't offer a sufficient solution. Therefore, our old buddy C++ is coming from behind the scene.

Downloads

No comments:

Post a Comment