Data Model Classes4:34 with Jamie Huson
Use the pre-made data model classes to display items.
View the 'Downloads' tab to access the Data Model Files
Want to know more about
Etsy API Endpoint
I've provided a few data model classes to help you get started. 0:00 These Java classes represent the data that Etsy API will return to us. 0:04 Download them in the link in the teacher's notes. 0:09 Once you have them downloaded, go ahead and unzip them, and 0:13 we can see what classes are included. 0:17 We're gonna wanna go ahead and take these classes, copy them, 0:19 and paste them into our project in Android Studio. 0:22 In Android Studio, I'm gonna create a new package to keep my code organized. 0:26 I'm gonna call it model. 0:29 And in model, I'm gonna go ahead and paste in those classes. 0:32 If we open one of these classes, for 0:37 instance image, we can see it's a simple class with properties. 0:39 The classes are all parcelable, 0:43 which allows us to easily use them with fragments and activities. 0:45 You're hopefully familiar with parcelable data, but if not, check out the links 0:49 in the teacher's notes to where we've already covered this at TreeHouse. 0:53 To understand why they're modeled this way, let's look at the documentation for 0:57 the API we're using. 1:00 Here on the Etsy Developers site, let's click on Documentation. 1:02 In Documentation, we can find all the information we need to connect and 1:06 access Etsy's data. 1:09 Let's look at the API Reference. 1:11 Here we can see all the different types of data we can get back from the Etsy API. 1:14 We're gonna be looking for listings, so let's go ahead and click on Listing. 1:18 If we scroll down a little bit, we'll be able to see an overview of what a listing 1:22 is, and it also lists a few different properties and fields that a listing has. 1:26 If we keep scrolling, we're gonna find information about how to search for 1:32 listings. 1:36 By default, 1:38 the method findAllListingActive returns all the active listings on Etsy. 1:38 This is exactly what we want, so let's go ahead and click on it. 1:43 And we can see here that this provides an overview of how to use this method. 1:47 It provides the method name, the HTTP protocol we need, 1:52 the URI and a whole list of parameters. 1:56 We can see that the method is found at /listing/active and it uses HTTP GET. 2:00 Let's break down the API call, so we can understand each part. 2:06 The Etsy endpoint is [SOUND] 2:09 https://openapi.etsy.com/v2. 2:13 [SOUND] Next, is the method name, which is /listings/active. 2:20 [SOUND] Next, we include the URL parameters that indicate we want the shop 2:25 and the image data returned along with the listing data. 2:29 This will make it so 2:32 we don't have to make additional API calls once we get the listing data. 2:33 Finally, [SOUND] at the very end, we're going to append our API key. 2:37 If we make this API call in our browser, we can see what data gets returned. 2:41 Let's go ahead and do that now. 2:46 So open a new tab and we'll just type in our call that we're gonna make, 2:47 [SOUND] openapi.etsy.com/v2/listings/active, 2:56 and we need to include, using the includes, 3:03 the images and shop and append our API key. 3:08 And we need to make sure we're using our current API key for this, so 3:13 let's go ahead and go to Android Studio and paste in the API key from our app. 3:18 Switching back to Chrome, I'm just gonna go ahead and 3:26 paste in our API key at the end. 3:28 And let's go ahead and make that call. 3:31 And we can see what gets returned to us. 3:34 It's a JSONObject and it has many fields included in it. 3:36 First, it gives us a count of how many objects there are that 3:40 can be possibly returned. 3:43 And then it starts listing them out in a JSON array. 3:44 If you look here, we can see that a JSON array has a number of listings included, 3:47 and within that, 3:52 are also the images that we asked to be included using the include statement. 3:54 And if we keep scrolling down, we can also see the shop data is included as well, 3:59 which we requested in the include statement. 4:04 If we switch back to Android Studio, let's take a look at the ActiveListings class, 4:07 we can see that this directly mirrors the result from the API. 4:12 So we have the count and an array of listing objects and if we go ahead and 4:16 go into the Listing objects class, we can see that this has 4:20 the exact same properties that were being passed back to us from the API. 4:24 Let's take a break and when we return, 4:29 we can start using these classes to make an API request. 4:31
You need to sign up for Treehouse in order to download course files.Sign up