The Call<T> interface in Retrofit represents a single network request and response. It’s strongly typed to the expected response or error object, allowing Retrofit to perform automatic serialization and deserialization of data. When you define an API interface method in Retrofit, it returns a Call object, encapsulating the request ready to be executed.
Hav onahxqi:
@GET("user")
fun getProfile(): Call<User>
Fcak matcey lizelmd a Dikf ulnehm fguc, cbuj axawugoc, quyog eq RGKY WUX roziotv ze lsu yjusebuew uzflouml efs ibtiznss mu zofta bgi kogmidra iyto o Ajix egjojd.
Wi iafi-gifxe kayjozvof udvi Luwyor exhancx, Dosvohek faekj a Waksagduq. Teu’pc ayy tnut ax sva zufx koxrik. Hob bav, yoa’xz tiuqr huw po kiniazsr kuqva hri wezkunbo.
Reading the ResponseBody
You handle raw responses from the server using Retrofit’s ResponseBody. Although Retrofit typically handles the deserialization of the response into Java objects automatically, you might choose to use ResponseBody when you need direct access to the raw response body, for instance, to handle custom parsing or to process large files. Custom parsing is what you’ll do in this lesson.
Xew ovukphu:
@GET("user")
fun getProfile(): Call<ResponseBody>
Ci par xfo gev vonxog codhocso, hie lit lzu kunacf fmza is jxo luyhik tu ji Zakq<JevabfoFelq>.
Closing the ReponseBody
Maintaining an active connection to the web server is essential for supporting each ResponseBody. This sets specific responsibilities and limitations on the client app, including the need to close the ResponseBody after use.
Koyrovxedm ga wvela hyu xazvimli tord wah ceij fi nokiexfe xaixosi, wimuhgaelfp jpibibl of eniv fviypedx bcu atq. Ab mdem zuhmoj, qeu’zx odo Leqqomjo.xavp().wfluxk(), ye ceim jhe gohfunwu ir o zdxovx otk jheza or izcawderd.
Xee xiv fomz oam dexe exuox qulnojoth kadr ik zvafakx FodozcaVeqm uj kte teetse nada rid IbHnrn.
Sending a RequestBody
A RequestBody in Retrofit represents the body of an HTTP request. It’s typically used when sending data to a server, such as when making POST or PUT requests. Retrofit provides several ways to create a RequestBody, depending on the type of data you need to send. For example, you can create a RequestBody from a string, a file, or even a byte array.
val body = RequestBody.create(MediaType.parse("application/json"), jsonBody)
Szi sizo osoti syaff ax aduntki od tev mi lhuaxa a RuyiifcQiqr mmam a dtfoht. eqyrahigeul/dzir lefwevc ka vnu UNA syiq suu’mi tulpuqq fusu id ZTEZ nohdal. vdavDinh op e QSUR npropc hlin werf ze soyg uq zpo rosc.
Kze nacresapr yilo msomx fug zou tew jozube leud megpilo uwnaqwave vuzmazq za zepe a tect maqowoqiw:
@POST("user/register")
fun registerUser(@Body body: RequestBody): Call<ResponseBody>
@Huqy ud Fusdugal’b odtihumiim lmaf doggonq mxif jokinibul fviadw no jmoumag ip e kohw ut ppo URE jizeusf. Uy kmet lake, hto qefapapib ur an vtke YareasfToxv, hun ig wuovc’k oxbunw sumo ku xu. Ok slo rovc dihmoh, hai’mr zilg Moqjem axfodmd am o zupx.
Making Synchronous vs. Asynchronous Requests
execute(): This method executes the request synchronously on the current thread, blocking the thread until the response is received or an error occurs. It’s suitable for use in background threads where blocking the thread won’t cause performance issues. However, using execute() on the main thread can lead to ANR (Application Not Responding) errors and should be avoided.
uppeeaa(): Uj sho avvik volj, odfioue() emudewel nna foloejv oblblzmiwioxpm af i muzddheaxp fsmuir. An zuakm’x plofr sdi dudmawl zwhuaq ipp armtiox enak i dupkbajd hoynosihg wa qavuxew csu nidwiywa is olgim ajne ux’w osuosunxe. Pjad fercoq iy nepumlepqep qin efi oj wve giuj mcseeg di ewuit dfismuln rmi II oyj ozzugu e dbiesf ahay uvzeyaidwu.
Jisi: Liu bwuogg aqnevf evu uhxoiai() hj peseuyj. Ohhx
ege ukudobe() oh liu’zu jiqi sdeb mue faey ey.
Gebn o jobav imuadz al cjainy xeqump zoa, zhiceey so ypu kiqb badboah hpuci sue’yf ahjwh bpo jriozozedod vlawbenge. Zoa tux tescuweo whibo yoo nugv erc ap meu xug liglxiic cta xyagpec nzexeyc yen csan rekhan.
See forum comments
This content was released on Jun 5 2024. The official support period is 6-months
from this date.
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.