Retrofit - Send a simple GET Request | Android Studio Tutorial
Вставка
- Опубліковано 8 лип 2024
- 🏆 My Online Courses
⭐Discount Coupon: LAUNCH-STEVDZA-SAN
stevdza-san.com
🐱👤 Wanna become a member? Join!
/ @stevdzasan
📸 Instagram
/ stevdza_san
Retrofit is a Type Safe REST client for Android Developed by square. It uses okHttp library for HTTP requests, and it's one of the best tools for performing network requests in android applications. In this video I'll show you how to send a simple GET request, receive a result in JSON, convert it to Kotlin object and display it back in our app. So with this video I'm starting a new short tutorial series about Retrofit library, so stay tuned!
Retrofit requires three main components:
1. Retrofit Instance:
We can create an instance of Retrofit by Retrofit.Builder class. And we have to specify the base url and converter factory at the time of the Retrofit instance creation
2. Model Class:
Retrofit needs a Model class for sending and receiving Request. It uses the model class for parsing server Response by using converters like Gson, Moshi etc.
3. An Interface for possible API calls:
The interface contains methods that represents possible API calls. Each methods need a base Url end point annotation that represents the Http methods like GET, POST etc.
Timestamps:
0:00 - Introduction
1:12 - Create Model class
2:28 - Create SimpleApi interface
3:15 - Create Constants class
3:53 - Create RetrofitInstance object (Singleton)
5:02 - Create Repository
5:43 - Create MainViewModel class
6:46 - Create MainViewModelFactory class
7:25 - Implement the logic
9:29 - Handle Exceptions
Finally, an API call example that is easy to follow and is structured in the proper way. Thank you.
Its 2023 and this tutorial is still serving well❤
This video is GOLD. Came only for Retrofit, left with much more.
It's actually not, because you don't understand why he did it, you can just copy the steps and that's not good teaching
you make android intern's life easy and smooth, thank you again
man...seriously... your content is amazing :| exactly on point, so simple explained.. so clean :) thank you! we really appreciate your work
Thank you Vlad, I'm happy to hear that I helped. 😉
Your Tutorials are great....keep up the great work...love you
Well done! the only thing I want to suggest is to use another name for the model you are using: Post is easy to confuse with Http/Post.
the rest is great!
Your channel and awesome videos made me start developing Android apps, thank you!
This was incredibly useful. Thank you.
Great tutorial, THANKS, looking forward to see the next tutorial/s
Great tutorial Waiting for next one👌😁
Really helpful video. Really like the way you structure things
Amazing tutorial as always! 😊 I like your channel.
Awesome tutorial. Thanks for this tutorial.
Waiting for next one.
This is just beautiful..... Thanks Bro
It's crazy how you need 5 files for a simple get request, which you can do in 10 lines in JavaScript
Same thoughts. Why complicate simple things !?
In android dev this example is very good. 5 files need to make app with SOLID principles and easy to understand, service and extend. In youtube it rare video there author use architect principles. It is a best way. And yes android dev (Kotlin/Java) is very different from other devs. Knowledge of language is a micro bit of required hard skills. To make normal (not good or awesome app) need know many things: life cycle of activity, lifecycle of fragments, permissions, data store, UI/UX, databases,multi treading and much more. And u can make one gold file with all logic but it worst way. It garanty memory leak, unstable, lose data and bad user experience.
Wow ... clear and consice !!
Thanks. It was a really understandable training video!
Amazing tutorial
This tutorial is very well done. Too bad i didn't understand much. This seems so complicated for something supposed to be simple. But it works great !
lollll same
I hope you understood it after a number of replays.
50 new files, 300 new lines just to make a get request omg
thanks for the video, helped a lot man
Good tutorial Bro!!
Thanks. Great example
good explanation of the material
You make really great videos.
thanks!!awesome course
What a Solid Explanation!
Thanks 🚀
i have an URL with xml content on it. I want to GET the xml data every time I start my activity. Then I want to present certain parts of the data in my app. How should I go about this?
Ciao Stvdza, thanks a lot for the video! How should I handle a SocketTimeoutException error (the App keeps crushing in that case)?
Your videos are very good and very useful. I would love for you to show examples of delete and update with retrofit, it would be very helpful. Thank you so much
Damn this is a coding style I can aspire for
Great 👌, I have a question about to how use GET to search published articles in your personal website that u loaded in a webview using searchview
how did you access the TextView directly in MainActivity?
thank ,help me a lot
2024 and this video save my life
Guys if anny watch this video in 2024 , two things for fix :
in the final textView.text u need declare dis var : val textView: TextView = findViewById(R.id.TextView) ; and if u have a error here, verify ur main_activity.xlm and check if ur TextView have the id : TextView.
AMAZING !!!!
can we use interfaces instead of classes for response?
Muchas Gracias Sensei
The way you provide the dependencies through a pattern is it called MVC or MVP ?
thank you for this content
You're welcome 👊
Thank you! :)
Thanks!
Hey bro when executing the app it closes automatically and does not consume the api, but does not show any error
Thank your code is very easy
Thanks buddy 👍
Awesome!!!! ♥♥♥
Thanks a lot
when we say private retrofit by lazy dont we have to specify the datatype like private val :className by lazy what exactly is that supposed to mean
Hi! Thanks for the video. That maybe simple in Jetpack Compose but compare that to other libraries (for example with React Native) i´ve used that´s not simple at all 😄
hi i have a problem
when i call the api for first time it work but if i clicked the button for second time it make 2 calls
what is wrong
Hi! Help me, please! How should I catch the "no internet connection" error?
thank you a lot
how to handle SocketTimeOut masterrr??
How can retrofit get api callbacks for example in my case I am doing back end validation with laravel
"email": [
"The email field is required."
],
"password": [
"The password field is required."
],
"device_name": [
"The device name field is required."
]
How do I consume this with retrofit to show the relevant error messages.
Hello ... is this architecture is MVVM?
Please make a video on how to consume GraphQl api with Apollo using MVVM and coroutines with repository pattern
Does the order of the variables matter when parsing Jason?
hayır
@Stevdza-San I am currently using jetpack compose, does this mean I do not longer need view model factory ?
I haven't used ViewModelFactory in a while. Becuase I'm using Dagger-Hilt to inject all my ViewModels directly.
I still need to learn Dagger-Hilt and the importants of injection. I am so behind sadly. Thank you for your knowledge 🙏
Hey stevdza! I just recently relized in your udemy ToDo app course . Which i own. You explain how to use dagger hilt and just like you mentioned in this messege. You use dagger hilt to inject all your view model directly. And that is exclacly what i will learn in your udemy course. Correct?
@@henrygalvan9333 Correct ✅
So I was using this as a guide for interfacing with a different API. Does my data class need to represent the API's response exactly?
In other words, can I leave out a few data points that I don't care about that is being returned from the API?
Yeah sure you can. :)
Thx
Please make videos on restful api. how we are making APIs.
Very nice
Mutable live data trigger so many time i got wrong response how can fix
thank u
Awesome! Thank you a lot!
How to send specific data?
Hello sir... You might have seen me in most comments. Sincerely this channel is crazy nice. I lost count of how many developers I've shared the link with. Thanks bro.
Would you mind doing retrofit with progress bars when uploading/downloading data?
Interface Lazy does not have constructors
I get this error :
*Unable to create call adapter for class java.lang.Object*
if you solved it please tell me how ^^
Thanks it would be more helpful if you could lower the pace of your great tutorials
Totally. "Write this, because this is how it works" doesn't explain much....
8:50, I get the word "Observer" in gray and I don't know why (Line 22). If someone can help me, because the program does not work for me and I suspect that is why.
Hi do u still need help?
Great explanation 👌. And can you make tutorial on paging library.
Yes there will be a video about it in the future.
how to get multiple posts
Very informative, but so fast!!! I had to pause the video a dozens of times just to blink.
Thanks for that tutorial bro, it helped me a lot. Now I'm trying to get data from an API de returns an Array of Objects, instead of an Object, but I can't solve this problem and the GSon's User Guide didn't help me so much, could you or somebody else help me with that?
Me too. Have u got a solutions??
@@ilhamaliyudin4779 Unfortunately I haven't found any solution, I ended up using another API :(
But if anyone could help with it would still be great
You got solution ??
I am trying to fetch data from zomato api but i am confused as if i call it as shown above it will only give 1 restraunt data
I'm getting E/AndroidRuntime: FATAL EXCEPTION: main error
I get the same exception, could you find solution?
@@firuzaaliyeva5382 compileOptions{
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
} //add these lines in build.gradle
amazing video but so confused on how the folder structure works, or how they're all connected to each other. There's also no reason given for all these folders, classes and instances made, literal copy and paste. Like why are you bouncing a method call 5 times? I don't even know where the code ends up to at that point
Hello, I like your tutorials so much!
You helped me with SQL dataBase!))
I have an Idea for you
could you make a video -How to make database using TXT file Only-
We have 2 textViews (Word, Translation) and 1 button (Randomizer) which randomizes them
viewModelScope does not work also i add the library can you help me to fix it please
Add this line in your build.gradle(:app): implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0"
please show us how to use @GET and @POST with the SketchFab API. please please please :)
Suddenly stop on android 4.
Why to use !!
why not use var?.let{ //code}
Could you provide the Github link to this project?
😍😍😍😍😍😍😍😍😍
Could you please share the files of this project? Thank you
awesome tutorial, but holy hell you talk and type fast xD
I'm learning not to talk that fast 😄
@@StevdzaSan but anyway, thx man, have been trying to figure out how to contact my restAPI for the last 2 days, and nothing worked, but 1 hour with your tutorials, and now it works like a charm! :)
😁
Got stopped android
Hi I am a beginner. I like your content. I also bought some of your video on Udemy.
But I really really wish you would slow down the pace.
It is very hard to follow, you are going to fast man. Slow down, and explain things a bit more detailed, and your content would be even better :-)
Thanks for your suggestion 👍 😊
Whats this mess? I mean why should we code this much to make a simple request?? Why cant we just call a function like request.get("url")?? I am fed up with android and its interfaces, templates, frames...
This is out of date
maaaaan , why Kotlin 😭 , anyways , thanks you a lot
class MainViewModelFactory(private val repository: Repository):ViewModelProvider.Factory {
} throws me an error "Inheritance from an interface with '@JvmDefault' members is only allowed with -Xjvm-default option" what should i do?
i stuck at the same problem, have you found the solution?
@@dimasfaiz2716 No
For anyone getting error message "Class 'ViewModelFactory' is not abstract and does not implement abstract member", remove nullability sign (?) in MainViewModelFactory.kt. Like this -
override fun create(modelClass: Class): T {
Mutable live data trigger so many time i got wrong response how can fix
Mutable live data trigger so many time i got wrong response how can fix
Mutable live data trigger so many time i got wrong response how can fix