Date

Lecture Date: Monday, October 9

We will start by finishing up the frameworks from last lecture before moving on.

Now that we know about service-oriented architectures and web services, how do we actually use them? And how do we know what we want to use?

Looking for some APIs to use? Google's your friend! You can also check some registries like PublicAPIs or ProgrammableWeb.

Android

Here's a general tutorial from ssaurel.com - http://www.ssaurel.com/blog/learn-to-consume-a-rest-web-service-and-parse-json-result-in-android/

The basic steps go like this:

  1. Find/create the web service that you want to use. This could be many different things for your app. You might need an external database to keep up with what your users are doing. You may need a place to host images or audio. Find something that will add value / important features to your app and go with it!
  2. Make the HTTP call. Android has a built-in HTTP library that allows you to build an HTTP request and then parse the result.
  3. Parse the result. Once you get the result, you'll need to pull it into a data structure you can use. If the service is using XML or JSON, Android has built-in libraries for handling both!
  4. Implement the features. So, the catch to all this is that starting with a previous version of Android, it was made a requirement that all networking activity has to take place off of the main thread. This prevents the app from hanging while the user is using it. Thus, you'll need to create an AsynchronousTask and use that to spawn a thread that will do the call in the background for you.

Once you've done all that, you can then do what you want with the data - load it into a list for viewing, process it some other way, etc.

Sending data to a web service is very similar. The main difference is when you make the HTTP call, you'll have to make either a GET or POST command and then pass the appropriate variables you need to with the call.

Another option is to use a third-party library that will provide a wrapper around the REST service. If you go this route, you don't have to deal as much with the network functionality or parsing the JSON. Most wrappers will just hand back to you a Java object you can just use. Retrofit - http://square.github.io/retrofit/ is one good option.

Here are some Retrofit tutorials I used to make my Lou's List example:

iOS

The basic steps are effectively the same for iOS as Android, including running the call on a separate thread. The main difference is a change that occurred with iOS 9. In iOS 9, Apple added some network security features that require very particular HTTPS traffic. This makes it difficult to connect to... well... most anything. They apparently even had a session at their developer conference on how to get around it.

You'll need to make some changes to the Info.plist file to make it work. Information can be found in the links below.

Some tutorials you can look at:

Here's some code to pull down and parse the JSON:

override func viewDidLoad() {
    super.viewDidLoad()

    let config = URLSessionConfiguration.default // Session Configuration
    let session = URLSession(configuration: config) // Load configuration into Session
    let url = URL(string: "http://stardock.cs.virginia.edu/louslist/Courses/view/CS?json")!

    let task = session.dataTask(with: url, completionHandler: {
        (data, response, error) in

        if error != nil {
            print(error!.localizedDescription)
        } else {
            do {
                let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments)
                //Implement your logic

                print(json)

            } catch {
                print("error in JSONSerialization")
            }
        }
})
    task.resume()
}

Want to be like the Android folks and use some cool frameworks and APIs?

First, learn what CocoaPods is - https://cocoapods.org/. CocoaPods is a package and dependency manager for Xcode (Mac, iOS, Swift, Obj-C). Built in Ruby, it allows you to quickly add components to an Xcode project. We used it as a part of the Core Skills app for Firebase, in fact.

Here are two to look at for doing REST: