Share
Embed
Transcript
28 slides
-
What are we building this week? Loading remote data from
OpenWeatherMap api 02 Project Demo Making Network Requests 01 Loading Remote Images Loading remote images from a URL 03 -
Week 7 Project Updates • Load current forecast data from
OpenWeatherApi • Load 7-day forecast data from OpenWeatherApi • Load and display forecast icons from a remote URL -
Retrofit • HTTP client for Android & Java • Open-source
library from Square -
Api Interface Define your HTTP api using a Kotlin/Java interface
Retrofit.Builder The Retrofit.Builder class will help generate an implementation of your api service Call The Call class lets you make synchronous & asynchronous requests to your api Retrofit -
Define Your API Interface data class Forecast(val temp: Float) data
class Coordinates(val lat: Float, val lon: Float) /** * Api response for OpenWeatherMap's /weather endpoint */ data class CurrentWeather( val name: String, val coord: Coordinates, @field:Json(name = "main") val forecast: Forecast ) -
Define Your API Interface /** * http://api.openweathermap.org/data/2.5/weather?zip=98119&units=imperial&appid=<apikey> */ @GET("/data/2.5/weather") fun
currentWeather( @Query("zip") zipcode: String, @Query("appid") apiKey: String, @Query("units") units: String ) : Call<CurrentWeather> -
Create Your API Implementation val retrofit = Retrofit.Builder() .baseUrl("http://api.openweathermap.org") .addConverterFactory(MoshiConverterFactory.create())
.build() return retrofit.create(OpenWeatherMapService::class.java) -
Create Your API Implementation val call = weatherService.currentWeather(zipcode, “some api
key”, "imperial") call.enqueue(object : Callback<CurrentWeather> { override fun onFailure(call: Call<CurrentWeather>, t: Throwable) { Log.e(ForecastRepository::class.java.simpleName, "error loading current weather", t) } override fun onResponse(call: Call<CurrentWeather>, response: Response<CurrentWeather>) { val weatherResponse = response.body() if (weatherResponse != null) { // handle the loaded weather } } }) -
• A Thread is an independent path of execution within
your code • Instructions are run 1 by 1 • An instruction can’t start until the previous one is finished; in this case, the 2nd instruction is “blocked” by the first Threading On Android -
• Android apps run on the Main Thread by default
• Every 16ms the UI will draw itself • Block the Main Thread, and you will cause slowdowns and jank in your UI • Block the Main Thread for too long, and you’ll receive an “Activity Not Responding” dialog Threading On Android -
• To avoid blocking the Main Thread, long running tasks
should be run on a background thread • Threads, Executors, Runnables, AsyncTask, RxJava, Coroutines, Work Manager - a lot of ways to do background work • We will use Retrofit for this week’s assignment Threading On Android -
• The Retrofit Call class enables us to asynchronously load
data • Pass a Callback to be notified when your request is complete • The work will be done on a background thread and you will be notified on the Main Thread Retrofit Call Callbacks -
Create Your API Implementation val call = weatherService.currentWeather(zipcode, “some api
key”, "imperial") call.enqueue(object : Callback<CurrentWeather> { override fun onFailure(call: Call<CurrentWeather>, t: Throwable) { Log.e(ForecastRepository::class.java.simpleName, "error loading current weather", t) } override fun onResponse(call: Call<CurrentWeather>, response: Response<CurrentWeather>) { val weatherResponse = response.body() if (weatherResponse != null) { // handle the loaded weather } } }) -
• https://openweathermap.org/api • /weather - returns current weather • /onecall
- returns many things including 7-day forecast OpenWeatherMap API -
• App resources can’t all be packaged with an app
• Images/Video/Music/etc must be loaded from the network • Multiple libraries available to load remote images in our app Loading Remote Images