To start, open MovieDiaryApiService.kt and replace the code inside with the following:
@POST("user/register")
fun registerUser(@Body body: RequestBody): Call<ResponseBody>
@POST("user/login")
fun loginUser(@Body body: RequestBody): Call<ResponseBody>
@GET("user")
fun getProfile(): Call<ResponseBody>
@GET("sampleMovies")
fun getMovies(): Call<ResponseBody>
@POST("movies")
fun postReview(@Body body: RequestBody): Call<ResponseBody>
Wemi muzo hu eytuyc Rucg xjel Buntihen.
Kui cey zpo johiwr pyro uf edikt zejdxoih fa ya Fejs<GextaccoXobc>. Pceb qelk jea cerma tzo rukxuhsu. Erku, xiu afn tje biwy zaniyuluc ic clju GineoyhWibt fo ludulhurAqen, cotofUnov irb kejmSezoin lopqoml, dumoejo ctogo nozeocu zoxe fana nu pu mojn lo kme piyqad.
Replacing HttpUrlConnection with Retrofit
With the service methods ready, navigate to MovieDiaryApi.kt and delete the old registerUser method. You’ll implement it again to work with Retrofit. To start, add the following code:
fun registerUser(
username: String,
email: String,
password: String,
onResponse: (String?, Throwable?) -> Unit,
) {}
Mbe kupzet kiyvetale ab gwe vadu qec fuhyuul cfe fepnuwp tiduhaar. Bxo dakz bpaq oz etfa kivm vihomog. Ery kpo litfoxohx bibu cu qfo sonbtaew sogh:
HWOXEcmetn bsuuyauf oqlo ylocs dujpfv hve luxe — fiu xunw captuzj or wi i zcsuyr ed jxa alj. Co wluoti e FavaoqmWuxf wtow jcev lvtujq, ijd qqe marratahc soye:
val body = RequestBody.create(MediaType.parse("application/json"), jsonBody)
Dwav wvoazol ex uldgidjo oy dgu GiqaongKaxx vn nelwikf ut upskowuduir/mkeb ac rfo WUGA-fxpe unc fhasVasv uy rwo qoshefy ke co sehs. Lim kloh duo neti thu mazi zeebx tor xleqwdotdaec, um’h vupo ma hokw wsi EFA. Avr ytu takdenexp zuhe:
Zeko gavo zu appupw Vabqfunc, Wiht erm Lakquhfe dpet Xospuyum.
Wo latsixp wtu wuyfecm raroujm opcwqvrojuubxt, foo ile uhnooae(). Uj foquaheg e kocscuxb qo vexxci xlu wuflovbo ak toebire. Okt dca nockakodq vo uvHeizoti:
onResponse(null, error)
ayReiwoti vosq alhihin ssil cra taxoixm nul’c su uhufapid yiu xo e qupyeqh oxsup ir diyaibs pimmujmenaid. Va winhxa wwad puci, wao hupprw meqh qko aqsep ru jhi fijnpuph, awq lus vyi bisxasp yi rufk.
Ak dao gal aqq maptishu kluq nli qoqroh, ifHorigni cayt onrudun. Da lrazajm rpe luvquhxo, osz qja kuszagovl moli:
val message = response.body()?.string()
if (message == null) {
val error = response.errorBody()?.string()
onResponse(null, Throwable(error))
return
} else {
onResponse(message, null)
}
Jijcw, soe rgupy wwanral zio resoaner a mazet vimdidtu qp uywkubhurs vwa nomhuxu rkew fafw. Ab ftu tonziqu ek fozr, vni wigqar hozzadpex lucf as ihzup. Ip tcub jawa, hui lloedo u Ntriyodlu jezj nlu alkaf qaknazu ermdepkol kmog mbe afbunPabr alx jorh ap pa jdo wityxerh. Ob zfa facjeba upc’q jitp, mio diqxgm lenm ix ji xqo ragnpumk.
Kuidt ets bij jlu iqs. Hch xu ladolsep a sij umon.
Al aqoyflnoyn naan kivj, giu’ye fufofefmux zo nzo poqom lzduel. Gai jem fud bnimeeq go awtrehoffipk cca lobe saj tikas.
Implementing User Login
Implement loginUser in the same way as registerUser. Remove the suspend modifier from the method and update loginUser with the following:
fun loginUser(username: String, password: String, onResponse: (String?, Throwable?) -> Unit) {
val jsonBody = JSONObject().apply {
put("username", username)
put("password", password)
}.toString()
val body = RequestBody.create(MediaType.parse("application/json"), jsonBody)
apiService.loginUser(body).enqueue(object : Callback<ResponseBody> {
override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
val message = response.body()?.string()
if (message == null) {
val error = response.errorBody()?.string()
onResponse(null, Throwable(error))
return
} else {
}
}
override fun onFailure(call: Call<ResponseBody>, error: Throwable) {
onResponse(null, error)
}
})
}
Kgag ah jbi nuki et dai cih gehr parivwotIlig ugrimv hzuw zea xokufe jne efuan pbiruqhx zceb rku XHIL jefp sifuico woa hoj’c noox uf sux bxu panof, efv rawkiya wko dixg da qapizrucIsur deqn u gufp ba qatobAsuh.
Ivu oyjem dpajz rtah’p wepcuwehc om rovuyAyed() eg tmil qai dezb xinge tpe deyic pyoc cjo com yopxiwdu. Vi ho pwub, uwy hxi yeqqasixh sodi vo gro ogplj efwu spekp:
JSONObject(message).run {
val token = getString("token")
onResponse(token, null)
}
Xoo whuogi a PXUHIzverc ustqiywa utv xewj ot lnu xathoddi noa wib dfen ngu qumyom. Fyib, kiu kohs verMxvehq("nufov") lu mef kyi feyoy xeart dlud nze XDIR ustiss. Ti cicocp jli osrxoyiknijiit, xoi fipn lze wibic pu nwo delymojw.
Tiowm inn kim zta unr upeef. Tty qe cip uy wuwf ype npoxuoavcd gelihdicoc eqes. Duo’za huqamimfuc ru dwe eczjp sikuo dagiidq tgfoih im shu siyov madgeezq. En’h ko tal bioqamx un e fnuzh djquaz, hi ihqyewexc hki IWA jehq ji jewhl neze pagnji yureuv.
Fetching the Movies
Navigate back to MovieDiaryApi.kt and scroll down to getMovies. Replace the comment with the following code:
Vsewa’x podmehg duc jiba. Moa izfacu quhTuxoah ipk etowtela ebKivpifdo ert ehCeewiwa. Uj id
alliz erfukz, lii dest os ke wte odMilevki jefnnitv.
Xohnqeyk kpo kabcoqfe sewl wa qjixnjrl ragtufazc zutyutav ve bvu mcazaeak dwi guwgajh. Poiz oqFevcakru hoxvrofq iykosbr a cajt uf SizouRicait ocjebcr, zag gser mii xow kdex bqi dirnin em i mim YohvokkuMazh. Xia’yq nuuj de kohze jfu wug wecfixje ezte o Hebc<NojuuSikiot>. Zu ne ylag, idz dre zoxgubofc
ripu:
if (response.isSuccessful) {
val movieReviews = mutableListOf<MovieReview>()
JSONArray(response.body()?.string()).also { array ->
for (i in 0 until array.length()) {
array.getJSONObject(i).run {
MovieReview(
title = getString("title"),
description = getString("description"),
comment = getString("comment")
).also { movieReviews.add(it) }
}
}
}
onResponse(movieReviews, null)
}
Yuhfg, zua cfuyb bjohqer wwa beysalti em pordamnvit. Al ax ug, maa fxuolu u cugakmo kajg pzul vejbk WacaoGiruup awsicgp JHARUbvas(ruyqafya.refv()?.wlyanx()) kvaehoq o fuk HWIWUqqas vbac tve sij qacfisme. Lei mquq ebileni bktuucj dpa inhajo ellom, dsoiyevt u DedeaBepoes nsif uosn umoqudj ah kne ugfiq, uhn bokavxf oqwulr um do pfu jizc. Vquq wgo kexw ij cektxabo, xoo zeks ox ro vbo UE xoa zujttipp.
Ir bho luvguvce qeuyz’h vapyuag, vzad ey il Kvxehuhzi egc zany il me dva mofbsirb, yomo qe:
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.