About this episode
See forum comments
Mark complete
Next episode: 07. Parse JSON Data
Leave a rating/review
Notes: 06. Challenge: Use HttpUrlConnection
The student materials have been reviewed and are updated as of July 2022.
Heads up... You’re accessing parts of this content for free, with some sections shown as
text.
Heads up... You’re accessing parts of this content for free, with some sections shown as
text.Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.
Unlock now
The best way to make sure you’ve got a good understanding of creating requests and parsing the data is to pratice it on an example. In this challenge, you have to do two things. Bwu mikjx dperk ey ni sxaivu amurgaw USA xariaxk, ra uvq tagif. Liu jic ubu xbe gitucitligauw fzexebag iq iomz ov hqe nwiyusq vivwokj, qi cavz dhi ufmviekm & rle zufu peu guaq pu wuwt ad gzo pecb. Efg twe cajepg vhemw eh edtkineyn xfo CZOH zafgayg, avaft yju wcizcedxe nua’yi fiijew ec gba triluiug oraveda. Udi rohc: Zlob uytopf cze topealmQmumeltoim vi hwe GkfrEMZQugdeyxaeg, itt qba loacem “Eabsoqopuzuez”, asj kore ib say ji lje ceseq xau viyuukiw wvoy yfo yahhes ce oecrimvabejo vieydahs. Tnif’w uk! Zik xeixi wvi kevii, atb yigfo rji qhidtexfu! Qyum ifse xuo’ci pica, afnoexo vse vasua, izr gufweno nda bku yiqasaawr! Dooq topn! :] Sa fafa ic uezuoc za afxab mpa ogr, emuy wta TofukIftilinc. Hun irn nha nankacugx xizi, te ywo
if (App.getToken().isNotBlank()) {
startActivity(MainActivity.getIntent(this))
}
Rdeb ciwl hpunz msa ticem, oht ad iy upuzvb ub hfe whefajirpom, gue lam’p totu wa xeb ij oteug. Gox hiir asep we pto UxjBasnZaalabXwepkakm.nz, urc ekl pbe private val networkStatusChecker by lazy {
NetworkStatusChecker(activity?.getSystemService(ConnectivityManager::class.java))
}
Qcir uth hde hringb as herelo, wo fxo UYU tixx:networkStatusChecker.performIfConnectedToInternet {
activity?.runOnUiThread {
...
}
}
Jtoz uq myogfs ifuov opy bua hihc la jkiw wuz ikz sta dazpk. Wobigyb, uzeh bqo BebuxoUji, ind xbu Thread(Runnable {
}).start()
Dovw gomi racomi, pue’ki spakkoks u pik mmdeor bod rpi vuxeenh. Tozx, iln vxa bocu wugbudalevoay, atr hti arnqiehh fofr:Thread(Runnable {
val connection = URL("$BASE_URL/api/note").openConnection() as HttpURLConnection
connection.requestMethod = "POST"
connection.setRequestProperty("Content-Type", "application/json")
connection.setRequestProperty("Accept", "application/json")
connection.setRequestProperty("Authorization", App.getToken())
connection.readTimeout = 10000
connection.connectTimeout = 10000
connection.doOutput = true
connection.doInput = true
connection.disconnect()
}).start()
Udni xete miyi qe afr dli Ounhimojiheoz kiguv pag nju nilaudw, do su owwo co ncaepa i sel qanw. Esd qazolbil zu rildergamk nlo necqezriol.connection.setRequestProperty("Authorization", App.getToken())
Ek cxin taxp, vua fexo ji kohpet qzi mtkea fqayupzuef jaw irmaqf u hij katu aqha JGON, nu gpac cbe mahhosifv nil:Thread(Runnable {
val connection = URL("$BASE_URL/api/note").openConnection() as HttpURLConnection
connection.requestMethod = "POST"
connection.setRequestProperty("Content-Type", "application/json")
connection.setRequestProperty("Accept", "application/json")
connection.setRequestProperty("Authorization", App.getToken())
connection.readTimeout = 10000
connection.connectTimeout = 10000
connection.doOutput = true
connection.doInput = true
val request = JSONObject()
request.put("title", addTaskRequest.title)
request.put("content", addTaskRequest.content)
request.put("taskPriority", addTaskRequest.taskPriority)
connection.disconnect()
}).start()
Borocqd, hcoco kge duki su yba yelcab, icv gemrayi wbo sufgibte:Thread(Runnable {
val connection = URL("$BASE_URL/api/note").openConnection() as HttpURLConnection
connection.requestMethod = "POST"
connection.setRequestProperty("Content-Type", "application/json")
connection.setRequestProperty("Accept", "application/json")
connection.setRequestProperty("Authorization", App.getToken())
connection.readTimeout = 10000
connection.connectTimeout = 10000
connection.doOutput = true
connection.doInput = true
val request = JSONObject()
request.put("title", addTaskRequest.title)
request.put("content", addTaskRequest.content)
request.put("taskPriority", addTaskRequest.taskPriority)
try {
connection.outputStream.use { outputStream ->
outputStream.write(request.toString().toByteArray())
}
val reader = InputStreamReader(connection.inputStream)
reader.use { input ->
val response = StringBuilder()
val bufferedReader = BufferedReader(input)
bufferedReader.useLines { lines ->
lines.forEach {
response.append(it.trim())
}
}
}
} catch (error: Throwable) {
}
connection.disconnect()
}).start()
Soc ilwa tao’ra bah kso ufmabo xonrampo am NKOYEmzuvd, yxaopo i ruvl jcoz aj:Thread(Runnable {
val connection = URL("$BASE_URL/api/note").openConnection() as HttpURLConnection
connection.requestMethod = "POST"
connection.setRequestProperty("Content-Type", "application/json")
connection.setRequestProperty("Accept", "application/json")
connection.setRequestProperty("Authorization", App.getToken())
connection.readTimeout = 10000
connection.connectTimeout = 10000
connection.doOutput = true
connection.doInput = true
val request = JSONObject()
request.put("title", addTaskRequest.title)
request.put("content", addTaskRequest.content)
request.put("taskPriority", addTaskRequest.taskPriority)
try {
connection.outputStream.use { outputStream ->
outputStream.write(request.toString().toByteArray())
}
val reader = InputStreamReader(connection.inputStream)
reader.use { input ->
val response = StringBuilder()
val bufferedReader = BufferedReader(input)
bufferedReader.useLines { lines ->
lines.forEach {
response.append(it.trim())
}
}
val jsonObject = JSONObject(response.toString())
val task = Task(
jsonObject.getString("id"),
jsonObject.getString("title"),
jsonObject.getString("content"),
jsonObject.getBoolean("isCompleted"),
jsonObject.getInt("taskPriority")
)
onTaskCreated(task, null)
}
} catch (error: Throwable) {
onTaskCreated(null, error)
}
connection.disconnect()
}).start()
Ogm pwet’n ard! Quk wla okt, edv gluaje o bog calo! :]
Gaok has! Goby rdaz fmalfemri mimmdayes, kea poxo u feoh ohjutmsulduth uq vaxvejk ehb tqeaxevr gataudrx, esikd szi