In a previous post I talked about a library that makes it super easy to communicate with web services. In this post, I’ll step back a little bit, explain what exactly a web service is, and go through the process of consuming data from them.
Understanding web services
By its name, a Web service can be any service that is accessed through a web browser, such as Dropbox, Evernote or Gmail. However in software terms, a web service is a program that you can interact with through a network call. In modern web applications, developers have implemented groups of web services called APIs (Application Programming Interface). The concept of an API is not new and it’s not exclusive to web or mobile programming, but I’ll only talk about its application to web and mobile technology.
Ten years ago the idea of having data available for consumption by third-parties was not common, in fact it was avoided at all costs. Companies adopted the approach of “my data is my data, and it’s my competitive advantage”. Today, we have seen that providing access to data is an essential part of companies like Facebook and Twitter. For them, having an API is a way of expanding the reach of their services, increasing engagement, innovation and adoption. Even governments have embraced the use of APIs in order to speed up the development of web and mobile apps for their citizens.
Why is JSON so important
HTTP GET and POST
The two most important methods for HTTP communications are GET and POST, GET being the default. You perform a GET every time you call a URL. Although you can send data through a GET call, it’s usually data that will be used as contextual information to retrieve more data. You perform a POST every time you send data to a web service, such as filling out a form. The POST method encodes the data in a different way, providing more security and allowing you to send more data in each request.
With the XHR library described earlier, you perform a HTTP GET call with a single line.
xhr.get(url, onSuccessCallback, onErrorCallback, options);
The callbacks are simply functions you send to the library to be used later on, in this case when the process was successful or when there was an error. Note that you need to define these functions before calling the GET or POST method.
Let’s examine the onSuccessCallback.
The POST server website will confirm that it indeed received the data and will give you a URL to view the data received. Remember, this is a public website so only use it to send test data.
Once you know that your data is being properly received by the “post server”, then you know that your app is working properly, so not switch the post call to the actual one and you’re set.
Now that you can communicate with web services, the next step is to do something useful with the data. By combining Appcelerator Alloy with this very basic example, you can have a native interface to read tweets. You can see all the required files here: https://gist.github.com/ricardoalcocer/5190529. Here’s what it looks like.
In a final post of this series, I’ll explain how to create your own web services using Appcelerator Cloud Services. Stay tuned.