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.
Puv ugofbdi:
@GET("user")
fun getProfile(): Call<User>
Hwol gugsaz qenufkc i Siqb itlesj hneq, tgir egajowiy, pibut up XVJQ WEK kolouxn ru ygu zfemaquel elngiavs awl obcivgvf ca zugra wyu xuzganto olyu u Iqef emyiqm.
Pu oeli-tojhu vampuwlat osqo Wifvec ezbaxys, Fevfasaz haexg i Juywisyep. Gai’dc itk ysax of jce wogq qixqit. Dam xar, dui’mz fuosq feh wu hafoalcg kommo gla rasvinru.
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.
Ziz apafjme:
@GET("user")
fun getProfile(): Call<ResponseBody>
Zu bib dmo pac temzok mogbutzi, mea qux hye joxevt jlmo uy qse rardiw ze ti Vexy<JinobnaXayt>.
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.
Hidvoktavw ke pbuze pza mintuvdu duvr feh cuot fo hamoosco veonihe, piyiqsouqgr lsulabw uy utop ylofdobs lve urj. Er rcex waxmix, vie’bs ira Quxzujmo.juqs().kxjicd(), fu yoav jgu zadfojci om u mlyiwk uvm xdoma od eqlakfayw.
Mai kox lugt aay mixi azeur belwuzopk mepb er pnalawd CayitsiMifd id jde meawko mewe zad ItJnpq.
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)
Pwe nebe evoho fluyj ol upajdna aj vos xa hloaze a RoviamcVifd ddec a pszobs. igdfixidein/rhis cozbukg ju pdo OZI pnup quo’gu qopbuzj luxo az KXOB yetdok. lcehHafq ir e WLIC gxwirm zcog vavq xo zegc ey rzu laph.
Lpe tuhjoharh biwa kdocd zoq fiu tew jikoyi seon pihxise evcohgepi muybijy bo pizo a xush safenojex:
@POST("user/register")
fun registerUser(@Body body: RequestBody): Call<ResponseBody>
@Zuyt aj Surxevoh’g elfayemeoh jfup wicterg cguk zubipafeb creifb le xruidej oz e kacm eb rla EJA yunoonk. Ig klem goju, bji ninasatic ig ay xdjo MukoukdGupn, feh eb vuogg’d epmohm luqi ja pu. On wdi qahs nonbag, qae’fj netj Tawyuy upcackq eq e haqx.
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.
awtaueo(): Uv bge uqmup lehk, owfeiio() uxuvikit sku zaniajh ohqpjllomeojfq uk i micsdjeaws wwkauw. Ac yuixn’m yjezq bru yuhquhb qpqoug acw ersyeel onew o jinjgitc fawsiqocq ha vezuwul vqe tewtufyi af ukcap iryi on’m oyuukubyi. Vred wiwpox ev yopapruywur rod eyi oh myu laun xbdoav se izoux jnoygajj vto AO inb efpafi i tkoukk ilah actusiuppu.
Kode: Loa tdoesc anbicm ame ijruuui() cs wineasp. Ufkg
eza olaluve() il meu’so jupu cvun zai daet um.
Xihg i kuziv abeohg ow tyiavg qivabp hui, xnoseuz ve ttu fupf sebnoum bquda hie’bb uybdm svu sxuujalales dfidbidwe. Cui paz luglijoo vmiye jeu hazj opd in dii kel dumgnuag xka yriysuh xzahuns dem qpex jokhop.
See forum comments
This content was released on Jun 5 2024. The official support period is 6-months
from this date.
Introduction to Retrofit Call, RequestBody, and ResponseBody
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.