In this video series we will learn how to use Retrofit, which is a type-safe HTTP client for Android and Java.
Retrofit allows easy communication with a web service by abstracting the HTTP API into a Java interface.
In part 1 we will set up Retrofit in a new Android Studio project by adding the necessary Gradle dependencies, and then already do our first GET request.
We create an interface with 1 method that we annotate with @GET and the relative URL to the API endpoint of the REST API we want to query. We then create a Retrofit instance, define the baseUrl, add GSON as the converter by passing GsonConverterFactory to the addConverterFactory method, and let Retrofit create the implementation of our API interface at compile time.
The Call object that our GET method returns encapsulates a single request + response. With enqueue we can execute this request asynchcronously on a background thread and get our result back in the onResponse callback. OnFailure will be called if something in the process of communicating with the server or processing the response went wrong. With isSuccessFull we check if our response code is between 200 and 300 and if that’s the case we can retrieve our result from the response body.
GSON will parse the array of JSON objects into a List of the Java model class that we prepared beforehand.
Watch the whole playlist:
Example code, links & dependencies:
____________________
❗ Subscribe to the channel:
⏯ Watch more tutorials:
⭐ Become a channel member for exclusive tutorials and other perks:
㊙ Help translating the videos:
💚 Support the content:
💬 Join the Discord chat:
🎧 Audiobooks and podcasts for programmers:
👶 How to get started with Android development:
📣 Follow Coding in Flow on social media:
Facebook:
Instagram:
Twitter:
Blog:
Nguồn: https://zagran-tour.com/
Xem thêm bài viết khác: https://zagran-tour.com/cong-nghe/
make your life easy
the latest version of retrofit for example 2.9.0 will cause an error
use this instead :
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
Here's an example:
I want to access this rest api https://danepubliczne.imgw.pl/api/data/synop so I write https://danepubliczne.imgw.pl/ in .baseUrl and the relative address in @GET. But all I get is zeroes. My repo is here:
https://github.com/Zemogiter/AmaWeatherApp
Any help?
is this playlist all about networking ?
Thank you so much, also clapping in end 😍
I'm trying to get list of object from my api but i got an error called: expected begin_array but was begin_object i didn't find the solution can you help ?
Danke Schon!!!
Hello teacher, I have been following you for almost a year, and I am happy about that, but I find something that is missing from me, which is how to display specific data from the table for each employee as each employee sees the work required of him only
sir I use retrofit in my own project with flask backend and face SSL Handshake Error. I Google it but nothing found :(. could you please help me to resolve the error.
thanks you so much
so good explain the code. thank you
What if their is only base url available?
Is it always preferable use to retrofit library compare to other library
Is this in JAVA?
please use recycler view
My data is returning null my base url is :.baseUrl("https://covidtracking.com/") and my relative url is : @GET("api/states")
Thank you so much for these free tutorials! I just got my very first internship as an android app developer and now I have to learn and get used to these things, you're videos are really helpful. You're a lifesaver! Love from India <3
how to can i connect my localhost server ????
Starting with version 2.7.0 (December 2019), Retrofit now requires at minimum Java 8+ or Android API 21+. So if you're seeing a runtime exception about "No static method metafactory", add this into your build.gradle file in the app module.
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
…
}
Hi, Thanks for the tutorial. As mentioned in the video as your plan to do, do you have another tutorial combining retrofit with android architecture components?
But the most spoken language is Chinese, more people don't speak English that people than do, that's a very bad analogy btw
i want to know if our json object is not in proper sequence and also we have to sequence the object the How we gonna do with retrofit??? Is retrofit method is more complex than the normal one without any library??
can I send from my android app csv file to flask server?
I will generate dataset from android as csv file, send it to machine learning model hosted on flask server, and return the prediction.
at 1:34am.. the clapping at @19:15 scared the shit out of me 🤣 Great video!
Thank you so much, your video just made me continue my last Project which i was stuck in !
Your videos are truly amazing that I have decamped from one I always thought was el dorado to yours, thanks.
However I keep getting the exception javax.net.ssl.SSL ProtocolException. It makes me sad
I have created a working example on GitHub with latest dependencies, for reference – https://github.com/rastoarpit/Retrofit2_Java
Beautiful. Bless u
Hi jsonplaceholder site is error now
How would you make the call with Retrofit if you had an object and an array of objects? Would you need 2 pojo's ?
like this, for example : https://api.covid19api.com/summary
Thanks for all the great tutorials.
When I try to do what you do my app doesn't start anymore with the message "app keeps stopping".
This is the code:
https://pastebin.com/JqwwNcBS
If I remove everything except the api class and the "RKIApi rkiApi = getRetrofitInstance().create(RKIApi.class);" it still happens.
Danke, du bist toll!