Android Kotlin: Forecast App 02 - Retrofit + Coroutines + Gson Fetch API Data - MVVM Tutorial Course

แชร์
ฝัง
  • เผยแพร่เมื่อ 11 พ.ย. 2024

ความคิดเห็น • 131

  • @matt-g-recovers
    @matt-g-recovers 5 ปีที่แล้ว +3

    Ill say it again.
    THIS is the most powerful tutorial series of 2019.
    These cover it all, everything I use in a professional environment and most of the concepts needed for the foundation of a good application.
    Thank you!
    I Also have a TON of love amd respect for Florian and Mitch. They know who they are and I know Florian is working on a phenomenal series on Kotlin !

  • @nehankpoilkar9641
    @nehankpoilkar9641 4 ปีที่แล้ว +7

    The url for fetching weather has changed
    As cheryl posted :
    For new API:
    @GET("current")
    fun getCurrentWeather(
    @Query("query") location: String,
    @Query("lang") languageCode: String = "en"
    ):Deferred
    companion object{
    operator fun invoke():ApixuWeatherApiService{
    val requestInterceptor = Interceptor { chain->
    val url = chain.request()
    .url()
    .newBuilder()
    .addQueryParameter("access_key", API_KEY)
    .build()
    val request = chain.request()
    .newBuilder()
    .url(url)
    .build()
    return@Interceptor chain.proceed(request)
    }
    val okHttpClient = OkHttpClient.Builder()
    .addInterceptor(requestInterceptor)
    .build()
    return Retrofit.Builder()
    .client(okHttpClient)
    .baseUrl("api.weatherstack.com/")
    .addCallAdapterFactory(CoroutineCallAdapterFactory())
    .addConverterFactory(GsonConverterFactory.create())
    .build()
    .create(ApixuWeatherApiService::class.java)
    }
    Plus Android 8 does not allows url with only "http"
    Solution :
    Step 1- add this entry in manifest
    Step 2 - and:
    create res/xml/network_security_config.xml





    It worked for me..!! Cheers

  • @agung_laksana
    @agung_laksana 5 ปีที่แล้ว +26

    Wow that json to kotlin class plugin is really really helpful. Thank you very much.

    • @jeghamaymen
      @jeghamaymen 5 ปีที่แล้ว +2

      Just be careful of the types it automatically assigns, for example it may get confused between Int and Double very easily.

  • @waichee723
    @waichee723 5 ปีที่แล้ว

    I am still going through this tutorial. This is definitely the best android tutorial on TH-cam. You are a hero.

  • @zapphiere
    @zapphiere 5 ปีที่แล้ว +5

    This video saved my semester!, I couldn´t understand how to use Retrofit till I found this one.
    Thank you so much for the great quality content. I will surely do the whole tutorial in the future :)

    • @timelord404
      @timelord404 5 ปีที่แล้ว

      seriously, Are you guys having retrofit in syllabus?

    • @sumitshukla6436
      @sumitshukla6436 4 ปีที่แล้ว

      @@timelord404 Hahahaha

  • @marshallborsos3396
    @marshallborsos3396 4 ปีที่แล้ว +6

    If people are still having issues getting a null error from the response, do the following:
    1. In ApixuWeatherApiService.kt, remove @Query("language") languageCode: String = "en". The current version of WeatherStack does not support foreign languages on the free plan, and English uses no language code (unset)
    2. Follow the instructions given by nehank poilkar to ensure you do not have issues related to the new parameter nomenclature and due to the fact that Android Studio by default does not allow access to the internet or non-HTTPS resources

    • @amitkumdixit
      @amitkumdixit 4 ปีที่แล้ว

      I am getting null will try it solution. Thanks

    • @amitkumdixit
      @amitkumdixit 4 ปีที่แล้ว

      thanks Step 1 did the trick

  • @developertriplogic8890
    @developertriplogic8890 5 ปีที่แล้ว

    I just found how to fix this, and you don't need to reply my question, you tutorials are helpful for us

  • @thelumiereguy
    @thelumiereguy 5 ปีที่แล้ว

    Thank you very much! I usually do this in Java and am new to kotlin. Thank you for teaching me how to use retrofit with coroutines.
    You've gained a new sub !

  • @roothresh6499
    @roothresh6499 3 ปีที่แล้ว

    Really thanks. You saved my life and time…

  • @kaletsugas
    @kaletsugas 5 ปีที่แล้ว +5

    Hi Reso Coder, Thank you for this tutorial. The retrofit2-kotlin-coroutines-adapter is Deprecated. will this tutorial be updated to make use of kotlin coroutines, suspend?

  • @felipefranco7444
    @felipefranco7444 4 ปีที่แล้ว

    You saved me best tutor ever!

  • @arthurgomes9248
    @arthurgomes9248 5 ปีที่แล้ว

    You are a really skilled teacher.

  • @alextroy605
    @alextroy605 4 ปีที่แล้ว

    Wow! That really interesting and simple guide about Retro + Weather + Kotlin + VM.

  • @fahrican9708
    @fahrican9708 4 ปีที่แล้ว

    Thanks for explanation in the ApiService!

  • @Monarch_943
    @Monarch_943 6 ปีที่แล้ว

    Very good tutorial! It turns out I was doing alot of things the wrong way. Looking forward to future tutorials!

  • @Kekszmester
    @Kekszmester 5 ปีที่แล้ว

    This tutorial is the best gift for Christmas! Thanks!

    • @ResoCoder
      @ResoCoder  5 ปีที่แล้ว

      I'm really happy I could help you!

  • @rinkuk7924
    @rinkuk7924 3 ปีที่แล้ว

    very helpful! thank you

  • @ahmetcoskunkzlkaya4884
    @ahmetcoskunkzlkaya4884 3 ปีที่แล้ว

    Bro you're the best

  • @voicheci007
    @voicheci007 6 ปีที่แล้ว +12

    Brilliant! Is there a Patreon so we can donate to keep the tutorials coming?

    • @ResoCoder
      @ResoCoder  6 ปีที่แล้ว +4

      Not yet 😉 Thanks though!

    • @voicheci007
      @voicheci007 6 ปีที่แล้ว

      @@ResoCoder Hope you reach 100k subs soon!

    • @dk-sky3820
      @dk-sky3820 5 ปีที่แล้ว +5

      Reso Coder
      You should really provide a way for thankful watchers to express their gratitude, mate 😉
      Your videos has literally saved my life (my job at least)

  • @alexeyshaykov
    @alexeyshaykov 4 ปีที่แล้ว +1

    Greate lesson - Thx man!!

    • @orlov_alex
      @orlov_alex 4 ปีที่แล้ว

      Привет, не было случайно проблем с генерацией ссылки в
      companion object {
      operator fun invoke(): WeatherStackApiService {
      val requestInterceptor = Interceptor { chain ->
      val url ???
      У меня почему то ключ в концу ссылки приклеивает и соответственно запрос не обрабатывается =(

  • @abdulalimrajoub6063
    @abdulalimrajoub6063 6 ปีที่แล้ว

    Thanks man, Waiting for the next tutorial in this series 🔥

  • @orlov_alex
    @orlov_alex 4 ปีที่แล้ว +3

    Getting an ERROR :
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String ***.CurrentWeatherEntry.toString()' on a null object reference
    My URL made by companion object in ApiService looks like this for some reason :
    api.weatherstack.com/current?query=London&language=en&access_key=56356555d5db9e0435606fa389bb504
    Can not figure out why the key is at the end of the url and how to put it where it belongs
    Any ideas?

    • @taimurqazi5241
      @taimurqazi5241 4 ปีที่แล้ว +1

      im facing the same problem

    • @luizpaulo6169
      @luizpaulo6169 4 ปีที่แล้ว

      put in yout

    • @luizpaulo6169
      @luizpaulo6169 4 ปีที่แล้ว

      Solved!
      put in your Mnifest

    • @HoussemNasrii
      @HoussemNasrii 4 ปีที่แล้ว

      @@luizpaulo6169 I had the same problem , the reason was that the api changed a bit so key become access_key and q become query (key -> access_key), (q -> query)

  • @AshishGautam-sy3vm
    @AshishGautam-sy3vm 5 ปีที่แล้ว

    Awesome Tutorial Reso Coder.. keep it up

  • @amitranjan6998
    @amitranjan6998 5 ปีที่แล้ว

    Hi can you tell me why return@Interceptor ? chain-> is this return return@Interceptor?

  • @almtg871
    @almtg871 4 ปีที่แล้ว +1

    thank you for this tutorial but i have a problem. When i run my program It crashes in line with "return@Interceptor chain.proceed(request)". Can anybody help?)

  • @okankaya923
    @okankaya923 5 ปีที่แล้ว

    amazing tutorial, thanks so much for spending your time on these tutorials.

  • @mostasimbillah
    @mostasimbillah 6 ปีที่แล้ว

    Really great tutorial .. waiting for next tutorial

  • @thomasj.5382
    @thomasj.5382 6 ปีที่แล้ว

    for what annotation used for in android??is this doing anything in the code flow or just a metadata

  • @khaxbiker
    @khaxbiker 4 ปีที่แล้ว

    Hi. Https is not supported in freemode for APIX. Another thing is that the webpage is now called weatherstack. -If you follow this course be careful to update info

  • @kmcat
    @kmcat 4 ปีที่แล้ว

    Děkuji.

  • @LeaCuvelo
    @LeaCuvelo 5 ปีที่แล้ว

    Thank you very much for your fabulous videos! I wanted to ask you which theme is the one that you have in Android Studio??
    Thanks
    Regards from Argentina!

  • @tinoutech5016
    @tinoutech5016 4 ปีที่แล้ว

    Hello reso, can you tell me how to protect our Api keys and our App in general from reverse engineering ? (Other ways than Proguard )

  • @nicoqueijo
    @nicoqueijo 4 ปีที่แล้ว

    How come you are not using Moshi instead of Gson? I've heard it's better for Android/Kotlin

  • @luizpaulo6169
    @luizpaulo6169 4 ปีที่แล้ว +1

    Guys, for who is getting null response, edit your CurrentWeatherResponse.kt, like that:
    import com.google.gson.annotations.SerializedName
    data class CurrentWeatherResponse(
    @SerializedName("current")
    val currentWeatherEntry: CurrentWeatherEntry,
    val location: Location,
    var request: Request
    )
    Previously, the @SerializedName("current") was in the wrong line!

  • @EddyCaffrey
    @EddyCaffrey 6 ปีที่แล้ว

    Great tutorial as always. Thanks

  • @shlomogevim4801
    @shlomogevim4801 5 ปีที่แล้ว

    Very good one. Thanks.

  • @justynakozik8602
    @justynakozik8602 5 ปีที่แล้ว

    Love your tutorials! I am developing my own forecat app but with custom api and less functionalities. But I have some issue, maybe you will be able to help me.
    How can I get the api error request caught? (basically havig the same service a you)

  • @developertriplogic8890
    @developertriplogic8890 5 ปีที่แล้ว

    I am following your tutorial, but in different case I trying to fetch JSON from API and its type is Array and I got error like this
    "com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $" can you help me?

  • @TheHiHiker
    @TheHiHiker 5 ปีที่แล้ว

    I had to change precipMm to a double for app to work in CurrentWeatherEntry.

  • @TheRajmoney
    @TheRajmoney 4 ปีที่แล้ว

    where is request success / fail part?

  • @Chekist2008
    @Chekist2008 3 ปีที่แล้ว

    Keep in mind that type of field named "precip" in your "CurrentWeatherEntry.kt" should be FLOAT (not integer)!
    I also didn't get that and got exception while get precip=0.1 in my response... i also send request to weatherstack to add data-type description of each field in response (to documentation).

    • @Chekist2008
      @Chekist2008 3 ปีที่แล้ว

      pressure also "Float"

  • @baranidharanmurali469
    @baranidharanmurali469 5 ปีที่แล้ว

    Please help the Apixu Api is not working
    I'm not getting the currentWeather data on response json it always return empty. Am i missing a step? Help!

    • @waichee723
      @waichee723 5 ปีที่แล้ว +1

      I got the same issue as well, tried a few cities and the "current" field is always empty. I guess we will have to wait for APIXU to fix that and return to this later.

    • @waichee723
      @waichee723 5 ปีที่แล้ว

      Hey bro use Paris instead, its working -> &q=Paris

    • @baranidharanmurali469
      @baranidharanmurali469 5 ปีที่แล้ว

      Thank you bro I think they have fixed it it's working now🤟

  • @thomasj.5382
    @thomasj.5382 6 ปีที่แล้ว

    this tutorial only give one location which is london? what if i need to choice place and parse json?

    • @ResoCoder
      @ResoCoder  6 ปีที่แล้ว +1

      It's only the second part of this series. Later on we'll implement custom location.

  • @tahaelhosseny8596
    @tahaelhosseny8596 5 ปีที่แล้ว

    please set the link of kotlin Coroutines tutorial in video description so that we can get it by easy way a can't get the link yet , thanks in advance

  • @ArdaKazanci
    @ArdaKazanci 4 ปีที่แล้ว

    17 - 10 - 2019
    API has also been updates.
    Sample : "api.weatherstack.com/current?access_key=API_KEY&query=CITY_NAME"
    HTTPS is giving an error.
    You must use HTTP. Because the API supports this.
    Be sure to include "android: usesCleartextTraffic =" true "in the manifest.xml file.
    Thanks.

  • @almtg871
    @almtg871 4 ปีที่แล้ว

    i have a problem with "return@Interceptor chain.proceed(request)"
    when i run my app , it crashes

  • @sadzolot2
    @sadzolot2 6 ปีที่แล้ว

    What theme do you use in Android Studio?

    • @dk-sky3820
      @dk-sky3820 5 ปีที่แล้ว

      Material Theme UI

  • @indrajitpatil4999
    @indrajitpatil4999 6 ปีที่แล้ว

    Very nice

  • @JmGodlike
    @JmGodlike 6 ปีที่แล้ว

    awesome, thanks!

  • @СергейДеркунский-ж3р
    @СергейДеркунский-ж3р 4 ปีที่แล้ว

    It all doesn't work. The project on Github is also not working. The problem is getting a response from the API.

  • @NTTkrausus
    @NTTkrausus 5 ปีที่แล้ว

    I have to use weatherstack.com and i have CurrentWeatherResponse(location=null, CurrentWeatherEntry=null) on Emulator. Can you help me?

    • @RafaelDuarte
      @RafaelDuarte 5 ปีที่แล้ว

      they changed the api. Try with this code ;)
      @GET("current")
      fun getCurrentWeather(
      @Query("query") location: String,
      @Query("lang") language: String = "en"
      ): Deferred
      //equal to static methods
      // 15:15 - 22:06 --> Part2
      companion object {
      operator fun invoke(): ApixuWeatherAPIService {
      val requestInterceptor = Interceptor { chain ->
      val url = chain.request().url()
      .newBuilder().addQueryParameter("access_key", API_LEY).build()
      val request = chain.request().newBuilder().url(url).build()
      return@Interceptor chain.proceed(request)
      }
      val okHttpClient = OkHttpClient.Builder().addInterceptor(requestInterceptor).build()
      return Retrofit.Builder().client(okHttpClient).baseUrl("api.weatherstack.com/")
      .addCallAdapterFactory(CoroutineCallAdapterFactory()).addConverterFactory(GsonConverterFactory.create()).build().create(ApixuWeatherAPIService::class.java)
      }
      }

    • @zoranjankovic4708
      @zoranjankovic4708 4 ปีที่แล้ว

      me too

    • @zoranjankovic4708
      @zoranjankovic4708 4 ปีที่แล้ว +1

      @@RafaelDuarte thank you

    • @RafaelDuarte
      @RafaelDuarte 4 ปีที่แล้ว

      @@zoranjankovic4708 👊

  • @felipepereira3061
    @felipepereira3061 4 ปีที่แล้ว

    Sadly, im getting null pointer exception on response.toString() =/

    • @marshallborsos3396
      @marshallborsos3396 4 ปีที่แล้ว +1

      See my comment a few minutes ago.
      1. In ApixuWeatherApiService.kt, remove @Query("language") languageCode: String = "en". The current version of WeatherStack does not support foreign languages on the free plan, and English uses no language code (unset)
      2. Follow the instructions given by nehank poilkar to ensure you do not have issues related to the new parameter nomenclature and due to the fact that Android Studio by default does not allow access to the internet or non-HTTPS resources

  • @woodenspoon2658
    @woodenspoon2658 4 ปีที่แล้ว

    If you have problem nullexception or cleartexttraffic then add android:usesCleartextTraffic="true"
    to your manifest file

  • @pedro0563
    @pedro0563 6 ปีที่แล้ว

    How did you get Android Studio to look like that?

    • @BoxingHacker
      @BoxingHacker 6 ปีที่แล้ว

      MaterialUi Theme plugin

  • @자유야놀자
    @자유야놀자 6 ปีที่แล้ว

    thanks

  • @jamielindsey5680
    @jamielindsey5680 5 ปีที่แล้ว +4

    So frustrating that so much of this no longer works!! I have spent more time trying to figure why things are giving errors than actually coding.
    E.G;
    CoroutineCallAdapterFatory no longer exists and there is basically NO WAY of finding out how to sort it!
    EDIT;
    So I managed to fix this (???) by changing the code in ApiuxWeatherSevice.kt
    remove:
    .addCallAdapterFactory(CoroutineCallAdapterFatory())
    and change from this:
    fun getCurrentWeatherAsync(
    @Query("q") location: String,
    @Query("lang") languageCode: String = "en"
    ): Deferred
    to:
    suspend fun getCurrentWeatherAsync(
    @Query("q") location: String,
    @Query("lang") languageCode: String = "en"
    ): CurrentWeatherResponse
    and change:
    val currentWeatherResponse = apiService.getCurrentWeatherAsync("Leeds").await()
    to:
    val currentWeatherResponse = apiService.getCurrentWeatherAsync("Leeds")

    • @andrewtittle9130
      @andrewtittle9130 5 ปีที่แล้ว

      The example I just tried worked ok. Maybe versioning issues

    • @saddamjamali9273
      @saddamjamali9273 4 ปีที่แล้ว

      @@andrewtittle9130 first notice that don't use "q" because the current url has something different.

    • @roshnibatra7659
      @roshnibatra7659 4 ปีที่แล้ว

      I am facing same issue but even after applying the solution provided by you my app is crashing
      "Unable to create call adapter for kotlinx.coroutines.Deferred'

    • @РожковАндрей-х8ш
      @РожковАндрей-х8ш 4 ปีที่แล้ว

      java.lang.IllegalArgumentException: Could not locate call adapter for class java.lang.Object.

  • @zoranjankovic4708
    @zoranjankovic4708 4 ปีที่แล้ว

    good tutorial but give me back all null. Does anyone know what the problem is....current_text.text = currentWeatherResponse.toString()

    • @marshallborsos3396
      @marshallborsos3396 4 ปีที่แล้ว

      See my comment a few minutes ago.
      1. In ApixuWeatherApiService.kt, remove @Query("language") languageCode: String = "en". The current version of WeatherStack does not support foreign languages on the free plan, and English uses no language code (unset)
      2. Follow the instructions given by nehank poilkar to ensure you do not have issues related to the new parameter nomenclature and due to the fact that Android Studio by default does not allow access to the internet or non-HTTPS resources

  • @rohitgrover6485
    @rohitgrover6485 3 ปีที่แล้ว

    1.25x speed is best for these video.

  • @rebekahoj
    @rebekahoj 4 ปีที่แล้ว

    Nice tutorial but there is no seperation of concerns.

  • @mahnoorshafique3965
    @mahnoorshafique3965 4 ปีที่แล้ว

    Followed the whole tutorial .still my app is getting crashed.can anyone help me?

    • @juanandrew2446
      @juanandrew2446 4 ปีที่แล้ว

      There are changes made since the video is posted
      1. Apixu is no longer available and changes to weatherstack and also since android 28, you cannot access http by default
      Solution :
      - Look at nehank poilkar and Marshall Borsos comment
      2. Since Retrofit 2.6, coroutine call adapter is deprecated
      Solution :
      - Look at Jamie Lindsey comment, but keep in mind that query parameter is changed to the solution no.1
      3. If you get an error saying java.lang.BootstrapMethodError: Exception from call site #4 bootstrap method...
      Solution :
      - You need to add this code:
      compileOptions {
      sourceCompatibility JavaVersion.VERSION_1_8
      targetCompatibility JavaVersion.VERSION_1_8
      }
      under buildTypes in your build.gradle(app) file
      4. Ensure data type for each variable, since we auto-generate the data class using plugin
      These are the steps I did to make this tutorial works

  • @khaxbiker
    @khaxbiker 4 ปีที่แล้ว

    Another thin. for the call to the api. " api.apixu.com/v1/current.json?key=89e8bd89085b41b7a4b142029180210&q=London&lang=en" there are several parameter you have to change as current.jason -> current q-->query., key-> key_code . Another thing, Serialized didn´t work for me

  • @yahussainmazloom1
    @yahussainmazloom1 4 ปีที่แล้ว

    you Tutorials are very helpful, but in this video you didn't explain retrofit enough. :(

  • @AhmedAli-jx9ie
    @AhmedAli-jx9ie 6 ปีที่แล้ว

    the most important part of the Coroutines was skipped, and don't tell me there is another course for Coroutines because it doesn't tell how to deal with Coroutines in android environment

    • @ResoCoder
      @ResoCoder  6 ปีที่แล้ว +2

      Well, we'll come back to coroutines later on. For example in the part which I'm releasing this Saturday and also in later parts.

  • @littlegiant6402
    @littlegiant6402 5 ปีที่แล้ว +2

    app crashes after doing this second video

    • @stephanbranczyk8306
      @stephanbranczyk8306 3 ปีที่แล้ว

      Reset the first API key the web site gives you. That's what I did and it worked for me after they gave me a second key. The first key they gave me wasn't valid for some reason (according to their error message). Then, test your app using this URL below inserting your API key between the '=' sign and the '&' sign api.weatherstack.com/current?access_key=INSERTAPIKEYHERE&query=New%20York or better yet, just change your API to weatherapi.com, weatherapi.com supports HTTPS apparently, and weatherstack.com doesn't unless you pay.

  • @ajajitjaiswal
    @ajajitjaiswal 5 ปีที่แล้ว

    Thank you for the awesome Weather app tutorial. How to send @FormUrlEncoded for post request. and also how to send @Header while creating interceptor
    for example
    @POST("api/v1/login")
    @FormUrlEncoded
    fun login(
    @Field("username") email: String,
    @Field("password") password: String
    ): Deferred
    I'm trying this but backend not receiving these form fields

  • @jiankian
    @jiankian 5 ปีที่แล้ว

    IDE font size and Editor font size is too small. it's not best way for Make a course

    • @ResoCoder
      @ResoCoder  5 ปีที่แล้ว

      Umm, excuse me? Is the font size of 26 small for you? Android studio UI cannot be scaled.

  • @_erayerdin
    @_erayerdin 5 ปีที่แล้ว

    Eating watermelon has never been this technical.

  • @archindar_
    @archindar_ 5 ปีที่แล้ว

    Need to change from deferred to suspend in retrofit 2.6.0
    github.com/square/retrofit/blob/master/CHANGELOG.md#version-260-2019-06-05

  • @leelynnchanel
    @leelynnchanel ปีที่แล้ว

    I'm still receiving NULL despite of everything advices given by those in the comments...

  • @Manudoido100
    @Manudoido100 4 ปีที่แล้ว

    If you get CLEAR TEXT ERROR, lock at this solution stackoverflow.com/questions/45940861/android-8-cleartext-http-traffic-not-permitted, it worked for me.

  • @hassanammer7854
    @hassanammer7854 4 ปีที่แล้ว

    I can't understand from you because you explain so difficult

  • @FessNiklas1987
    @FessNiklas1987 6 ปีที่แล้ว

    Awful diction