Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Getting the OkHttp Library Using Gradle4:54 with Ken Alger
Android Studio includes Gradle, a build system for Android projects. Gradle includes dependency management, which allows us to easily include 3rd party code like OkHttp.
We left off with the general knowledge of the OkHttp API. 0:00 We've been looking at the project page on GitHub. 0:04 But let's turn our attention to the project's website shown here. 0:06 I've included links for this in the Teachers Notes as well. 0:12 The project's site provides additional information about the library and 0:15 has some examples. 0:18 One thing to note that is shown here is the minimum requirements for using OkHttp. 0:20 We need to be using Android 2.3 and Java 1.7. 0:28 There are newer versions of both of these at this point, but 0:32 it is something to check and consider. 0:36 We're using Gradle for our project, and 0:38 if we look here in the download section, We see a Gradle statement we can use. 0:40 It's also listed over in their GitHub repository. 0:45 So let's copy this line, and head back to Android Studio. 0:54 In the project pane we need to expand Gradle scripts, 1:01 and the build.gradle file for our app. 1:09 This one here, we don't want the Stormy project file, we want the app file. 1:15 Android Studio has a main module, 1:21 typically called app, it has application-specific code. 1:23 This is the file in which we can paste our line of code from OkHttp. 1:27 Looking in here, though, where should It go? 1:32 This Gradle file doesn't look at all like Java code, right? 1:34 Well, it isn't. 1:38 Gradle is an automated build system used in Android development, and elsewhere. 1:40 It allows us to configure a few things, and in exchange, it does a lot of work for 1:45 us automatically behind the scenes. 1:50 It's a huge time saver, and once it is setup for 1:52 a specific project, we don't have to do much else. 1:55 Another productivity enhancement, very cool. 1:59 When using an outside resource, such as OkHttp, 2:02 our Stormy app depends on that library, Gradle excels at managing dependencies. 2:06 And hey, look at this, 2:11 there is a section in our build.gradle file called dependencies. 2:13 That's where we'll paste our line of code from OkHttp. 2:17 So we can go right down here under this last implementation, 2:22 and we'll paste in our line from OkHttp. 2:25 Let's quickly talk about this build file. 2:30 This Android section up here at the top provides settings for 2:32 things like the SDK version, and our default configuration. 2:35 The dependencies section, where we just added our code, 2:40 tells Gradle about external dependencies we'd like it to manage. 2:43 We can see that there is a file tree where Gradle watches for Java archive files, or 2:47 JAR files, in the Libs directory. 2:51 Some third-party libraries are still used via JAR files, and 2:53 Gradle can keep track of those. 2:57 There are some additional dependencies in here relating to testing and 2:59 compatibility features. 3:02 The format, though, is the same. 3:04 In our case, the com.squareup.okhttp3 tells Gradle where to find the file, 3:06 that OkHttp is the library to get, and that 3.10.0 is the version to use. 3:14 When a new version comes out, you can come back here to this build.gradle file, 3:22 update the version number and synchronize the new changes. 3:26 Which reminds me, anytime we make changes in this file, we need to sync the changes. 3:30 This allows Gradle to update the project, and 3:35 get any additional files our project may need. 3:38 You'll notice that Android Studio displays a warning up here about this as well. 3:41 We can either click the Sync Now link, Or 3:46 click on this button to perform a project sync. 3:51 After Gradle all goes and rebuilds the project, 3:58 which depending on your system and network can take a few moments, 4:00 we see a message that Gradle build finished, down here at the bottom. 4:03 Once we see that message, we're all set. 4:08 If, however, 4:11 you get an error, try using your Google skills to find out what happened. 4:12 Check the Teachers Notes to see if there are any updates, or 4:16 ask questions in the Treehouse forum. 4:19 With OkHttp installed and all set to go, we're ready to use it. 4:22 We head back to the OkHttp website here, We can look for the examples. 4:26 There's also other links to resources on this page as well, 4:33 such us Javadocs, there Wiki, and StackOverflow. 4:38 The OkHttp Wiki has some decent usage examples, and 4:42 I encourage you to have a look around before we come back together and 4:46 make our first http get request from Stormy. 4:50
You need to sign up for Treehouse in order to download course files.Sign up