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
  • Release Code
  • Requirements:

Was this helpful?

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

A6: You Should Even Lift, Bro.

Moving onto physiological wellness!

PreviousDemo: RetrofitNextBonus Week | Android Job Search

Last updated 2 months ago

Was this helpful?

This assignment has a release code! Please download it below, extract it, and open it in Android Studio. Ensure it can compile and run fine on your emulator before starting this assignment.

For our final assignment, we'll have you develop the networking logic for a basic fitness app!

Rounding out our journey away from money and towards happiness, of course we have to take a stop by physical wellness.

Let's walk through the code, shall we?


Release Code

Expand each of the files below for a description of the release code!

main package

Contains the home screen / main screen logic. Pertains just to part 0).

Overall, this screen gives the option for the user to input an exercise name as well as choose some tags about their exercise.

results package

Contains the search results screen logic. Pertains to parts 4) and 5).

This screen shows the results of a successful networking--a scrollable horizontal pager of exercises that match the search results.

retrofit package

Contains empty Retrofit files you'll have to implement in 2) and 3)!

ExerciseCard.kt

A card that displays information about one exercise. This corresponds to all the information that our API will return:

Knob.kt

A tappable knob that is used for tags. This will be used in the main screen to allow the user to select certain tags about their exercise search:


Requirements:

This assignment is not split into features, but rather more so as steps towards implementing Retrofit correctly.

0) Main Warmup

Time for a bit of a warmup to make sure you understand the codebase (and MVVM)!

You'll notice that none of the buttons nor the text field work yet on the main screen. Let's fix that using our UiState class and the uiStateFlow!

  1. Connect the query and onQueryChanged(...) from MainViewModel to the MainScreen.

  2. Do the same such that Training Type and Muscle Group tags change correctly when they're tapped on!

(Make it so that the Knob with the current trainingType/muscleGroup is set to active = true, false otherwise.)

1) API Ninjas Setup

For our backend for this assignment, we would like you to make an account on API Ninjas and access their Exercises API. It's free!

  1. On the My Account page, you should see an API Key that you can show/hide. Keep hold of that for later.

In essence, this endpoint will return a list of (at most 5) exercises matching some parameters we put in. Our app will allow users to (optionally) put in a name, a training type, and a muscle group.

2) Retrofit Dependencies
  1. Add the following dependencies to your build.gradle :app file.

// okhttp3
implementation("com.squareup.okhttp3:okhttp:4.10.0")
// retrofit
implementation("com.squareup.retrofit2:retrofit:2.9.0")
// gson
implementation("com.squareup.retrofit2:converter-gson:2.9.0")
  1. Add the following permission to your AndroidManifest.xml (under just the manifest block, nothing else):

<uses-permission android:name="android.permission.INTERNET" />
  1. Gradle Sync your project. There should be no build errors (hopefully...)

3) ExerciseApiService.kt

Time to use Retrofit to implement a networking call for us!

  1. Make a @GET suspend function called getExercises(...) that uses the "exercises" route.

  2. Give this function 3 optional string arguments: name, type, and muscle. These should be annotated as @Query parameters under the same name.

  3. Give this function 1 final arugment:

@Header("X-Api-Key") apiKey: String = "YOUR_API_KEY_HERE",

replace YOUR_API_KEY_HERE with the key API Ninjas gave to you upon signup!

  1. Referencing the JSON that API Ninjas gives to you on their exercises api page, make a Exercise data class that accepts 6 string fields (corresponding to the JSON fields).

  2. Change the return type of your function to a List<Exercise>.

4) RetrofitInstance.kt

Time to set our URL and other parameters with our RetrofitInstance class!

  1. Make this correctly into a hilt singleton that can then be injected into other hilt references. This also involves adding the @Inject constructor notation.

  2. Inject the retrofitInstance into ResultsViewModel.

Please reference our demo code to how see how this is done! You should make a exerciseApiService field, or something similar.

5) ResultsViewModel & Networking

Alright, time to tie it all together! In this part, you'll implement and test final the networking logic to get your app actually showing the proper response from the backend.

  1. Implement loadData() by using your retrofitInstance in the launched coroutine:

    1. Make a call to your getExercises(...) . Make sure you pass in the query, trainingType, and muscleGroup parameters we extracted for you from the navigation arguments!

    2. When the call terminates, set the UiState loading to false.

    3. Test the app! You should no longer see the app infinitely load.

  2. Add another field to your UiState class that sends down the (loaded) list of exercises to the UI.

    1. The call to getExercises(...) should have returned the list of exercises; make sure to actually send this down your UiState flow.

  3. Edit ResultsScreen and ExerciseCard to actually send down each exercise's information to the card.

    1. Also, make sure that the pager is showing the right number of cards; it is hardcoded to 3 right now.

    2. You can choose to just delete the @Preview for exercise card if you want, or edit it to allow the preview to continue showing!

    3. (Don't worry too much about the particulars about how the text is rendered. You won't be graded about the nitpicky stuff.)

If you get any networking errors, make sure to debug with LogCat errors!

If you've solved A6 correctly, your solution should look something like this...

When you're done, export your project through Android Studio and upload your ZIP to CMS! Congrats on finishing A6--the final assignment of Intro to Android!

Go to and create an account.

Visit to see the API that we'll be working with! Read through the description, parameters, and response.

We'll be hitting the endpoint. Note that this is NOT the link you manually want to visit to read the api; this is a unique link used to actually make networking calls to.

Implement RetrofitInstance with the correct API base URL. Please note that "exercises" is the extension of the URL; the base URL is just

https://api-ninjas.com/
https://api-ninjas.com/api/exercises
https://api.api-ninjas.com/v1/exercises
https://api.api-ninjas.com/v1/
118KB
a6-release.zip
archive
A6 Release Code!
9MB
a6-soln-demo.mp4
A6 Solution Demo Video!
Exporting with File -> Export -> Export to Zip File...