Best Practices For Consuming APIs on Android

so the first thing I want to talk about and this is square is retrofit good and retrofit is essentially a client for connecting the arrest web endpoints to your Android application it kind of what they do is make it super simple to do we’re talking four or five lines of code and it handles all the downloading all the parsing so it’s really kind of magic when you’re used to writing ten twenty thousand lines of code just to do this stuff robustly and I was going to show a little bit of what it looks like so here’s the kind of their documentation so something like getting just a simple get request where the URL can vary is literally this is pretty much the gist of the interface you have to write so it’s really super simple so that’s sort of the backbone in your stack right that’s your interface layer and this does come built-in natively to use the core Android HTP libraries which are kind of terrible to put currently but what it does allow you to do is with a second tool also written by square which is called okay HTTP allows you to swap out to use this as your backbone instead and what OK HTTP does is a lot of nice things it will do gzip for you automatically it’ll do SPD Y if your back-end supports it automatically it will do connection pooling automatically it does retries automatically does HTTP cache automatically and you can use the expiration fields on the HTTP and all that sort of stuff to make your cache break so really this does a lot of really good things it handles a lot of network flakiness like back off retry and all that sort of stuff so it’s a pretty robust set that before everyone’s trying to write this himself and really what you end up with usually is something that just does one retry and then you’re bored or maybe two or three you know so nothing nearly as robust as this so that’s sort of the second layer to your stack here yes and we’re seeing yep actually just looks in your class path that it finds and it swaps out and uses it so it’s pretty simple which is written by the same people so that’s sort of why so it’s really nice if more or less just export of their whole web stack and then open-source contribute a lot more to it so that’s sort of like phase 2 of the stack here and agreeing to go to detail of how this all works but this is kind of the overview and then the third phase is so I haven’t talked about any parsing yet right so all we have now is we have a downloader and we have sort of an interface on top of it which is retrofit so as far as actually parsing we’re using like string parsing themselves so you know me how you have to maintain stake if I look at the objects I have to do okay it’s one of those tokens then I go here then I go there make this whole the statement whole yeah your whole parser and you can parse their goal code and get everything done so that you can just just all this go just to get a plain old Java object so the third part here is JSON which again it’s expected by retrofit but if you use JSON Oh magic just kind of happens under the covers and during the you set the type you build the code like we’re going to to represent the JSON object and then when it comes in from me HTTP call you just get back this fully inflated Java object and you don’t have to worry about any of the token none of us none of the parsing stuff you just get objects dealing and objects are simpler than writing parsing code yes you don’t have to write anything it’s Dom yeah you can you can use jackson if you want stream-based it’s a little more work and also one of the drawbacks to this is jisan with this stack will only work with JSON or based data right it won’t do XML but you can plug in an XML parser to this stack as well Jackson Jackson has an X I’m not a library that I could not live with me get working well it’s easy to swap in a converter if you can get the converter in the first place I couldn’t get the converter room and I just I get a point and stop fighting it’s a little harder but you can do it zesty sense that’s sort of like the whole stack so I kind of want to talk about what I’ve made so far and kind of where it’s going so to come here I already kind of made a project to start with here that we’re going to add this web downloading to and see how this whole stack works and how to make use of it so basically what I have so far is just a kind of a simple application that it’s hitting twitch TVs client or their Web API because it’s all open you don’t need to keep your anything so it makes it easy to demo basically it’s going to

download it it’s going to render it into a list view and just show you the preview of the channel and some text the title and that sort of thing so basically parse them all the data and then simple UI to show how it works right puts it all together ensures you this list view with all the information from the web yeah this is just an app with the shelter’s and doctors if your client is your Flickr with buses right now yeah basically the adapter has data but it’s about it so yeah it’s not a whole lot here and I realize people probably show of hands who’s ever done Android at all couple okay so I’ll go somewhere in the middle here so it’s basically a brand new HelloWorld application and then I just added the UI code and this is to do in there where it’s time to download the data and I have a method that’ll take that data and just render it so I’m not going to cover how that rendering stuff works but I’ll cover how to get the data parsed in in there with all this good stuff so basically now so where you start right now I have this twitch API I want to hit how do I do it and how do I do it in less than a couple days right so to start basically so I already talked about the stack so we basically have to go to the website of each of the three of them and they have the jar file so retrofit right off their github page as the jar that you can download and already having saved also am I going to square okay HTTP so that again is your downloader it tells all your cookie stuff so that is also a jar so it makes it nice and easy you don’t have to integrate it in your project or anything that’s difficult and JSON which interestingly just a in fact I think all three of these are just Java I don’t think they’re Android specific so you might actually use this for these only places as well so jisan I know I’ve use on other projects for sure and again that’s just a jar Cygnus downloaded so basically your first step now is now we have three of these downloaded so Wellston lose them just lost my cheat sheet I’ll just read download swim it really is right so they’re I mean they’re they’re all amazing you could use main if you like maybe varial to have that take care of you for the downloading we use downloading yourself and importantly the project what your what your process is and when you’re doing with it initially for this demo we’re just doing so basically in this is Android studio it works very similar to eclipse if using that you’re just going to drag and drop these into your lips folder so lives okay and then I got the other two down here lives and then the one got you if you’re in Android studio you got to make sure you sink Gradle which is this little guy or it won’t be able to see any of the files which will probably baffle you the first time you do it so now we have the libraries installed so then the next step is okay so how do we connect like what are we hitting here is website or wherever they have that information you going around sample API call so what you need is actually the Alpha or what it makes it easiest is to grab a sample response from the API so you take one of those pieces of output and then we’ll show you how to turn that into how generate objects that match that perfectly for the library yep the plan is that if you don’t have an API you’re finding a guy that you want to work with online that publishes that makes available JSON implement you go poke it for a second and get a typical response back and then from there you can go and continue the process of actually building the app so that it can do so for this demo I was just looking for something that’s a simple API that doesn’t have any off because that doesn’t really prove the demo here although you can’t accommodate that so I look for twitch TV they have a lot of public api’s and the one in particular that I chose with stream list which is sort of like the root level display of all the popular shows that are on right now so their documentation is nice it’s online and when you’re browsing through here it tells it talks about the parameters and it gives some examples which is actually the easier way to just browse of this so the one that is interesting to me is this list JSON somebody fault XML if you put this JSON

then it will JSON it for you and so what I did is basically I took this I ran it through list a JSON right I didn’t put any parameters I’m just getting the basic response here and what it gives you is this nice JSON response right and if you look at it so we have the same object just kind of keeps repeating right and each one is kind of has the channel info it has the overall info up here and then inside you have this channel info and it has the screencap urls in the name of the game League of Legends and that sort of stuff so looking at that I can say okay this is an array of this same content right so the way I want to do this so again we’re trying to do this parse we’re not writing any code as little as possible so what I do is I find a sample output this guy right here I copy/paste one of these right all the way down to the bracket so I have like one representative item here right and then what I like to use is this JSON schema to POJO org site and what you can do is that I think it lost my savings our list but is I’ll just cheat on this one but what you can do basically is you just paste it in here and what it’ll generate now there’s some settings you want to pick is you want to pick you come in here and into your package name so you’d want to go to your app and see where you plan on putting it and it builds classes that match the structure of the JSON so that as long as you’re using and it’s got options for which libraries you’re using as well so as long as you’re using language chosen everything should just work and you can’t download a jar that contains the classes that generates or preview the classes you can copy them out of there and paste them into your into your project and as long as you as long as you put the classes in the right structure as you fold it between the name and address record here you won’t have to do anything to make it work if you watch it’ll just all work because everything you write the package is all right it’s using the right classes and their trouble settings here you can fire them with the types long integers double numbers there’s a couple of things that you can set up so the nested objects you’re saying it will actually so let me show you this one so yeah I don’t to type in all the packaging because it’s a pain to line up but basically just figure out we’re going to stick this right type in the correct package name give the root class a name I was kind of just going to quit TV data it doesn’t really matter you want to pick JSON because this isn’t a scheme it’s just JSON right so that’s one of the tricks try to pick a representative entry because some might be missing fields to try to pick the biggest one if you want to use this technique annotation you can actually annotate it with Jackson of JSON and that sort of thing I picked none because in this case when this generates these objects are going to line up exactly because I’m generating it from them right so there’s really no reason to annotate them and I like to use primitive types that I want to do Mel checks but you can also not use that and then just make sure you Mel check everything you get a blank integers or strings and that sort of thing so then all you do and here you can generate the jar but I used to just do the preview because I think it’s quicker and what it generates here so the first thing we see here is channel so that was actually the interior class if you go back to our list here just channels inside right so generated channel and then it has each of the things here it does get a little wonky on some stuff like it adds this additional properties flag that I usually just delete because I don’t need it and maybe some of these fields here you might see like object subcategory it probably mess that up in this case I didn’t care but sometimes this is 100 percent but I probably get you 90% thing or better you ought to type all this crap out which takes quite a while so the Q’s getters and setters for everything and then if you keep scrolling down it will have the second one will have the higher length one down here too so here’s the sacrament which TV data and that’s the name we gave it so you have to give the name for the root level and the interior ones I can just generate because the names they have to line up that’s the way jisan works and if basically what you want to do is use this to get you really close and then you can pull up the jisan documentation and tweak any issues but for simple stuff it usually just kind of works that’s why when I said magic happens ji-sung uses the structure of the object that you give it as the table level maybe want to jam the JSON to and it uses the names to match things up so that as long as you haven’t lied to about the behavior it’ll loop just Jim thinking that you’ve got strings and because it just auto parcels it just takes a POJO and takes JSON looks for names that line up and it just makes it is how it works basically so let’s chase a question so if you

think about it that does introduce slight bit of danger and you have to be very careful with your data classes because if you do introduce your results so JSON doesn’t care about whether they’re private or public Peter uses reflections to populate the fields so you can use private fields in your classes and if you’re not careful and your API developers don’t communicate they just add something to the API and you’re using one of those private fields for your own instance but they happen guess that fielding and object that they send you back contains that field the field is going to be populated for you and it might not be what you want so be careful with that just just a morning a heads up keep your eye out for that yeah and if you don’t want the names to have to line up you can use the annotations and do it that way but it’s a little more work together question I don’t think it’ll figure it out now so what I usually do is with theirs when there’s a raise I’ll just take one of them and paste it in and then when I’m ready to parshat is put list in front and it’ll figure it out that way like I said it’s not a hundred percent but it gets you really close this is just a mock guessing for you if you need to look at you’re using it all right mostly just saves you typing Lissa’s in this anyway it’s a list of odds I think you can use a very long list I’m not certain exactly – so basically I have I say these office for speed here but I’m going to paste in those two generated classes right now and like I said usually I paste them in and I delete that objects thing they do at the bottom just to grab all additional tags because I don’t really think it’s terribly useful so so at this point we generated this channel thing and we generated this stream data which is the higher level class and again when you generate it just point it to your correct name space and you won’t have to even mess with this it will just drop right in so at that point with this okay so we have these pojos we don’t have anything download or working it right so kind of next step is so now we should actually write a class to go download some stuff and parse it so to do that I’m going to yep I will cheat a little bit mmm won’t get is this one open up so this is actually the entire API code it fits on one blown-up giant screen it’s actually really simple which is what I love about using this stack so basically I guess some of the overhead here is we made this static instance because it’s really no reason not to this is using again retrofit as kind of the base so what you’re looking at here is pretty much all retrofit code you don’t really see okay HTTP that’s under the hood you don’t really even see jisan that’s under the hood because again retrofit will just use JSON if it’s included and give you a really cryptic or if it’s not yeah so that’s good didn’t have to so basically all there really is to it so there’s two steps your first step is and this is just some Java ism if you’re not too familiar Java but basically I want to make an instance of this thing if it doesn’t exist yet so your first step is basically create arrested after that just points to your endpoint and that’s it so basically what new rested after and you just give it the URL of where your base address is pretty simple you’ll get that from your API yeah but no interfaces at all it’s just here’s your base endpoint right so that’s step one with retrofit and I get all this stuff is in their documentation it’s fairly decent it’s not a whole lot to it but then again there’s not a whole lot to api’s in general as far as interfaces going only have a half a dozen things really um going in oh that’s a load I said your computer’s rebooted because of problems that’s great so that’s step one step two is now wait to define the interface right and this is where you go to your API documents or the guy sitting actually that wrote the thing and test coma how the heck do I actually communicate with this thing so in our case we’re going to flip back to our interface document that we got from twitch and so what they have is so we know the base URL it’s kind of step 1 so we know it’s API / stream / list JSON right so that’s really annoying may go away so that’s basically this right here so it’s the

annotations are dead simple so get request you put the word get post you put the word post super simple has head it has all the basic stuff so get request / stream / list dutch json guy with the data based on the funny guy / VA guy quick so that you can call all other pads on your restful api with different methods and it’s dead simple yeah like it doesn’t get any simpler than that because you’ve probably written hundreds lines of codes to do this sort of stuff before and then this names arbitrary I just called it get streams and then basically what you can do now there aren’t any here but it’s a you had an optional like say with streams slash username slash list JSON right it’ll support that sort of stuff dynamically as well if you need to do dynamic URLs in this case we just have query parameters so to figure those out I just go back to the documentation here yep and in this case I have a long list of stuff right so I don’t look at all at once I want to page it and I get to the bottom load another chunk and the left child another chunk so I expect to see some sort of page size parameter some sort of offset parameter so when you look at the documentation you see just that you see there’s also some other ones I don’t care about but there’s limit so number of results returned default 50 max 100 so I want to set that to something and you see offset so return your offset default is 0 so again when we request it with mouthing words get page zero and 50 results because that’s your defaults so in this case we are just going to say and this is a symbol to this query parameter and that’s the name of the field on the endpoint that will string the limit right and this is us I call it limit – because that makes sense so it’s an int limit and then a second query parameter called offset and an offset and that’s as simple as defined interfaces so the second part is now the response type so how the heck like how does this parsing actually happen now right I don’t see unicode as it one line of code here so this is all the parsing you wrote is right here is literally all you have to write so in this case callback is the way you denote to retrofit that you’re going to have an asynchronous callback when the request is done yeah you can already retrofit in two modes and change them but it’s not per after or endpoints the way you cooperate retrofit tree to do it synchronously or asynchronously and I believe you can actually do it on the same endpoint grief all the same path on an endpoint with in asynchronous mode for synchronous mode depending on which method call because if you love the return type of this method avoid so the what happens if some type is void and it’s going to pull back as the last parameter so the callback what happens when you make that call retrofit recognizes that and it immediately yields control back to the thread that make the call and then once the network traffic happens and it’s actually got a real closure 2 3 to work with it takes that photo and it calls methods on that callback that then after out back into your code so the things to happen or if you wanted to you could wait for the call to come back which you’d have to do a few a thread but that’s Android detective you have to be responsible for not locking up the system but you can wait for the welcome back and then you proceed from there you’re pulling everything off if you’re doing that and the way they did that’s really simple basically all you do if you want to call back you put this last parameter of the word call back in it and you get a callback if you don’t want to call back if you want it synchronously then just put your return result right here just put your type right there and it’ll be synchronous now they only catch on Android is he’s saying is you’re not allowed to run downloading on the UI thread it’ll crash the AB cos crashes it for you so depending how you architected it either approach is valid it depends how you build it question you can yeah and we’re going to cover that a little more later but you can yeah that’s all kind of like what we’re calling back so okay so then the last part of this really is so well where’s the where’s the parson where’s like the three hundred lines of code where your tediously parsing and probably got it wrong ten times and don’t realize it it’s right here so basically all you say is callback and then it could be any type you want inside in this case we know it’s a list and here’s where I’m manually putting the word list right so I’m just saying it’s a list of this justin TV stream data which is our root level object and that’s it that parses it that’ll this will do everything so I all I do is like instantiate an instance of this guy I call get streams and it does all the parsing does all the downloading and comes back to you so that’s sort of a next step it’s

really not so I’m just going to go to our main activity class where it’s waiting it’s waiting laying in wait to download something so this class just to give a little bit of a review is it some yeah is when you first open it detects that it’s empty it creates a list view and this all adapter Android talk that some people know some well twelve yeah so what I will a minute it basically is going to make a grid view which is Android UI construct it’s going to detect that it’s empty it’s going to go download the data it’s going to get the data back and it’s going to populate it and then as you scroll and hit the bottom and run out of data it will automatically go download more data and append it so I’m not going to cover how all that UI stuff works I’m going to show how this downloader wires into it and I kind of try to keep it kind of isolated here so basically I have this method called download data and it’s at this point where it’s detect that it needs to download data and it should just do it right here so here is basically our client so there’s a client we just wrote and it’s single din that grabs our stuff right so Raj is it’s just a static instance of grabs our stuff so get twitch TV API clients like Our client who has one interface to do that one call and that’s it right get streams and get streams if we recall wanted the limit so the number of items on the one page the offset into the pages and then a callback and that’s it right so I did a little bit of tracking I just made a constant in this file called items per page I think I said it 50 and then page number I start at 0 and every time I finished down with my anchorman at 1 so all we’re doing here is just multiplying by what page we’re currently on to get our offset for our subsequent requests because this method will run every time we try to get more data and keep track where it was and there’s just a call back and again it’s that same list type and the same data type and really that’s all it is that’ll download it and then you have a success method that’s called cout folding me here not a scroll over we had a success message thread and a failure one and what’s nice about this is this comes back on the UI thread you don’t have to worry about threading it’ll do everything in the background you not to worry about that and if you have a failure you can just handle it here it’ll actually give you the errors and you can parse it and do something specific if you like in our case I don’t really handle the failure on the success base that is have a nice method called to API data and it just magically takes the data makes it render so kind of hit someone complexity there you never have to consider parsing or anything if you get the consumer yet it is called with the object great you’ve got everything you don’t do the rest of the stuff to do if it’s called and all you have to do whatever you have to do in the interface that’s to do so then to kind of show what it looks like unless it crashed and of course it crashed on me yes you going to go in further my laptop crash while was sitting close for presentation yep you let that boot no I here’s okay I have a good reason here there’s good reasons because reasons the launch number D he lost my train of thought so I never used the emulator for any reason other than demos because I find it generally worthless and I have a wealth of phones at my disposal which is very nice the genymotion yet is the best thing in that world but I still recommend it’s never using any simulator ever ball emulator ever simulator to be a starch improvement yeah oh he’s got it nice and then we hit the bottom of all audio

second irritating this it’s kind of like a juice in application and it’s Tomorrowland food committee right is an opportunity so it’s really really hot in the past you would spend a month to state the fact engine working and now I mean you could almost do this and go find this super specific reason and the great thing about it too is so with that little bit of code so we were we’re writing totally tonal and the race was all Renner ated and now we have all the advantages we have download caching we have gzip we have you know all that sort of stuff all to retry and back off retry I’m going to right any of it’s very robust so we’re going to transition into a second part so some of the that’s that’s good to get up and go it quickly and get your moving and now you’ve got an API parser now one of the things with JSON is at the very beginning everything that JSON is is going to be strings and integers so if you want objects or strings and numbers so what if you want something that’s a little bit more robust as far as objects go like maybe some of the problems you might run into would be my API requires a 1 or a 0 for true and false instead of true and false because that’s how the API was designed and that’s what we have to work with so or maybe you don’t have access to the API developers maybe you’re using Justin streams or whatever that’s which TVs endpoint and they’re like no and you can use it and not your dream and then in your code do all this goofy visor it’s a lot cleaner if you can just convert it at the interface layer and not have to deal with whatever straighting presentation area right you can either you can either always have to write code that says ok one I know means true and I’ll just keep doing that or you can write the better thing to do would be to write a method on your po Joe that says okay give me the boolean value of that as opposed to the int value but why even bother with that one of the things that one of the really neat things that retrofit lets you do is you can if you don’t like the way that something works you can adjust the way that it works so what I’m talking about right now are actually converters and give me one second to find the correct file here so API client this is the same API client that Bill just finished building but the ref adapter has some extra stuff in there so for in the converter well this is the this is the API client that Bill built and then I’ve added a converter and a request interceptor what the converter does is that lets me say that whenever I get a out date that’s a perfect example of things that are fun to parse so dates are going to come back as strings you can’t get a date object from JSON it’s going to give you a string or an in maybe are along yeah maybe it’s seconds or milliseconds since epoch maybe it’s the string mo and space blah blah blah but in this case it’s actually the latter it’s the day of the week the month all that fun so if you want to say for all dates let me let me get myself involved there let me jump in and build an actual date class so that I don’t have to get that string go get my date parser build it into a date then deal with it I just always want to treat them as dates the change I made was I registered you create a builder that is going to be responsible for building your JSON you register the type and the D serial out the converter so you can convert either coming from the API or going to the API or both you can have one class that does both directions or one or the other and you create that you build all your classes that you want to interject so here I’ve commented out a string reversing to serialize ER because why not I mean so you get strings from the API spin them the other way just for fun so I can show that if anybody’s interested but I have that commented out because it really messes up the URLs and it’s really hard to get images with backward strings basically you build the converter you stuff that into the adapter and then from that on anytime a date object is expected so how do you tie those together so I’ve notice got the date class in the game class here we’ll come back to game but date is pretty simple date is good old Java date

so really and the way that I let the API know that I want to deserialize that to date is I go into Justin stream TV data and I change my uptime which happens to be a string into a date object and then whenever ji-sun sees that date object and it’s trying to jam whatever it’s got for the value for that uptime key into the object it’s going to call my deserialize err so let me jump into the deserialize err it’s almost readable so you create a class that implements JSON to serialize ER and takes that object that you want to deserialize here i’ve created a simple date format there not thread safe so I synchronize around that so that we don’t get weird redness and weird dates that’s happened before that’s always fun this is my so here’s I copied the text that the date would give me from the API and then I built the date formatter to read that into a date object when what happens is when jisan goes to convert to build that POJO for you and it sees this key it goes gets an instance of this and gives it the JSON element and asks it for the date back so you have full control over what you do when the API sees okay here’s a date you deal with it give me a date or here’s that key that you want to meet a handle no it says that key that you wanted me to handle is a date so give me the date here’s the JSON element and all I’m doing here is grab the string create my data object parse it and pass that back so it’s nothing fancy Java wise but I never have to know never anywhere again do I have to touch that date logic I can just say as long as the API is sending me dates like this from now on just give me back dates when I build my object I can say this is a date this is a date this is a date and I get date objects I never have to do date parsing myself it’s all handled by the AP so if we take it a step further something that might be interesting is if you have an enumerator or an enum rather that you need to deal with so you know that you can only have a set number of values for something and anything else that doesn’t fit in those values you want to show up as unknown or junk or whatever you create a Java enum so game is another one of those objects that I said listen whenever you see a game object let me handle the deserialization I’ll give you back the game thanks geez on so these are my these are just objects that I found in that JSON snippet and what did the enum itself converts from a string into any number you this is just simple string matching just for proof of concept and then get description prints out the text as based on that enum that it got back so again this is really simple JSON is that object from the parser we get the string out of that and we call we ask the game up or the game enum to parse that into a game enum for us we get that back and we pass that back so my gaming um is in charge of handling what strings it turns into what objects and the only thing I have to worry about is that now I’ve got a game object and it’s gonna be one of those UNAM values so I can go on with my life as a developer the nice thing is it’s binding it to every interface behind you don’t apply to each single one of them yep so if you have multiple endpoints and you get this game object back you can build your objects out to represent the values returned from the endpoint and you just reference game and anytime your reference game it’ll call into this method to do that animation from the string so this is where you troll your well if you wanted to yeah if you wanted to introduce you could introduce randomness so like why is this you know I’m coming back this way sometimes in the other way at other times that’s if you were looking to have things thrown at you and Nerf darts accelerated at your head at a rapid fashion now so the twitchtv client that we did didn’t had nothing to do with authentication it was just anonymous you open api whatever what if you have to do with an occasion so one of the things that you have to do with authentication usually is you have to take some information and stuff it into the header so that the api can identify you as who you say you are and know that you’re allowed to have this data are you allowed to access this particular bit of information how do we

do that the not correct way would be to write methods that take the take the request get it out of the request builder mangle it just the way you want and then send it off you don’t have to do any of that thankfully it’s the in the API client you can set a request interceptor and that request interceptor can handle all that so what will happen is you set this as the request interceptor for your API client and then any time an API call goes out this it has the opportunity to be intercepted it will be intercepted you can selectively choose to or not to intercept it here if you want in this case we’re always intercepting it and if we have a cookie we’re going to stuff that in the header and we’re always going to set a user agent key just so that whoever we’re calling can have this pieces of information and you can take whatever you want and put those in there as well so if you have if you can imagine if you wanted to do something where you have to authenticate and then prove that you’re that authenticated user you may be you get a token back and you always send back that token in the header so what you do is you you go into API client I’m not going to show this I’m just going to talk through it so here’s our twitch TV API interface you would have a login method that would give you back a token and then a do something authenticated method that does something authenticated but if you call that without logging in first you wouldn’t have the token it would fail you get the error that not authorized you know how do I fix this what you do is you could wrap one way that we’ve done this you wrap the interface you provide methods that interact with the converter so that what happens is when the login method returns that is put into the where is it call it oh I’m missing it via screen response than just wrapper it and whatever comes back if at all he came back just suck it out and save it all in your classroom you put that into the private request interceptor so nobody else has access to that outside of the API client class but then now all of your requests will be have that token appender added to the header so that they’ll appear authenticated and you won’t get the unauthorized exception one of the nice tricks on this that I like to is you can go down to the interface you can actually just wrapper so what we do is we just take the callback class and we just create a face callback that has everything pass through that it just keeps a reference to the real callback and allows you basically intercept all responses and what I do there’s things like look for own authorizers and if you get it to just throw up in with it and handle it that way so that what you’re talkin can catch things like Oh an authorised and Commissioner offline or that sort of stuff and one place rather than yeah one place round twenty you don’t and problem this so it’s really what saw like that this is you can add the bass ketball for yourself using that using a request interceptor yeah this is that’s a very time okay HDPE will do the normal ones may go back with good password so now I hope you know Veronica the first one you probably could but I tried yes most areas yeah that’s be joyful society so that’s sorted a little more of the that stuff and so using this we move here use this in two different applications use this whole stack basically and really the problem and I’m running into so this is great but it says this problem if you don’t Android where you will be on a screen gallery and I rotate the screen wound and that just kills a little screen ago not works kill them now you have this down door muttering often weeds whose response just Falls before and you probably have to be rotated say oh I don’t know yet didn’t know downloading superfluous than throwing out results so one of the things we have to do is figure out how to British here’s McCandless and we talked to different proaches on two different applications mostly just because of architectural considerations at that point so the one approach that I used was I’m a basically ever question editor class and what that did is essentially instead of going straight to here to get your view request if they all went to the Refresh manager class it would wrapper the call back right so listens for all callbacks and when you call him he gives you your Lee pop and that UID is your to hold on to essentially say I’m waiting for this request and so what would happen is when that request

finishes asynchronously if that UID still found if you still exist essentially as a listener it just sent it out such as pub/sub so now you have okay well what about rotational what happens so what you end up doing is when the screen rotates you’ve detached from your listener this class is still living it’s a single ten so it’s still down in the background and when you finish your rotation you still have any new ideas you shave it off you reattach the manager and stay alive he either delivers the results immediately it’s our finished or wait sport to finish there deliberately so you get kind of needed way to handle replication and what we did was we actually used async task loader z’ which do the same they do a very similar thing so that’s just another class that lives off outside of the scope of the activity and you can rotate all you want but this class is around it calls the download it talks to the API client and it doesn’t care how many times you rotate when it gets its data it’s there and you ask it for its data when you finish your rotation and you’ve got everything rebuilt and you’re ready to actually show the user something you just go ahead got any data for me and it gives you the data and you do what you want with that data so you can rotate all you want it’s it I mean you run the risk of losing of spending Network calls to actually download information but at the same time the only way that you would do that I think is if you backed out of the activity before it returned so it’s not much the users given up at that point I mean you’ve already made the call you might get data back that you don’t need but there’s not much you can do there’s different trade-offs each the trade-off on on my approach is you have to be very certain to attach a list or rotate Russian oblique the whole activity which is very bad and very easy to do so you could ruin a do not approach you want to fake that attach key cashed up in your base classes though chance let’s get out but yeah good so yeah I think that’s like both side I think that’s it if you have any questions yep you’re in the front we weren’t doing any caching on our project then you’ll get basic HTTP caching and really not yeah there’s other things you could go suppose so you could please yeah and you could you could conceptually do that too you could take a service and just call the API client take the data jam it into this equal light database and then update from there and you can put a content provider in front of it school yeah you know and it puts some sort of expiration on and do a custom kind of thing we should have a nice single point of entry you could even conceptually make or override well I guess implement callback in such a way implement a base callback that will write to sequel and one that won’t and then it’s up to you whether you want to save the data or you just want to let it go after you’re done with it so when you pass that callback the callback is just an interface you can pass an implementation at the callback that saves it or you can pass an implementation of the callback that doesn’t depending on whether you care about it or not so if you want to save some of your information but not like maybe you don’t maybe your downline knew a bunch of images in your app and you don’t want to save those in your secret light database but you want to save all the all the information that those images belong to and then redownload the images each time you could save the image URL and not always the image so the image download well that’s you could use a different library to do the image downloading giant glossing over is happening but if you wanted to you could do that all through a retrofit theoretically I wrote that work we glossed over the next I think this actually really briefly because you could just put a manager class on the top right and on the way in intercept on request to see if I have my database don’t wanna make requests return simply extinct related right and then when it comes out to intercept that don’t fit in your bathing suit so I think next work really well for that sort of a second-tier cache there there’s a request interceptor you could also if you wanted to test heavy network traffic you could you create a request interceptor that just took your requests held on to them and sat there 30 seconds later okay go ahead and take this one I use that for debugging but I have special Android you always want to test this rotation it doesn’t know downloading does that work Bradley so I get this notice have a little debug menus with the app right say make every API request 8:20 sides you know and since it’s all gone to one place now you guys I backed away well really really helpful and you can also what I did too is made it so I can just say arbitrary irk goes just again through the app isn’t like a developer settings page so I could be lying screaming and just inject it and you’re from an Android you could just that Senate intent from your command and just

inject it and or so your very next request is her which is really helpful for testing specific things on specific experience right when I’ve downloaded three pages of the TV stream and then I want to go download the fourth what I want that to give me an authorization error or whatever and just okay send that intent then you scroll to initiate the fourth page download and see what happens I think that was a big benefit isolating those two was handling authors and minors of Oaxaca it’s really visible which is the latest ever has been before are there any specific patches well it’s just an interface like it’s an interface so you can implement the interface yourself generally their stuff is really easy to to unit test just because I mean this is just a straight-up 8/3 straight up interface stranger and you could this up here in the API client above the fold you could act like you see it tells the API client the Builder to actually give me one of those but you can implement a test one of these that does whatever you need it to or yep because it’s just a straight interface there’s no behavior you mock it you tell it I mean some of the mocking libraries even let you only partially implement something by Maki like you tell it give me one of these interfaces I don’t care about the behavior of the 90 other methods I want to test this one method when this method gets called then give me this object back but I don’t want to implement the rest of the junk that I don’t need to test in the specific case and this would be so this is a simple one method interface but like once you have an actual back-end that you’re supporting it’s a lot larger but that would be a great use case for that is they love em or mockito over okay yep do recommend any tools for Andrew for API is not there’s nothing let’s turn tricks to it like it nominal tools I use is going to chrome plug-in that all to JSON a five things pretty for you which is nice which I think you saw was browsing through on Eclipse heads really in sort of if London’s Mexican comics minecraft is playable in town so it will basically mock but but that’s absurd by intercepting requests and there’s no it’s not intercept in speedruns requests at the operators that’s a fear that’s if you’re using an emulator testing Charles and just it’s like little holy request yeah Charles is like you can share a certain research for enable through looks like 30 bucks I think for the whole time you can watch our trackers as a lot of additional let’s do this when I use all these Charles proxy and not only the mana village of yes and just liable just truly officially so you’re trying to voted that well silo that’s real cool they go all right thank you yeah thanks for coming