How to set up an error response message for the app?

0

 So we’ll just do a new callback and android it will auto complete a lot of stuff for us and so in the on response so when we get the response from uh retro from uh the online api let me hide this online api will just check if the response dot is successful sorry responses isn’t successful or response dot body is equal to null so if if there’s some problem in the response that we have received so then we’ll just um  uh we’ll just return from this there’s nothing more that we can do but if the response is successful so if we come out of this branch now let us parse this so you can head over to to see so we return and then we we pass this response and set the value of this live data object with called m api response so there’s something that we don’t talk too much about is what’s this mutable live data.

So again a quick like we’ve not talked about this before so i’ll try to go as in depth as possible any like in a repository you have your data now to store the data in android uh you would mostly use a mutable live data or something called a live data so if your live data will change you know from from time to time use immutable live data so uh which will be clear again in some time i’ll show an example of it here what we’ll do is m api response is equal to response dot body so a couple of things here oh oops dot set value and we’ll paste this here so if you notice in the interface it returns like it is wrapped within this call but technically it returns an object of this type api response which is already written so it’s in the starter code which um it’s a model for how the response is going to be so again now we’re not going too much in depth here so again i’ll give you a very no it’s like a crash course in a way and feel free to ask questions and even after this.

So i’ll explain it in depth for how however long you want but in this one-hour session you know we will try to um go as quickly and as slow as possible at the same time like good balance so if you notice here this tree structure i mentioned right you have a main object here you have  current hourly and daily so what you did was we have current current hourly daily and also time offset so these four fields are what we are interested in so current stores the current weather early is um an array that stores early weather so the weather in the first hour will be uh where’s temp yeah temp 85 the next r will be 84 all right so we have this array here hence uh early is an array and current is not right because current is just current whether there’s no array there then we also have one for daily now if you notice within daily there’s again temp so let me not do this example let me do within early so it’s it’s a tree structure right an object within objects.

So within this early array each object is modeled somewhat like this it has this date which is in unix format right this is the seconds from like 19 unix format you can check it out on google and uh google what it is so a quick this is like the number of seconds from 1970 this is a way of representing time in computer science we mostly use unix time formats number of seconds since 1970 date then temp and then weather is what we’re interested in so within our hourly if you click on this class date temp and weather then within whether there is main and icon mean it would be like clouds so again you can take your time and at your leisure you can go through this and try to see what it means um but in like in an overview or like a summary of what we have done here in this models which is done for us already is it just uh creating a model of our response so very quickly again quick refresher very quickly let’s say in this object lag long current early daily oh sorry i mean time offset current early and daily we don’t really care about these three uh fields.

So all what we the fields that we care about current army daily and time of set we name the variables as the same as this year like time of set with the underscore name it here as the same and and work around this and create getters for it so uh we’ll head back over to our view model now this mutable live data is like a wrapper for actual api response class that we’ve just created so we just set the value of it to the response.body now the parsing is already done for us behind the scenes so we do not pass through this the tree structure it will return the api response now this could get a little boring so don’t worry we’ll go through it a little quickly um to do three is over now the interesting part happens why is it interesting because now from 4 to 210 we get to manipulate the ui of of our app function so everything in main activity now.

So we already have created instances of the each of the text fields here like temp weather high low so temp will be the current temperature whether it’ll be like clouds or rainy you know the text textual weather then high and low will be the high in the low text views right let’s let me show the weather card layout so you’ll understand like this is so again you can i just use all the skills that you have covered in the previous sessions so you can check it out at your leisure and be sure to ask me any questions how we did this um so this is temp this is weather this is high this is low and this is the image view for the weather.

So image view weather text view temp text view weather text view high text below which you can don’t worry it will be clear now initialize view models let’s create an instance of a repository so to do this there’s a specific way to do it let’s do m weatherview model so there’s already an uh a variable that we have uh here that we have here we have to initialize it is equal to so here’s how we do it view model providers which is deprecated but we’ll still use it for now so deprecated means they won’t support it in the future which is okay for now uh we’ll just use it for for now it’s an easy way to do it and it works so we’ll stick with this uh.