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
72 stars 66 forks source link

Get Data from the Internet #26

Open ParkSangYeol opened 1 year ago

ParkSangYeol 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&hl=ko#7

Specify the language of the codelab if it is not English:

Documents in all languages except English

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

Describe the problem

.addConverterFactory(Json.asConverterFactory("application/json".toMediaType())) I think it should be changed as below. .addConverterFactory(Json.asConverterFactory(MediaType.get("application/json")))

Steps to reproduce?

  1. Go to Unit 5 pathway1 course 5, 8th document

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

image

ScottyNZ commented 1 year ago

okhttp-3.14.9.jar vs okhttp-4.9.3.jar

Hi, Had the same issue when starting from repo-starter Branch. This issue appear to be related to okhttp3.MediaType.Companion.toMediaType when I built from the repo-starter branch and added import okhttp3.MediaType.Companion.toMediaType it failed to locate the companion object as the jar associated with it was Gradle: com.squareup.okhttp3:okhttp:3.14.9 (okhttp-3.14.9.jar) which does not appear to contain the necessary toMediaType function.

However I found on a separate successful build that import okhttp3.MediaType.Companion.toMediaType was associated with Gradle: com.squareup.okhttp3:okhttp:4.9.3 (okhttp-4.9.3.jar)

Adding Coil Library to the App build file allowed syncing to okhttp:4.9.3 (okhttp-4.9.3.jar)

I.e. add implementation "io.coil-kt:coil-compose:2.1.0" to App build file.

I don't understand why this makes difference but it appears to be the only difference between the two build files in the two projects. Hopefully someone with knowledge of the build system can shed some light on the issue

lixoten commented 1 year ago

Yes, we need implementation "io.coil-kt:coil-compose:2.2.2" BUT...

In previous codelab "Get data from the internet" we had this line of code: addConverterFactory(Json.asConverterFactory(MediaType.get("application/json"))) it imported: import okhttp3.MediaType

In the next codelab: we have this like of code:... see the difference? .addConverterFactory(Json.asConverterFactory("application/json".toMediaType())) it needs this import: import okhttp3.MediaType.Companion.toMediaType BUTTTTTT we have this from previous codelab import okhttp3.MediaType

THE SOLUTION IS TO PICK ONE and stay with it IF u have this import import okhttp3.MediaType then you need to use:
addConverterFactory(Json.asConverterFactory(MediaType.get("application/json"))) ELSE IF u have this import import okhttp3.MediaType.Companion.toMediaType then you need to use:
addConverterFactory(Json.asConverterFactory(MediaType.get("application/json")))