Intro to Android Development
  • Welcome
  • Syllabus
  • Hack Challenge
  • Resources
    • Lecture Videos
    • Ed Discussion
    • Git & GitHub Help/How-To
    • Setting up Android Studio
    • Starting an Android Studio Project & Making an Emulator
    • Importing, Exporting, & Submitting Your Projects to CMS
  • SP25 Course Material
    • Week 1 | Course Logistics, Kotlin, & Basic UI
      • Relevant Links
      • Demo/Lecture: Eatery Card
      • A0: Eatery Card (Follow-Along)
    • Week 2 | States, Components, LazyColumn
      • Relevant Links
      • Demo: Todo List
      • A2: Shopping List
    • Week 3 | Navigation & Animations
      • Relevant Links
      • Demo: Onboarding
      • A3: Stock Trading (RobbingGood)
    • Week 4 | MVVM and Flows
      • Relevant Links
      • Demo: Eatery Card 2
      • A4: Chat of a Lifetime
    • Week 5 | Dumb Components & UIEvents
      • Relevant Links
      • Demo: Music Player
      • A5: Rate My Vibe
    • Week 6 | Coroutines, Networking, JSON
      • Relevant Links
      • Demo: Retrofit
      • A6: You Should Even Lift, Bro.
  • Bonus Week | Android Job Search
    • Relevant Links
    • Android Technical Interview Question!
  • Textbook
    • 1. Introduction to the Editor and Views
      • 1.1 Introduction to the Editor
      • 1.2 SDK Management
      • 1.3 Kotlin Overview
      • 1.4 Views
      • 1.5 Android Studio Project Demo + Understanding The Editor
    • 2. Jetpack Compose
      • 2.1 Introduction
      • 2.2 Layouts
      • 2.3 Modifiers
      • 2.4 Animations
      • 2.5 Lazy Lists
      • 2.6 Reactive UI
    • 3. Intents and Manifest
      • 3.1 Activities
      • 3.2 Implicit Intents
      • 3.3 Explicit Intents
      • 3.4 Manifest
      • 3.5 Permissions
      • 3.6 Summary
    • 4. Navigation
      • 4.1 Types of Navigation
      • 4.2 Implementation of the Bottom Navigation Bar
    • 5. Data and Persistent Storage
      • 5.1 Singleton Classes
      • 5.2 Shared Preferences
      • 5.3 Rooms
      • 5.4 Entities
      • 5.5 Data Access Objects
      • 5.6 Databases
    • 5.5 Concurrency
      • 5.5.1 Coroutines
      • 5.5.2 Implementation of Coroutines
      • 5.5.3 Coroutines with Networking Calls
    • 6. Networking and 3rd Party libraries
      • 6.1 HTTP Overview
      • 6.2 3rd Party Libraries
      • 6.3 JSON and Moshi
      • 6.4 Retrofit
      • 6.5 Summary
    • 7. MVVM Design Pattern
      • 7.1 Key Idea
      • 7.2 Implementation Ideas
    • 8. Flows
    • 9. The Art and Ontology of Software
    • 10. 🔥 Firebase
      • 10.1 Setting up Firebase
      • 10.2 Authentication
      • 10.3 Analytics
      • 10.4 Messaging
      • 10.5 Firestore
  • Additional Topics
    • Git and GitHub
    • Exporting to APK
  • Archive
    • Archived Native Android Textbook Pages
      • 1. Layouts and More Views
        • 1.1 File Structure and File Types
        • 1.2 Resource Files
        • 1.3 Button and Input Control
        • 1.4 ViewGroups
        • 1.5 Summary + A Note On Chapter 2 Topics
      • 2. RecyclerViews
        • 2.1 RecyclerViews
        • 2.2 RecyclerView Performance
        • 2.3 Implementation of a Recycler View
        • 2.4 Implementation with Input Controls
        • 2.5 Filtering RecyclerViews
        • 2.6 Recyclerview Demo
      • 3. ListViews and Searching
        • 3.1 ListView vs. RecyclerView
        • 3.2 ListView Performance
        • 3.3 Implementation of a ListView
        • 3.4 Searching in a List View
      • 4. Fragments
        • 4.1 What are Fragments?
        • 4.2 Lifecycle of a Fragment
        • 4.3 Integrating a Fragment into an Activity
        • 4.4 Sharing Data Between Fragments
        • 4.5 Fragment Slide Shows
      • 5. OkHttp
      • 6. Activity Lifecycle
      • 7. Implementation of Tab Layout
    • Fall 2024 Course Material
      • Lecture 1 & Exercise 1: Introduction to Android
      • Lecture 1.5: Beauty of Kotlin
      • Lecture 2 & HW 2: Modifiers, Lazylists and Reactive UI
      • Lecture 3 & HW 3: Animations, Intents and Manifest
      • Lecture 4 & HW 4: Coroutines & Navigation
      • Lecture 5 & HW 5: Persistent Storage, Networking, and JSON Parsing
      • Lecture 6 & HW 6: MVVM, Flows
      • Bonus Lectures & Bonus HW
      • Bonus Lecture: Industry Practice
    • Spring 2024 Course Material
      • Lecture 1 & Exercise 1: Introduction to Android
      • Lecture 4 & HW 4: LazyLists
      • Lecture 6 & HW 6: Networking, Data, and Persistent Storage
    • Spring 2020 Course Material
      • Week 1: Intro to the Editor
      • Week 2: Views and Layouts
      • Week 3: Intent and Manifest
      • Week 4: ListView and RecyclerView
      • Week 5: Fragments
      • Week 6: Networking
    • Spring 2021 Lecture & HW 8: Networking & 3rd Party APIs
    • HackOurCampus Workshop
Powered by GitBook
On this page
  • Part 1: Define your API interface
  • Part 2: Complete the ViewModels
  • Part 3: Build your screens
  • Congrats, you've finished demo 6!

Was this helpful?

  1. SP25 Course Material
  2. Week 6 | Coroutines, Networking, JSON

Demo: Retrofit

PreviousRelevant LinksNextA6: You Should Even Lift, Bro.

Last updated 21 days ago

Was this helpful?

For this demo, you'll get the chance to practice getting data from an API. For the walkthrough, we'll be using NinjaAPI's sentiment API, but you can feel free to choose from most of to work with since the setup process is pretty similar regardless of the API's parameters.

Download the starter code:

Part 1: Define your API interface

You'll be setting up the route to your API in the APIService file (in the Retrofit folder). Follow the TODOs below to complete this!

  1. Each API Ninjas API is identified by some name. On the API page, this will be the /V1/[API-Identifier]. Replace "endpoint" in the @GET annotation with the [API-Identifier] of the API you're using.

  2. Each API documentation should provide three types of information: the Parameters (if any), Headers, and Response. For this TODO, you'll need to define the Header (your personal API key), and all of the parameters. Here's what the syntax for these should look like:

    1. The @Query annotation tells Retrofit that this information should be added to the URL that's being used to access the server (esentially passing the information as a parameter)

suspend fun getData(
    //using the Sentiment API as an example
    @Query("score") score: Float? = null,
    //If you have multiple parameters to query write an @Query line for each of them.  
    
    @Header ("X-Api-Key") apiKey: String = "[your api-key]"
)
  1. Define a data class to represent the data you're querying. This data class should be a reflection of the data that the API is returning. You can find this information in the Response section of the API documentation.

  2. Specify the return type of getData(). (It's the data class you just defined)

    1. This is necessary because otherwise Retrofit doesn't know how to deserialize the JSON it receives from the API

Part 2: Complete the ViewModels

Our Model in this project is the RetrofitInstance, which is where the route to the API, apiService, is instantiated.

  1. Since we need to pass a Model to each of our View Models, we'll pass the following into the constructor of MainViewModel and ResultViewModel.

 private val retrofitInstance: RetrofitInstance

For the ResultViewModel, specifically

  1. Complete the UiState. This should contain all of the data that you need to display in the ResultScreen. It'll probably either be the type that getData() returns or a list of that type.

  2. Get and store your data from the retrofitInstance in a val. You'll need to get through multiple layers of abstraction to get to the actual function where you can call your data.

    1. Hint: Type out retrofitInstance, then a dot . to see what functions are available to you.

    2. If your API requires a parameter, you can access this from the query value that's defined in the file.

  3. Update the UiState with your newly stored data. This is what will allow the view to access and reflect the changes in the data.

    1. You'll also want to change loading to false, since the data is available now.

_uiStateFlow.value = _uiStateFlow.value.copy(loading = false, ...)
  1. If you're taking in text input from the user, you may also need to handle the case where no input is provided.

Part 3: Build your screens

We've outlined MainScreen and ResultScreen for you with the appropriate view models passed in and uiStates defined.

  • MainScreen will be the first screen that the user sees. The button defined here will make the call to your API and navigate to the ResultScreen where the data from the API will be displayed.

  • Depending on the specifics of your API, you might also need to take some kind of input here that you'll use as a parameter in your API call. We've defined a Textfield here for that purpose.

Follow the TODOs in MainScreen.kt and ResultScreen.kt. For ResultScreen, some Text components should be enough to display the data!

  • If your data is more complex, you may need to make a separate component for it that takes some data via its parameters.

Congrats, you've finished demo 6!

Now you should be able to successfully build and test your app!

API Ninjas' APIs
115KB
demo6_starter.zip
archive