google-developer-training / android-basics-kotlin-mars-photos-app

Apache License 2.0
92 stars 193 forks source link

Android Basics: Get data from the Internet #121

Closed harr1424 closed 2 years ago

harr1424 commented 2 years ago

name: Android Basics Unit 4 issue about: Report problems with the Android Basics Unit 4 codelabs title: 'Android Basics Unit 4 issue:' labels: '' assignees: ''


URL of codelab https://developer.android.com/codelabs/basic-android-kotlin-training-getting-data-internet?continue=https%3A%2F%2Fdeveloper.android.com%2Fcourses%2Fpathways%2Fandroid-basics-kotlin-unit-4-pathway-2%23codelab-https%3A%2F%2Fdeveloper.android.com%2Fcodelabs%2Fbasic-android-kotlin-training-getting-data-internet#6

In which task and step of the codelab can this issue be found? Exception Handling

Describe the problem Despite adding the try-catch block exactly as instructed, launching the Mars Photo App results in an immediate app with the following output in Logcat:

2022-02-14 16:59:08.510 5994-5994/com.example.android.marsphotos E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.android.marsphotos, PID: 5994

Steps to reproduce?

  1. Go to... com.example.android.marsphotos.overview/OverviewViewModel.kt and add the following try-catch block:
    private fun getMarsPhotos() {
        try {
            viewModelScope.launch {
                val listResult = MarsApi.retrofitService.getPhotos()
                _status.value = listResult
            }
            } catch (e: Exception) {
                _status.value = "Failure: ${e.message}"
        }
    }

    Next ensure airplane mode is enabled on emulator

  2. Click on... Run app
  3. See error...

Versions Android Studio version: 2021.1.1 Patch 1 API version of the emulator: 30

Additional information Include screenshots if they would be useful in clarifying the problem.

Gui88 commented 2 years ago

The try { is inside the launch