google-developer-training / basic-android-kotlin-compose-training-mars-photos

Solution code for Android Basics in Kotlin course
https://developer.android.com/courses/android-basics-kotlin/course?gclid=CjwKCAjw4c-ZBhAEEiwAZ105RTyT-iaLHzrhMBUXdMhO230ZDwOwxxI2x4RgK8DwBxK8t1h0wmU_QxoCi4YQAvD_BwE
Apache License 2.0
66 stars 58 forks source link

Get Data from the Internet #59

Closed koraysels closed 1 year ago

koraysels commented 1 year ago

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

In which task and step of the codelab can this issue be found? https://developer.android.com/codelabs/basic-android-kotlin-compose-getting-data-internet?continue=https%3A%2F%2Fdeveloper.android.com%2Fcourses%2Fpathways%2Fandroid-basics-compose-unit-5-pathway-1%23codelab-https%3A%2F%2Fdeveloper.android.com%2Fcodelabs%2Fbasic-android-kotlin-compose-getting-data-internet#6

Describe the problem It says the app should run and display the JSON content but it does not.

interface MarsApiService {
    @GET("photos")
    fun getPhotos() : String
}

teh stacktrace of the crash indicates it cannot convert the returned JSON into a String object..

 E  FATAL EXCEPTION: main
                                                                                                    Process: com.example.marsphotos, PID: 8117
                                                                                                    java.lang.IllegalArgumentException: Unable to create call adapter for class java.lang.String
                                                                                                        for method MarsApiService.getPhotos
                                                                                                        at retrofit2.Utils.methodError(Utils.java:54)
                                                                                                        at retrofit2.HttpServiceMethod.createCallAdapter(HttpServiceMethod.java:116)
                                                                                                        at retrofit2.HttpServiceMethod.parseAnnotations(HttpServiceMethod.java:67)
                                                                                                        at retrofit2.ServiceMethod.parseAnnotations(ServiceMethod.java:39)
                                                                                                        at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:202)
                                                                                                        at retrofit2.Retrofit$1.invoke(Retrofit.java:160)
                                                                                                        at java.lang.reflect.Proxy.invoke(Proxy.java:1006)
                                                                                                        at $Proxy2.getPhotos(Unknown Source)
                                                                                                        at com.example.marsphotos.ui.screens.MarsViewModel$getMarsPhotos$1.invokeSuspend(MarsViewModel.kt:46)
                                                                                                        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
                                                                                                        at kotlinx.coroutines.internal.DispatchedContinuationKt.resumeCancellableWith(DispatchedContinuation.kt:367)
                                                                                                        at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt:30)
                                                                                                        at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable$default(Cancellable.kt:25)
                                                                                                        at kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt:110)
                                                                                                        at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:126)
                                                                                                        at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(Builders.common.kt:56)
                                                                                                        at kotlinx.coroutines.BuildersKt.launch(Unknown Source:1)
                                                                                                        at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch$default(Builders.common.kt:47)
                                                                                                        at kotlinx.coroutines.BuildersKt.launch$default(Unknown Source:1)
                                                                                                        at com.example.marsphotos.ui.screens.MarsViewModel.getMarsPhotos(MarsViewModel.kt:44)
                                                                                                        at com.example.marsphotos.ui.screens.MarsViewModel.<init>(MarsViewModel.kt:36)
                                                                                                        at java.lang.Class.newInstance(Native Method)
                                                                                                        at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.kt:202)
                                                                                                        at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:322)
                                                                                                        at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:304)
                                                                                                        at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:278)
                                                                                                        at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.kt:128)
                                                                                                        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:187)
                                                                                                        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:153)
                                                                                                        at androidx.lifecycle.viewmodel.compose.ViewModelKt.get(ViewModel.kt:215)
                                                                                                        at androidx.lifecycle.viewmodel.compose.ViewModelKt.viewModel(ViewModel.kt:156)
                                                                                                        at com.example.marsphotos.ui.ComposableSingletons$MarsPhotosAppKt$lambda-3$1.invoke(MarsPhotosApp.kt:64)
                                                                                                        at com.example.marsphotos.ui.ComposableSingletons$MarsPhotosAppKt$lambda-3$1.invoke(MarsPhotosApp.kt:45)
                                                                                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
                                                                                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
                                                                                                        at androidx.compose.material.SurfaceKt$Surface$1.invoke(Surface.kt:134)
                                                                                                        at androidx.compose.material.SurfaceKt$Surface$1.invoke(Surface.kt:117)
                                                                                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
                                                                                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
                                                                                                        at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228)
                                                                                                        at androidx.compose.material.SurfaceKt.Surface-F-jzlyU(Surface.kt:114)
                                                                                                        at com.example.marsphotos.ui.ComposableSingletons$MarsPhotosAppKt$lambda-4$1.invoke(MarsPhotosApp.kt:40)
                                                                                                        at com.example.marsphotos.ui.ComposableSingletons$MarsPhotosAppKt$lambda-4$1.invoke(MarsPhotosApp.kt:39)
                                                                                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:116)
                                                                                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
                                                                                                        at androidx.compose.material.ScaffoldKt$ScaffoldLayout$1$1$1$bodyContentPlaceables$1.invoke(Scaffold.kt:322)

Versions _Android Studio version:_2022.2.1 Patch 1 API version of the emulator: 33

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

koraysels commented 1 year ago

damn! i found the solution..

interface MarsApiService {
    @GET("photos")
    suspend fun getPhotos() : String
}

forgot to make the function suspendable

sey commented 9 months ago

I would suggest re-opening this issue (to get it fixed) as I was going to report the issue. The codelab does not specify the suspend keyword and the error message is misleading.