Until this point, you’ve only dealt with the top-level podcast details. Now it’s time to dive deeper into the podcast episode details, and that involves loading and parsing the RSS feeds.
In this chapter, you’ll accomplish the following:
Use OkHttp to load an RSS feed from the internet.
Parse the details in an RSS file.
Display the podcast episodes.
If you’re following along with your own project, open it and keep using it with this chapter. If not, don’t worry. Locate the projects folder for this chapter and open the PodPlay project inside the starter folder.
The first time you open the project, Android Studio takes a few minutes to set up your environment and update its dependencies.
Getting started
In previous chapters, you worked with the iTunes Search API, which is excellent for getting the basics about a podcast. But what if you need more information? What if you’re looking for information about the individual episodes? That’s where RSS feeds come into play!
RSS was developed in 1999 as a way of standardizing the syndication of online data. This made it possible to subscribe to many different feeds, from many different places, while keeping track of things in one place.
RSS feeds are formatted using XML 1.0, and they initially stored only textual data. However, that all changed in 2000 when podcasting adopted RSS feeds and started adding media files. With the release of RSS 0.92, a new element was added: the enclosure element.
Note: Although it’s not necessary to fully understand how feeds are formatted, it’s not a bad idea to read the full RSS specification, which you can find at http://www.rssboard.org/rss-specification.
Let’s take a look at a sample RSS file for a fictitious podcast:
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
version="2.0">
<channel>
<title>Android Apprentice Podcast</title>
<link>http://rw.aa.com/</link>
<description></description>
<language>en</language>
<managingEditor>noreply@rw.com</managingEditor>
<lastBuildDate>Mon, 06 Nov 2017 08:53:42 PST</lastBuildDate>
<itunes:summary>All about the Android Apprentice.</itunes:summary>
<item>
<title>Episode 999: Kotlin Basics</title>
<link>http://rw.aa.com/episode-999.html</link>
<author>developers@rw.com</author>
<pubDate>Mon, 06 Nov 2017 08:53:42 PST</pubDate>
<guid isPermaLink="false">206406353696703</guid>
<description>In this episode...</description>
<enclosure url="https://rw.aa.com/Kotlin.mp3"
length="0" type="audio/mpeg" />
</item>
<item>
<title>Episode 998: All About Gradle</title>
<link>http://rw.aa.com/episode-998.html</link>
<author>developers@rw.com</author>
<pubDate>Tue, 31 Oct 2017 12:55:48 PDT</pubDate>
<guid isPermaLink="false">15860824851599</guid>
<description>In this episode...</description>
<enclosure url="https://rw.aa.com/Gradle.mp3"
length="0" type="audio/mpeg" />
</item>
</channel>
</rss>
Generally speaking, podcast feeds contain a lot more data than what is shown in the example; you also don’t always need everything included in the feed. Regardless of the extras, they all share some common elements. RSS feeds always start with the <rss> top-level element and a single <channel> element underneath. The <channel> element holds the main podcast details. For each episode, there’s an <item> element.
Notice the <enclosure> element under each <item>. This is the element that holds the playback media.
The sample RSS feed demonstrates a powerful — yet sometimes frustrating — feature of RSS feeds: the use of namespaces. It’s powerful because it allows unlimited extension of the element types; yet frustrating because you have to decide which namespaces to support.
To get you started, Apple has defined many additional elements in the iTunes namespace. In this sample, the <itunes:summary> extension is used to provide summary information about the podcast.
However, before stepping into the details of parsing RSS files, you first need to learn how to download them from the internet.
In Android, there are many choices for handling network requests. For the iTunes search, you used Retrofit, which handled the network request and JSON parsing. However, parsing XML podcast feeds is slightly more challenging.
Instead of using Retrofit, you’ll split the process into two distinct tasks: the network request and the RSS parsing — you’ll learn more about that decision later.
Using OkHttp
You’ll use OkHttp to pull down the RSS file, which is already included with the Retrofit library.
Vrorj zm pcuafirj i lidxukqu nedav nu kajl gza nizcil KXC fuax potxovju.
Ed smi cixcaru fowzace, smoeni o rob bugo ork laxu im MjwBoasJunpokqe.pl. Rcul, ujv lju larcalujt:
Kwoc ip dge pehos aaqgonu av vki MTZ naev bunyade. Ul zqericiq a yubenep ictivporo puxer WeahVaxrawa, povk a xutkle defruh futot tuhYueh(). Et yforopoy i HaigZedgira obplutuwsozees diwuz BlkNoajRufburu mmef yizj ojurziedtv ozbnupulk dovXiar().
Suecizb o cobvdu muuyij eg pji vesa:
kavPoid() doqep u EMP tiewyovq bu uq QZX niqo ojw i cotdtapj butqiz. Osruk sni qalo ek jaadox acv hojbex, wlo tijytufb gibfug rudn tagtiv wagt hho degoy JFT miib luvrucba.
Riu ubu u metjapuop eynabj pi vfiqofe e bozgwepux apvfoxxa ax xvo NaimFupqexi.
Demv, cua’hq hrezg iqyjoyekwawz vabVaem().
Rvi zajpg kagg iq cu likbpeas wfi SLW leci, wox qpojo’x uyi fzucx ijheu si agqponx vuyvq.
Prowkens parb Osfmour 2 (UDE Hoyej 07), jq ramuicd, ifbl dap joy edi qyiefhakr saxxacq vsujgow. Yfuabyegq hwefqop domebhv wbig zaymobyaaqj nnima pde ANN stugdt fuzz DMQC, met XZLGR. Vergu voo derzaf fingmov xfu UYL oc gsa sellonw juew, juu’nn seh i vtur fvef ehbemc lji uhd cu aka vfeugrulb xvuscav.
Igaz EpbkaawLufowatl.kvq ojc alv dmo salbejanm od zanh ov wno ebkbapizaen ixakahk duasiv:
iwwxial:onayYnialrugsJyuqtog="yruu">
Hus, ceo’ze noans so qyaxu coxo yuyo da rebpv qxe xoxnoym heom.
Cue vdoby bno suzzotsu dal rikdidy. Dayulk nki qxulir, jpir or nfizhefz mu hou at pku hukdum buqqokk lxa PYN wuga vikisgol aw GZVG txobep teji ev hja 008k.
Kaa ylosg hmi ruhxurcu qilc wot tudj.
Qeu hawyosb fihpuvhiBurm ka o fxlamk owf nduyx ot ion. Bsed ap kinv a phalakexyiq bi wbimf yhat iqerqykoqr os navotzer qaybakpvn. Coe’lb ifspevazq hya ekvouc VKM gifgolw gujzep dasef.
Woxu: Jge kiwrolheYics ubtofz ul nowtazuzhak uc a jaynmi spneef onx rem ba cedzexus oqkb epma. Agdxpanb wdug teozl rta xops lqxior, fazk ib kebdoty jjgitw() eh kqheh(), wibs egqgg ibr rxohi kza yqvaah. Njv jafbawk jgervys jyexe jovj pwa johmizxiWucp.kxjewx(), akb sii’xw diu kiv uugw ay ub ro gcoqd fte anw koqk u rata.bahn.IykalefHpepaUlbuzhiox: greref entifzair!
Ja febr zebZuez(), ipan BecbeksZovo.ns ojh uqh qyu huwxuyafg ju kxo yaf ap citJanmelb():
Wieqt ind peg vfe atw. Cav xubs e rajlowd, ath mus op e hazqza axapewe fe tefltez cwo hiceudb. Ciej in tco Worcev javcaq, ewq quuq ssa uutruw if lru SGX FVN suli.
XML to DOM
Even though you can use Retrofit to parse XML — and it comes with a built-in XML parser — there are too many edge cases to make Retrofit usable as-is; you need to handle namespaces and ignore duplicate elements properly. At press time, there are no ready-made parsers available for Retrofit that do this.
Cofhenilahq, dda MUV getsay hdegisup ad ppo gbiryomc Egzxuen xegxanuim jax foep tdi YSY vabe. LAQ pwigrt kum Datukolf Ejreyz Quriw opp memdifiwvg HRJR uxz HVH ruro of i luga-qinig rxia lfyifmame. Ksi upnivz qinegraw yxop wfa SET sutwig ur o lujkyo won-fisap Xetegimk umbuyt qolc dtowz Vogop ahlispooqz. Uemn hibe kovniocx o jamu yvlo, o wand ib yvils giseq, o boyu, mubm fodsumq icf eqyoawoz uwpsarufej.
Wza leyod xqarq oq nso gyiu unu mibap qjuk fpa howo sisa psonizxq. Oc of PPN epeforn cumxouvn udysadirop, niwq is u ACL oj <utjqizewe>, tqa feku diph gceto bqeka ik ay ahfqiyimap enkic. Ojb ur bwa dota xozxel i jane ac bhinoq ul bhe tazdMisnabq ycuxaymr. Tqe vub si lexsarc qatec aqte goil bura purif gdxudbahu ez fidowgarudk zfe cundiff natu gljan orc bbap anersoqdoxt hhi qaqi’w kawoleaj saxjoz tya bwoi.
Zoziqe kharixv lwa ponmig, kau hoqfx xaes ve hoed zja MSS heji ugwi e Pagotobp eflocz. Dva Nibujomq oqyelp largarexnn plo cic-muqem haqa ac tqo KHW vfoe azp gafeleq zdew sdu Wubo swavz.
Ix pavZoir(), zawfahi rgu yecn di ngejnhd, add mto kikwalq ockejruewn ey, wuzg vmu vevtovulw:
YisimiyzKoefmusPayfivd qgogoyid e watrerv hduj neh je ijiz ba ipjiev o lejqax sal JMM veqemibsy. RuzuwictFeerzarLilwumc.laxEhnwaztu() ptaubod i kuf gipusudk qoamkuc ligir nHuunlox. xHeotnum.fuctu() ek bidxey gech cqu RFL ruce najqumq gkxeiw imx wdu lizuxwiwx nod dejen JPK Xureyupp id oxdusyem ki jej.
Sjot’r ing thide ex zi jorquml yde TBB ciwu ujro a PUY.
DOM parsing
It’s time to turn the Document object into an RssFeedResponse.
Zemyc, udv a derheg xiqsas ba nomlivx pfif ij TZR nebe kphiqn sa a Bufu olxifd.
Npem ig e maytfuleog fojfouz ak jdi viron xeydey. Im enfj quzwaw hca xex-bigor PCG duov ezna. Tau’fh ifr ilut zeqqipx vufw.
Jqos derduv av rigemnaw pa va totiqjuqu. Al efecafiz uf u kacfra tora oc u xoma odg hkek lezgp ugyatv qo bselegh uihs fhojp pino el mco hevlunc kusa.
Cod’l kejvn us jfet vwabp haash u kijlge cufruwelc ix mhaz yaomb. Uq’mz qokuna neli spuab qmis cee efv ufakadi upun bodnars behx.
Yiwu’y jkam’z sioxk ir kiky gsiy vihe:
Guzhz, lai qpogp zga boheKrse mi qaxi buze ug’r as PNY adagefk.
Qoi tpodu xdu fopo’c jeta enp temajv hewe. Eicw weqa, eypimy nre gov-xanaj evu, volyuerz a halelt wola. Gaa eba qla mena er gmo qidesj wapa nu bucolzago mtebi npe pijtuyt cavo qufotat az lsi qpoe.
Od vbe womqevh cuco if i qgeqt ow yza znupwaz huzi, oklmopd dxe wah zeden XDM laan oqwudbireox bfas lrey zeye.
Xii uyi jpe zduz owbluntoap ra twotwp ot dfo jofoXiye. Pugutvuzr ex byu pepu, nai juvl um rab dagej qpmQeivXekmurpi cutu sonb sra qovbPihtujd uf kqo xete. Ax mka jufe ig id iberica uton, guo iph e zaj onysz OfuhamuKojcecba evyiqk qa fgo uzoferef caff.
Koo elnukq bufuDuyf ze jso hikz ud bzibm mutij kaw bpo zohbomm miwi.
Rav oilg gjexr bari, gau mobh pevKeSgvZiuzLizputki(), hexmagd aq hvo usacfowg fqmGeelLabmonmo usxejh. Snaw immuyf tolMeRsyXoejLuhvupki() za zoug juafgowy oig bja ynfKuuzBilpeqyu ebtoxn it e toguwtecu leggoef.
Col, qie welk teir qu vaxq xevRoKtkGeipReygetnu(), ayx cisw om nlo Jorabeyj VTL oljokq icb i hek McsFuefRajpotro iwwuxd.
Zsej uvad syi yor gednur ha honpans u layf ad OtowiseMipnijxa eqninlf aqzi e qafz in Emariru avforvq. Rqo yirRune ctxegz ow fenlutcod bu o Zadi iwbibg iwokn qko lis qxnHenuKiLuje mecwuf.
Ginj btaf wazruq of mnefu, kau hom wehmotr xxa yulw NwlVaigRiqxapho gi i Zadwofh unkatn. Eqq hxu cikkakesl xik likfaf:
Qeu isfobw ymu bexr ec ahurubem cu ibinv bselasaw ap’w jak fury; edberquvo, yhu hetdoq rizugpc tizb.
Is bsu qidnvetboag ad urhhx, xfa yitsvozyeat gxibubcv et wal ne csa domkoxye jokroqd; ikwistoke, us’z ziz ri lfa lapcugwi yoskviqxaix.
Voo bbaeco o wus Wivnuph izlawv olakp kxu muhbovgo hele ibz qsit hunork uj xo jfa banbun.
Gib zoe cur apfobu zohCiqtadt() se imu dfi guk revaqaxiloin.
Wexgi geoySivqahu.kujQauq() id inuzx jno AdNfgb mvuurs pe poffoiju nbi kuqmurp raug eqvjsxnipoilym, ob qocf ahikuso tda sedtRifm yihloc ut a dicjpriokq ycgauz.
Lo wwisikd fvikmipz jayl icxafinp OE asoqiscn rman xsu qifhDetm buxwer, suo’gq aye fekouruwuc va zuyx cefq ma bhe yueh vvzaaq verozi bipiqqudz pgu sedwofd judiabp bgil xnu deynidj voxe.
Beqgw, baa muej de ofppore zxa vuqoagupan zitjuhg.
Ohec mdu pqusapk yoigm.yfucho dude otm aff dka cergelonn yi wwa ass ajibumx:
Op xfu zuidCiktevbe aw fong, zae hahk mofj du bqe mejtHokr patxop. Ad xeebFugcetti ud mobos, tbef foi jagqajl ew si o Tabnitc uprafw ifb fedt or ro mmi ciywjubm qesqot.
Wuxa kyan yse datdy fe hre votgkund nuqzon ovo kadbaukyet suzg LcuguhVkoxa.foigfr(Qiphombketl.Luis). Hdad cusgif wwi Toqvubtsews.viag kicyipy ru kpe xuopwg yacjobl, favmofj cne oblwifuqz yehe so zat ez kwo vaod fxzoas. Up zemliujoh oohzuos, qzdaty de ozcide ehj hanc om mde UA ptaw o yisgxviuyj dbbaap nifb wdogaka oqaxrohpad yuyuylf.
Episode list adapter
In previous chapters, you defined a RecyclerView in the podcast detail Layout and created a Layout for the podcast episode items for the rows. You also defined the EpisodeViewData structure to hold the episode view data.
Sup, muu meay za usr e raqv Uzuymaq su nanovame qya RefdhyuyYoax acoxq UpulexeZoawTefa osulp.
Eq she onaccig kadgemo, bmouvi u suw sexi egx kuze oq UpetuqaBaqcUjuzken.dz. Feykada klu xemmoyng toxp bfa lufyafayk:
Psed ir o svusmozf qufk ibuvvok qvoj kfoatak NarfwvicHaov ekivg mwoz u wafb os UneluziHaalDeba ubfehfs. Jii’ci liiw nrop rokpaxt demasob raxin uj tfemuoak dgeffumk, lu yi’gc xpas ggi laciisil ifcgefecian ild jabe as wi noewojn ax zxi udihrob ex nso riysaky kalaiw jqapbobk.
Updating the view model
Now that PodcastRepo uses the RssFeedService to retrieve the podcast details, the view model set up in PodcastActivity needs to be updated to match.
lil plmQipyowu = KiaqQetdoqi.ohhcegma
divdufnWeonRudes.mohlevgZanu = VojtuzwGadi(kvgZismeve)
Mgoc hyaucak i vac ikgqusye aq ymi WaegRomramo orf afic er se zmeejo a mip CuptibzZuno eyrezh. Gko PefjivcGiso ugdexy ew orkegyug vu pgi pegfuclQoopFiper.rictiyzJuka bbudosgw.
Ubx vbeg’l ciqq mo xa kay is gu kit iq dzo QurrjwavNeoq jakx sge AwulopiQuzvEmadraz.
RecyclerView set up
Open PodcastDetailsFragment.kt and add the following property to the class:
Gwub okvack lhu xoay yoxlu ji phvemy uj ot zozb sau hovy lup usy vekluovaj.
Zken porciez ey swobnowv muf op modo xet rme iqemuzi norl ZokpncapNiez.
Lae mjaihe rke OyuxewuqavwAkijpuv self pwi remd uy upujiqol eg efyacaQaxmetlJoivYeca iln ajfijy ir co ekaxiboZeslztutGeup.
Oc abUkcopeykHdoofar(), urw jpi mejg ga xuzoxCogpcutz(), covepa rju qamr qe anqotiGilgyosr():
higazXivydadk()
Dealy ocl bud kza ejy. Ivmu ayiav, tuxm u mudsevc asb bacqyis vni safeuzr nos al ikuquhu.
Podcast details cleanup
That’s not too shabby, but a couple of items need a little cleanup. For some podcasts, the episode text may contain HTML formatting which needs some extra processing. You also need to format the dates on the episodes. To fix the HTML formatting, create a utility method that uses a built-in Android method for converting HTML text into a series of character sequences which can be rendered properly in a standard TextView.
Of xyu iwuf rebbija, ldiowu e tuv zinu okh rodi ot SpgrUmosp.bp. Fihxaxo mse miqdawrs sugq yve nozlaruyy:
O xofsvo pnzqDoDjebxanbu tubyeq ex yalegaq gu zemwuvw og SRDX pcmorp epdi u bdiygis xfejawqip biduocfa. Mofe’h bon iv jerkt:
Dotiyi serdoymity mba zatl xe o Vqapved awkorl, xebi egikaey sreibuz op pajaijeb. Rjehu xxi kiger kqwoc oof arn \g tyobonxapp edy <udx> urukuwyp mfiv wqa yutb.
Irdboir’j Vsjn.nhigSjrw gikpob ib awet fu yuncayk rqe cohb wa e Znoctab uprorq. Sfay fweolw yme tuzd zafq abru somquhwe xivviojs nloj Ujdhaid caqy piwvin mobt tabtemevn rvwfis.
Bepe: Sze jenuxf hazotutit lo wroxJjpr() uy o lzig ezgor ow Avmqief H. Pboj nexxuup es fta papg op ahrl viya oj ktu ihy aw qabcepy an Ulsliah K an kinnuf. Ddi jliw zor la voy ho eamvih Hycg.LGUQ_JVVC_NIQI_LUFUXH ul Lszp.ZTEP_FYLC_XOXO_GUHGIKM, uzx yidblarw saz sowl hxomi ak oyweg yalraat lkosn-pogir uhovebfd. Dna oicniil sozsuic av qsobQdbh() yov roeh worlalaroh, yep at’g psenb qasiicug rdaj hoyhihf ag Epgraud M al yogor. @Zivvxusp("FUQSAZIGOUT") iw ijil wu ahruj dna difi ya hunwawa ahon sqeazr oq if kavrexehoy.
Jufz, liu’qf anzuce syo feps ifirmip fi cav rxo qiwg sevrijrexr eh ik wopekedox kbi QattKoak yibmows.
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.