{"id":326850,"date":"2022-01-10T08:39:45","date_gmt":"2022-01-10T08:39:45","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=326850"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=326850","title":{"rendered":"<span>\u041f\u0438\u0448\u0435\u043c \u0431\u0435\u0437 Retrofit&#8217;\u0430, json&#8217;a \u0438 Kotlin Coroutines Android \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\" class=\"article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u043e\u0440\u043e\u0439 \u043d\u0430\u043c \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043a\u043e\u0434 \u0431\u0435\u0437 \u043b\u0438\u0448\u043d\u0438\u0445 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a, \u0447\u0442\u043e\u0431\u044b \u0431\u043e\u043b\u0435\u0435 \u0433\u043b\u0443\u0431\u043e\u043a\u043e \u043f\u043e\u043d\u044f\u0442\u044c \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u044b \u0438\u043b\u0438 \u043f\u0440\u043e\u0441\u0442\u043e \u0440\u0430\u0434\u0438 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0430.<\/p>\n<p>\u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u043f\u0440\u043e\u0441\u0442\u043e\u0435 Android \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u044e\u0437\u0435\u0440\u0430\u043c \u043d\u0430\u0439\u0442\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0440\u0443\u0441\u0441\u043a\u043e\u0433\u043e \u0441\u043b\u043e\u0432\u0430:<\/p>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"\/img\/image-loader.svg\" height=\"2400\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/79b\/266\/6a7\/79b2666a7d579f3e831de51f76497d63.png\" data-width=\"1080\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 GET \u0437\u0430\u043f\u0440\u043e\u0441 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d \u0447\u0435\u0440\u0435\u0437 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0435 \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430 Java, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0430\u0445\u043e\u0434\u044f\u0442\u0441\u044f \u0432 \u043f\u0430\u043a\u0435\u0442\u0435 <code>java.net.*<\/code><\/p>\n<p>\u041f\u0430\u0440\u0441\u0438\u043d\u0433 JSON \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0439 \u0432 Android \u043f\u0430\u043a\u0435\u0442 <code>org.json.*<\/code><\/p>\n<p>\u0410 \u0434\u043b\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0432 background \u043f\u043e\u0442\u043e\u043a\u0435 \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430 \u0438 Java \u043f\u0430\u043a\u0435\u0442 <code>java.util.concurrent.*<\/code> <\/p>\n<p>\u0422\u0430\u043a\u0436\u0435 \u0434\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d Debounce \u044d\u0444\u0444\u0435\u043a\u0442 \u0441 \u0437\u0430\u0434\u0435\u0440\u0436\u043a\u043e\u0439 \u0432 500 \u043c\u0441.<\/p>\n<p>\u041d\u0443 \u0447\u0442\u043e \u0436 \u043f\u0440\u043e\u0439\u0434\u0435\u043c\u0441\u044f \u043f\u043e \u0432\u0441\u0435\u043c \u0447\u0430\u0441\u0442\u044f\u043c \u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e<\/p>\n<h2>\u0414\u0435\u043b\u0430\u0435\u043c GET \u0437\u0430\u043f\u0440\u043e\u0441 \u0431\u0435\u0437 Retrofit&#8217;\u0430)<\/h2>\n<p>\u041f\u043e\u043a\u0430\u0436\u0443 \u0441\u0440\u0430\u0437\u0443 \u043a\u043e\u0434:<\/p>\n<pre><code class=\"kotlin\">open class GetRequest(     private val url: String,     private val executor: ExecutorService,     private val handler: Handler ) {      fun execute(onSuccess: (json: String) -> Unit, onError: (error: GetError) -> Unit) {         executor.execute {              var conn : HttpsURLConnection? = null              try {                 val connection = URL(url).openConnection() as HttpsURLConnection                 conn = connection                  connection.requestMethod = \"GET\"                 connection.setRequestProperty(\"Content-Type\", \"application\/json; utf-8\")                 connection.connectTimeout = 5000                 connection.readTimeout = 5000                  val json = connection.inputStream.bufferedReader().readText()                  handler.post { onSuccess(json) }              } catch (error: Exception) {                 conn?.disconnect()                 handler.post {                     if (error is UnknownHostException) {                         onError(GetError.MISSING_INTERNET)                     } else {                         onError(GetError.OTHER)                     }                 }             }         }     }  }<\/code><\/pre>\n<p>\u041c\u044b \u043f\u0440\u043e\u043a\u0438\u0434\u044b\u0432\u0430\u0435\u043c <code>ExecucotService<\/code>, \u0447\u0442\u043e\u0431\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0430\u0448 \u0437\u0430\u043f\u0440\u043e\u0441 \u0432 background \u043f\u043e\u0442\u043e\u043a\u0435.<\/p>\n<p><code>Handler<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u043d\u0430 UI \u043f\u043e\u0442\u043e\u043a<\/p>\n<p><code>HttpsURLConnection<\/code> \u0432\u0445\u043e\u0434\u0438\u0442 \u0432\u043e \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0439 \u043f\u0430\u043a\u0435\u0442 <code>java.net.*<\/code> \u0438 \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d \u0434\u043b\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0441\u0435\u0442\u0435\u0432\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432.<\/p>\n<p>\u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b <code>HttpsURLConnection<\/code> \u044f \u0434\u0443\u043c\u0430\u044e \u0432\u0430\u043c \u043f\u043e\u043d\u044f\u0442\u043d\u044b.<\/p>\n<p>\u0417\u0430\u0442\u0435\u043c \u043c\u044b \u0447\u0438\u0442\u0430\u0435\u043c \u0432\u0441\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0447\u0435\u0440\u0435\u0437 <code>BufferedReader<\/code> \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0434\u0430\u043b\u044c\u0448\u0435 \u0447\u0435\u0440\u0435\u0437 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u044e\u0442\u0441\u044f \u0432 \u043c\u0435\u0442\u043e\u0434 <code>execute()<\/code>.<\/p>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u043d\u0430\u0448 \u043a\u043b\u0430\u0441\u0441 \u043c\u043e\u0436\u0435\u0442 \u0438\u043c\u0435\u0442\u044c \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a\u043e\u0432. <\/p>\n<p>\u042f \u0441\u0434\u0435\u043b\u0430\u043b \u044d\u0442\u043e \u043b\u0438\u0448\u044c \u0434\u043b\u044f \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0433\u043e \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 (\u043c\u043e\u0436\u043d\u043e \u0441\u043c\u0435\u043b\u043e \u044e\u0437\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e <code>GetRequest<\/code>)<\/p>\n<p>\u0412 \u043c\u043e\u0435\u043c \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u044d\u0442\u043e <code>DictGetRequest:<\/code><\/p>\n<pre><code class=\"kotlin\">class DictGetRequest(word: String, executor: ExecutorService, handler: Handler)     : GetRequest(\"https:\/\/api.dictionaryapi.dev\/api\/v2\/entries\/ru\/$word\",                   executor, handler)<\/code><\/pre>\n<h2>\u0421\u0442\u0440\u0430\u0448\u043d\u044b\u0439 \u043f\u0430\u0440\u0441\u0438\u043d\u0433 JSON&#8217;\u0430 \u0432\u0440\u0443\u0447\u043d\u0443\u044e ?<\/h2>\n<p>\u041f\u043e\u0436\u0430\u043b\u0443\u0439 \u044d\u0442\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043e\u0447\u0435\u043d\u044c \u0441\u0442\u0440\u0430\u0448\u043d\u043e:<\/p>\n<pre><code class=\"kotlin\">sealed interface DictResultData {      fun toUi() : DictResultUi      data class Success(private val word: String, private val definitions: List&lt;DictDefinition>) : DictResultData {         override fun toUi(): DictResultUi {             return DictResultUi.Success(word, definitions)         }     }      data class Error(@StringRes private val resId: Int) : DictResultData {         override fun toUi(): DictResultUi {             return DictResultUi.Error(resId)         }     }      companion object {         fun fromJson(json: String) : DictResultData {              if (json.isJsonObject()) {                 return Error(R.string.nothing_found)             }              val jsonObject = json.toJsonArray().firstObject()              val word = jsonObject.str(\"word\")             val jsonDefinitions = jsonObject.array(\"meanings\")                 .firstObject()                 .array(\"definitions\")              val definitions = mutableListOf&lt;DictDefinition>()              for (i in 0 until jsonDefinitions.length()) {                  val jsonDefinition = jsonDefinitions.jsonObject(i)                  val definition = jsonDefinition.str(\"definition\")                 val example = jsonDefinition.str(\"example\")                  definitions.add(DictDefinition(definition, example))             }              return Success(word, definitions)         }     }  }<\/code><\/pre>\n<p>\u042f \u044e\u0437\u0430\u044e <code>sealed interface<\/code> \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0437\u0430\u043f\u0440\u043e\u0441 \u043c\u043e\u0436\u0435\u0442 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u043e\u0442\u0432\u0435\u0442\u0430 (\u043e\u0448\u0438\u0431\u043a\u0430 \u0438\u043b\u0438 \u0443\u0441\u043f\u0435\u0445).<\/p>\n<p>\u041b\u043e\u0433\u0438\u043a\u0430 \u0440\u0430\u0431\u043e\u0442\u044b \u043c\u0435\u0442\u043e\u0434\u0430 <code>fromJson()<\/code> \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u043d\u0435\u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e\u0439.<\/p>\n<p>\u0412\u043e-\u043f\u0435\u0440\u0432\u044b\u0445, \u0437\u0434\u0435\u0441\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f Kotlin \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u044f \u0432\u044b\u043d\u0435\u0441 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e:<\/p>\n<pre><code class=\"kotlin\">fun String.isJsonObject() : Boolean {     return JSONTokener(this).nextValue() is JSONObject }  fun String.toJsonArray() : JSONArray {     return JSONArray(this) }  fun JSONObject.str(key: String, default: String = \"\") : String {     return if (has(key))  getString(key) else default }  fun JSONArray.firstObject() : JSONObject {     return if (length() == 0) JSONObject() else getJSONObject(0) }   fun JSONArray.jsonObject(index: Int) : JSONObject {     return getJSONObject(index) }  fun JSONObject.array(key: String, default: JSONArray = JSONArray()) : JSONArray {     return if (has(key)) getJSONArray(key) else default } <\/code><\/pre>\n<p>\u0412\u043e-\u0432\u0442\u043e\u0440\u044b\u0445, <code>fromJson()<\/code> \u043c\u043e\u0436\u0435\u0442 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043b\u0438\u0431\u043e \u043e\u0448\u0438\u0431\u043a\u0443 \u043b\u0438\u0431\u043e \u0443\u0441\u043f\u0435\u0445 \u0438 \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u044f \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u044e, \u0435\u0441\u043b\u0438 JSON \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u043c, \u0442\u043e \u044d\u0442\u043e \u043e\u0448\u0438\u0431\u043a\u0430 (\u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u043e\u0442\u0432\u0435\u0442\u0430 \u043e\u0442 \u0441\u0435\u0440\u0432\u0435\u0440\u0430, \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0443\u0441\u043f\u0435\u0445\u0430 \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u043c\u0430\u0441\u0441\u0438\u0432).<\/p>\n<h2>\u0420\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u0438 \u043d\u0430\u0448\u0430 ViewModel&#8217;\u043a\u0430 ?<\/h2>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u0438 ViewModel&#8217;\u043a\u0443, \u043e\u043d\u0438 \u0442\u0430\u043a\u0438\u0435 \u043c\u0438\u043b\u044b\u0435:<\/p>\n<pre><code class=\"kotlin\">\/\/ Repository class DictRepositoryImpl(private val executor: ExecutorService, private val handler: Handler) :     DictRepository {     override fun infoAboutWordBy(word: String, onSuccess: (dict: DictResultData) -> Unit) {         val request = DictGetRequest(word, executor, handler)         request.execute(             { json -> onSuccess(DictResultData.fromJson(json)) },             { error -> onSuccess(DictResultData.Error(error.resId)) }         )     } }  \/\/ ViewModel class DictViewModel(private val repo: DictRepository) : ViewModel() {      private val wordUi = MutableLiveData&lt;DictResultUi>()      fun observe(lifecycleOwner: LifecycleOwner, observer: Observer&lt;DictResultUi>) = wordUi.observe(lifecycleOwner, observer)      fun searchWordDefinition(word: String) {         if (word.isEmpty()) {             return         }          wordUi.value = DictResultUi.Loading         repo.infoAboutWordBy(word) { result ->             wordUi.value = result.toUi()         }     }  }<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u0432\u0441\u0435 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e: \u0432 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438 \u043c\u044b \u0434\u0435\u043b\u0430\u0435\u043c GET \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440 \u0438 \u0447\u0435\u0440\u0435\u0437 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0434\u0430\u043b\u0435\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u0432\u043e <code>ViewModel<\/code>.<\/p>\n<p>\u0420\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 <code>DictResultData<\/code> \u043a\u043b\u0430\u0441\u0441\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u043c\u0430\u043f\u043f\u0438\u043c \u0432 <code>DictResultUi<\/code>:<\/p>\n<pre><code class=\"kotlin\">sealed interface DictResultUi {     object Loading: DictResultUi     data class Error(@StringRes private val textResId: Int): DictResultUi {         fun text(view: TextView) {             view.setText(textResId)         }     }     data class Success(private val word: String, private val definitions: List&lt;DictDefinition>) : DictResultUi {          fun word(view: TextView) {             view.text = word         }          fun definitions(layout: LinearLayoutCompat) {             layout.removeAllViews()             definitions.mapIndexed { index, definition -> definition.str(index + 1) }                 .forEach { str ->                     layout.addView(AppCompatTextView(layout.context).apply {                         text = str                         setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f)                         setTextColor(ContextCompat.getColor(context, R.color.grey_300))                         layoutParams = LinearLayoutCompat.LayoutParams(                             LinearLayoutCompat.LayoutParams.MATCH_PARENT,                             LinearLayoutCompat.LayoutParams.WRAP_CONTENT                         ).apply {                             bottomMargin = 8.dp(context)                         }                     })                 }         }      } }<\/code><\/pre>\n<p>\u041d\u0435 \u043f\u0443\u0433\u0430\u0439\u0442\u0435\u0441\u044c, \u044f \u043d\u0435 \u0441\u0442\u0430\u043b \u0441\u0438\u043b\u044c\u043d\u043e \u0437\u0430\u043c\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u0438 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0440\u0435\u0444\u0430\u043a\u0442\u043e\u0440\u0438\u0442\u044c \u043a\u043e\u0434.<\/p>\n<p>\u041d\u0443 \u0438 \u044f \u043f\u0440\u043e\u0441\u0442\u043e \u043e\u0431\u043e\u0436\u0430\u044e \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c UI \u043a\u043e\u0434\u043e\u043c ?<\/p>\n<h2>MainActivity \u0438 \u043d\u0430\u0448 \u043b\u044e\u0431\u0438\u043c\u044b\u0439 Debounce \u044d\u0444\u0444\u0435\u043a\u0442<\/h2>\n<p>\u0412\u0437\u0433\u043b\u044f\u043d\u0435\u043c \u043d\u0430 <code>MainActiivty:<\/code><\/p>\n<pre><code class=\"kotlin\">class MainActivity : AppCompatActivity() {      override fun onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)         val binding = ActivityMainBinding.inflate(layoutInflater)         setContentView(binding.root)          val executor = Executors.newSingleThreadExecutor()         val handler = Handler(Looper.getMainLooper())         val viewModel = ViewModelProvider(this, DictViewModelFactory(DictRepositoryImpl(executor, handler)))             .get(DictViewModel::class.java)          viewModel.observe(this) { dictResult ->              val isError = dictResult is DictResultUi.Error             val isSuccess = dictResult is DictResultUi.Success             val isLoading = dictResult is DictResultUi.Loading              binding.frameLayout.isVisible = isLoading or isError             binding.progress.isVisible = isLoading             binding.errorText.isVisible = isError             binding.definitionsLayout.isVisible = isSuccess             binding.wordText.isVisible = isSuccess              when (dictResult) {                 is DictResultUi.Error -> {                     dictResult.text(binding.errorText)                 }                 is DictResultUi.Success -> {                     dictResult.word(binding.wordText)                     dictResult.definitions(binding.definitionsLayout)                 }                 else -> {}             }          }          val debounce = Debounce(Handler(Looper.getMainLooper()))         val runnable = Runnable { viewModel.searchWordDefinition(binding.searchEdit.text.toString()) }         binding.searchEdit.onTextChange { debounce.run(runnable) }         binding.searchBox.setEndIconOnClickListener { runnable.run() }     }  }<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u043c <code>ViewModel<\/code>, \u043f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c\u0441\u044f \u043d\u0430 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 <code>LiveData<\/code>  \u0438 \u0434\u0435\u043b\u0430\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441, \u043a\u043e\u0433\u0434\u0430 \u043d\u0430\u0431\u0438\u0440\u0430\u0435\u043c \u0442\u0435\u043a\u0441\u0442 \u0438\u043b\u0438 \u043d\u0430\u0436\u0438\u043c\u0430\u0435\u043c \u043d\u0430 \u043a\u043d\u043e\u043f\u043a\u0443 \u043f\u043e\u0438\u0441\u043a\u0430.<\/p>\n<p>\u041a\u043b\u0430\u0441\u0441 <code>Debounce<\/code> \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"kotlin\">class Debounce(private val handler: Handler) {      fun run(runnable: Runnable, delay: Long = 500) {         handler.removeCallbacks(runnable)         handler.postDelayed(runnable, delay)     }  }<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u043c\u044b \u043e\u0442\u043c\u0435\u043d\u044f\u0435\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u043d\u043e\u0432\u044b\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 500 \u043c\u0441, \u0435\u0441\u043b\u0438 \u043c\u044b \u043d\u0435 \u0431\u0443\u0434\u0435\u043c \u043d\u0438\u0447\u0435\u0433\u043e \u043f\u0438\u0441\u0430\u0442\u044c \u0432 \u043f\u043e\u043b\u0435 \u043f\u043e\u0438\u0441\u043a\u0430.<\/p>\n<h2>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>\u042f \u043a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e \u043d\u0435 \u0441\u043c\u043e\u0433, \u0434\u0430 \u044d\u0442\u043e \u0438 \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c \u0432\u0441\u0435 \u0442\u043e\u043d\u043a\u043e\u0441\u0442\u0438 \u0432 \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435.<\/p>\n<p>\u0421\u043e\u0432\u0435\u0442\u0443\u044e \u0432\u0430\u043c \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u043c\u043e\u043c\u0435\u043d\u0442\u044b:<\/p>\n<ul>\n<li>\n<p>\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b GET \u0437\u0430\u043f\u0440\u043e\u0441\u0430, \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u0442\u0435\u043b\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430, Headers \u0438 Cookies, \u043d\u0443 \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u0442\u0438\u043f\u044b \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432, \u0442\u0430\u043a\u0438\u0435 \u043a\u0430\u043a POST, PUT, UPDATE \u0438 DELETE<\/p>\n<\/li>\n<li>\n<p>\u043f\u0440\u0438\u043d\u0446\u0438\u043f\u044b \u0440\u0430\u0431\u043e\u0442\u044b \u043f\u0443\u043b\u0430 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0438 <code>Handler<\/code>. <\/p>\n<\/li>\n<li>\n<p>\u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435 \u0440\u0435\u0444\u0430\u043a\u0442\u043e\u0440\u0438\u043d\u0433, \u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u043d\u0435 \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u0434  \u0438 \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u043b\u044f \u0435\u0433\u043e \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u044f \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u043f\u043e\u0442\u0440\u0430\u0442\u0438\u0442\u044c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f (\u0432\u043d\u0435\u0434\u0440\u0435\u043d\u0438\u0435 ExecutorService \u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439 \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440, \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u043d\u0430 \u0431\u043e\u043b\u0435\u0435 \u043c\u0435\u043b\u043a\u0438\u0435 \u0447\u0430\u0441\u0442\u0438 \u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u0432)<\/p>\n<\/li>\n<\/ul>\n<p>\u041e\u0441\u0442\u0430\u0432\u043b\u044f\u044e \u0441\u0441\u044b\u043b\u043a\u0443 <a href=\"https:\/\/github.com\/KiberneticWorm\/LearningApps\/tree\/master\/Dictapp\" rel=\"noopener noreferrer nofollow\">\u043d\u0430 \u0440\u0430\u0431\u043e\u0447\u0435\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435<\/a><\/p>\n<p>\u0416\u0435\u043b\u0430\u044e \u0432\u0441\u0435\u043c, \u0443 \u043a\u043e\u0433\u043e \u043d\u0435 \u0434\u0438\u0430\u0431\u0435\u0442 \u0442\u0435\u043f\u043b\u044b\u0445 \u0438 \u0441\u043b\u0430\u0434\u043a\u0438\u0445 \u0437\u0438\u043c\u043d\u0438\u0445 \u0432\u0435\u0447\u0435\u0440\u043e\u0432 (\u0448\u0443\u0442\u043a\u0430) ?<\/p>\n<\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/599657\/\"> https:\/\/habr.com\/ru\/post\/599657\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\" class=\"article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u043e\u0440\u043e\u0439 \u043d\u0430\u043c \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043a\u043e\u0434 \u0431\u0435\u0437 \u043b\u0438\u0448\u043d\u0438\u0445 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a, \u0447\u0442\u043e\u0431\u044b \u0431\u043e\u043b\u0435\u0435 \u0433\u043b\u0443\u0431\u043e\u043a\u043e \u043f\u043e\u043d\u044f\u0442\u044c \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u044b \u0438\u043b\u0438 \u043f\u0440\u043e\u0441\u0442\u043e \u0440\u0430\u0434\u0438 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0430.<\/p>\n<p>\u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u043f\u0440\u043e\u0441\u0442\u043e\u0435 Android \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u044e\u0437\u0435\u0440\u0430\u043c \u043d\u0430\u0439\u0442\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0440\u0443\u0441\u0441\u043a\u043e\u0433\u043e \u0441\u043b\u043e\u0432\u0430:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 GET \u0437\u0430\u043f\u0440\u043e\u0441 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d \u0447\u0435\u0440\u0435\u0437 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0435 \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430 Java, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0430\u0445\u043e\u0434\u044f\u0442\u0441\u044f \u0432 \u043f\u0430\u043a\u0435\u0442\u0435 <code>java.net.*<\/code><\/p>\n<p>\u041f\u0430\u0440\u0441\u0438\u043d\u0433 JSON \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0439 \u0432 Android \u043f\u0430\u043a\u0435\u0442 <code>org.json.*<\/code><\/p>\n<p>\u0410 \u0434\u043b\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0432 background \u043f\u043e\u0442\u043e\u043a\u0435 \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430 \u0438 Java \u043f\u0430\u043a\u0435\u0442 <code>java.util.concurrent.*<\/code> <\/p>\n<p>\u0422\u0430\u043a\u0436\u0435 \u0434\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d Debounce \u044d\u0444\u0444\u0435\u043a\u0442 \u0441 \u0437\u0430\u0434\u0435\u0440\u0436\u043a\u043e\u0439 \u0432 500 \u043c\u0441.<\/p>\n<p>\u041d\u0443 \u0447\u0442\u043e \u0436 \u043f\u0440\u043e\u0439\u0434\u0435\u043c\u0441\u044f \u043f\u043e \u0432\u0441\u0435\u043c \u0447\u0430\u0441\u0442\u044f\u043c \u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e<\/p>\n<h2>\u0414\u0435\u043b\u0430\u0435\u043c GET \u0437\u0430\u043f\u0440\u043e\u0441 \u0431\u0435\u0437 Retrofit&#8217;\u0430)<\/h2>\n<p>\u041f\u043e\u043a\u0430\u0436\u0443 \u0441\u0440\u0430\u0437\u0443 \u043a\u043e\u0434:<\/p>\n<pre><code class=\"kotlin\">open class GetRequest(     private val url: String,     private val executor: ExecutorService,     private val handler: Handler ) {      fun execute(onSuccess: (json: String) -> Unit, onError: (error: GetError) -> Unit) {         executor.execute {              var conn : HttpsURLConnection? = null              try {                 val connection = URL(url).openConnection() as HttpsURLConnection                 conn = connection                  connection.requestMethod = \"GET\"                 connection.setRequestProperty(\"Content-Type\", \"application\/json; utf-8\")                 connection.connectTimeout = 5000                 connection.readTimeout = 5000                  val json = connection.inputStream.bufferedReader().readText()                  handler.post { onSuccess(json) }              } catch (error: Exception) {                 conn?.disconnect()                 handler.post {                     if (error is UnknownHostException) {                         onError(GetError.MISSING_INTERNET)                     } else {                         onError(GetError.OTHER)                     }                 }             }         }     }  }<\/code><\/pre>\n<p>\u041c\u044b \u043f\u0440\u043e\u043a\u0438\u0434\u044b\u0432\u0430\u0435\u043c <code>ExecucotService<\/code>, \u0447\u0442\u043e\u0431\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0430\u0448 \u0437\u0430\u043f\u0440\u043e\u0441 \u0432 background \u043f\u043e\u0442\u043e\u043a\u0435.<\/p>\n<p><code>Handler<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u043d\u0430 UI \u043f\u043e\u0442\u043e\u043a<\/p>\n<p><code>HttpsURLConnection<\/code> \u0432\u0445\u043e\u0434\u0438\u0442 \u0432\u043e \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0439 \u043f\u0430\u043a\u0435\u0442 <code>java.net.*<\/code> \u0438 \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d \u0434\u043b\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0441\u0435\u0442\u0435\u0432\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432.<\/p>\n<p>\u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b <code>HttpsURLConnection<\/code> \u044f \u0434\u0443\u043c\u0430\u044e \u0432\u0430\u043c \u043f\u043e\u043d\u044f\u0442\u043d\u044b.<\/p>\n<p>\u0417\u0430\u0442\u0435\u043c \u043c\u044b \u0447\u0438\u0442\u0430\u0435\u043c \u0432\u0441\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0447\u0435\u0440\u0435\u0437 <code>BufferedReader<\/code> \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0434\u0430\u043b\u044c\u0448\u0435 \u0447\u0435\u0440\u0435\u0437 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u044e\u0442\u0441\u044f \u0432 \u043c\u0435\u0442\u043e\u0434 <code>execute()<\/code>.<\/p>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u043d\u0430\u0448 \u043a\u043b\u0430\u0441\u0441 \u043c\u043e\u0436\u0435\u0442 \u0438\u043c\u0435\u0442\u044c \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a\u043e\u0432. <\/p>\n<p>\u042f \u0441\u0434\u0435\u043b\u0430\u043b \u044d\u0442\u043e \u043b\u0438\u0448\u044c \u0434\u043b\u044f \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0433\u043e \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 (\u043c\u043e\u0436\u043d\u043e \u0441\u043c\u0435\u043b\u043e \u044e\u0437\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e <code>GetRequest<\/code>)<\/p>\n<p>\u0412 \u043c\u043e\u0435\u043c \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u044d\u0442\u043e <code>DictGetRequest:<\/code><\/p>\n<pre><code class=\"kotlin\">class DictGetRequest(word: String, executor: ExecutorService, handler: Handler)     : GetRequest(\"https:\/\/api.dictionaryapi.dev\/api\/v2\/entries\/ru\/$word\",                   executor, handler)<\/code><\/pre>\n<h2>\u0421\u0442\u0440\u0430\u0448\u043d\u044b\u0439 \u043f\u0430\u0440\u0441\u0438\u043d\u0433 JSON&#8217;\u0430 \u0432\u0440\u0443\u0447\u043d\u0443\u044e ?<\/h2>\n<p>\u041f\u043e\u0436\u0430\u043b\u0443\u0439 \u044d\u0442\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043e\u0447\u0435\u043d\u044c \u0441\u0442\u0440\u0430\u0448\u043d\u043e:<\/p>\n<pre><code class=\"kotlin\">sealed interface DictResultData {      fun toUi() : DictResultUi      data class Success(private val word: String, private val definitions: List&lt;DictDefinition>) : DictResultData {         override fun toUi(): DictResultUi {             return DictResultUi.Success(word, definitions)         }     }      data class Error(@StringRes private val resId: Int) : DictResultData {         override fun toUi(): DictResultUi {             return DictResultUi.Error(resId)         }     }      companion object {         fun fromJson(json: String) : DictResultData {              if (json.isJsonObject()) {                 return Error(R.string.nothing_found)             }              val jsonObject = json.toJsonArray().firstObject()              val word = jsonObject.str(\"word\")             val jsonDefinitions = jsonObject.array(\"meanings\")                 .firstObject()                 .array(\"definitions\")              val definitions = mutableListOf&lt;DictDefinition>()              for (i in 0 until jsonDefinitions.length()) {                  val jsonDefinition = jsonDefinitions.jsonObject(i)                  val definition = jsonDefinition.str(\"definition\")                 val example = jsonDefinition.str(\"example\")                  definitions.add(DictDefinition(definition, example))             }              return Success(word, definitions)         }     }  }<\/code><\/pre>\n<p>\u042f \u044e\u0437\u0430\u044e <code>sealed interface<\/code> \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0437\u0430\u043f\u0440\u043e\u0441 \u043c\u043e\u0436\u0435\u0442 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u043e\u0442\u0432\u0435\u0442\u0430 (\u043e\u0448\u0438\u0431\u043a\u0430 \u0438\u043b\u0438 \u0443\u0441\u043f\u0435\u0445).<\/p>\n<p>\u041b\u043e\u0433\u0438\u043a\u0430 \u0440\u0430\u0431\u043e\u0442\u044b \u043c\u0435\u0442\u043e\u0434\u0430 <code>fromJson()<\/code> \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u043d\u0435\u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e\u0439.<\/p>\n<p>\u0412\u043e-\u043f\u0435\u0440\u0432\u044b\u0445, \u0437\u0434\u0435\u0441\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f Kotlin \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u044f \u0432\u044b\u043d\u0435\u0441 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e:<\/p>\n<pre><code class=\"kotlin\">fun String.isJsonObject() : Boolean {     return JSONTokener(this).nextValue() is JSONObject }  fun String.toJsonArray() : JSONArray {     return JSONArray(this) }  fun JSONObject.str(key: String, default: String = \"\") : String {     return if (has(key))  getString(key) else default }  fun JSONArray.firstObject() : JSONObject {     return if (length() == 0) JSONObject() else getJSONObject(0) }   fun JSONArray.jsonObject(index: Int) : JSONObject {     return getJSONObject(index) }  fun JSONObject.array(key: String, default: JSONArray = JSONArray()) : JSONArray {     return if (has(key)) getJSONArray(key) else default } <\/code><\/pre>\n<p>\u0412\u043e-\u0432\u0442\u043e\u0440\u044b\u0445, <code>fromJson()<\/code> \u043c\u043e\u0436\u0435\u0442 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043b\u0438\u0431\u043e \u043e\u0448\u0438\u0431\u043a\u0443 \u043b\u0438\u0431\u043e \u0443\u0441\u043f\u0435\u0445 \u0438 \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u044f \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u044e, \u0435\u0441\u043b\u0438 JSON \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u043c, \u0442\u043e \u044d\u0442\u043e \u043e\u0448\u0438\u0431\u043a\u0430 (\u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u043e\u0442\u0432\u0435\u0442\u0430 \u043e\u0442 \u0441\u0435\u0440\u0432\u0435\u0440\u0430, \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0443\u0441\u043f\u0435\u0445\u0430 \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u043c\u0430\u0441\u0441\u0438\u0432).<\/p>\n<h2>\u0420\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u0438 \u043d\u0430\u0448\u0430 ViewModel&#8217;\u043a\u0430 ?<\/h2>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u0438 ViewModel&#8217;\u043a\u0443, \u043e\u043d\u0438 \u0442\u0430\u043a\u0438\u0435 \u043c\u0438\u043b\u044b\u0435:<\/p>\n<pre><code class=\"kotlin\">\/\/ Repository class DictRepositoryImpl(private val executor: ExecutorService, private val handler: Handler) :     DictRepository {     override fun infoAboutWordBy(word: String, onSuccess: (dict: DictResultData) -> Unit) {         val request = DictGetRequest(word, executor, handler)         request.execute(             { json -> onSuccess(DictResultData.fromJson(json)) },             { error -> onSuccess(DictResultData.Error(error.resId)) }         )     } }  \/\/ ViewModel class DictViewModel(private val repo: DictRepository) : ViewModel() {      private val wordUi = MutableLiveData&lt;DictResultUi>()      fun observe(lifecycleOwner: LifecycleOwner, observer: Observer&lt;DictResultUi>) = wordUi.observe(lifecycleOwner, observer)      fun searchWordDefinition(word: String) {         if (word.isEmpty()) {             return         }          wordUi.value = DictResultUi.Loading         repo.infoAboutWordBy(word) { result ->             wordUi.value = result.toUi()         }     }  }<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u0432\u0441\u0435 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e: \u0432 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438 \u043c\u044b \u0434\u0435\u043b\u0430\u0435\u043c GET \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440 \u0438 \u0447\u0435\u0440\u0435\u0437 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0434\u0430\u043b\u0435\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u0432\u043e <code>ViewModel<\/code>.<\/p>\n<p>\u0420\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 <code>DictResultData<\/code> \u043a\u043b\u0430\u0441\u0441\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u043c\u0430\u043f\u043f\u0438\u043c \u0432 <code>DictResultUi<\/code>:<\/p>\n<pre><code class=\"kotlin\">sealed interface DictResultUi {     object Loading: DictResultUi     data class Error(@StringRes private val textResId: Int): DictResultUi {         fun text(view: TextView) {             view.setText(textResId)         }     }     data class Success(private val word: String, private val definitions: List&lt;DictDefinition>) : DictResultUi {          fun word(view: TextView) {             view.text = word         }          fun definitions(layout: LinearLayoutCompat) {             layout.removeAllViews()             definitions.mapIndexed { index, definition -> definition.str(index + 1) }                 .forEach { str ->                     layout.addView(AppCompatTextView(layout.context).apply {                         text = str                         setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f)                         setTextColor(ContextCompat.getColor(context, R.color.grey_300))                         layoutParams = LinearLayoutCompat.LayoutParams(                             LinearLayoutCompat.LayoutParams.MATCH_PARENT,                             LinearLayoutCompat.LayoutParams.WRAP_CONTENT                         ).apply {                             bottomMargin = 8.dp(context)                         }                     })                 }         }      } }<\/code><\/pre>\n<p>\u041d\u0435 \u043f\u0443\u0433\u0430\u0439\u0442\u0435\u0441\u044c, \u044f \u043d\u0435 \u0441\u0442\u0430\u043b \u0441\u0438\u043b\u044c\u043d\u043e \u0437\u0430\u043c\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u0438 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0440\u0435\u0444\u0430\u043a\u0442\u043e\u0440\u0438\u0442\u044c \u043a\u043e\u0434.<\/p>\n<p>\u041d\u0443 \u0438 \u044f \u043f\u0440\u043e\u0441\u0442\u043e \u043e\u0431\u043e\u0436\u0430\u044e \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c UI \u043a\u043e\u0434\u043e\u043c ?<\/p>\n<h2>MainActivity \u0438 \u043d\u0430\u0448 \u043b\u044e\u0431\u0438\u043c\u044b\u0439 Debounce \u044d\u0444\u0444\u0435\u043a\u0442<\/h2>\n<p>\u0412\u0437\u0433\u043b\u044f\u043d\u0435\u043c \u043d\u0430 <code>MainActiivty:<\/code><\/p>\n<pre><code class=\"kotlin\">class MainActivity : AppCompatActivity() {      override fun onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)         val binding = ActivityMainBinding.inflate(layoutInflater)         setContentView(binding.root)          val executor = Executors.newSingleThreadExecutor()         val handler = Handler(Looper.getMainLooper())         val viewModel = ViewModelProvider(this, DictViewModelFactory(DictRepositoryImpl(executor, handler)))             .get(DictViewModel::class.java)          viewModel.observe(this) { dictResult ->              val isError = dictResult is DictResultUi.Error             val isSuccess = dictResult is DictResultUi.Success             val isLoading = dictResult is DictResultUi.Loading              binding.frameLayout.isVisible = isLoading or isError             binding.progress.isVisible = isLoading             binding.errorText.isVisible = isError             binding.definitionsLayout.isVisible = isSuccess             binding.wordText.isVisible = isSuccess              when (dictResult) {                 is DictResultUi.Error -> {                     dictResult.text(binding.errorText)                 }                 is DictResultUi.Success -> {                     dictResult.word(binding.wordText)                     dictResult.definitions(binding.definitionsLayout)                 }                 else -> {}             }          }          val debounce = Debounce(Handler(Looper.getMainLooper()))         val runnable = Runnable { viewModel.searchWordDefinition(binding.searchEdit.text.toString()) }         binding.searchEdit.onTextChange { debounce.run(runnable) }         binding.searchBox.setEndIconOnClickListener { runnable.run() }     }  }<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u043c <code>ViewModel<\/code>, \u043f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c\u0441\u044f \u043d\u0430 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 <code>LiveData<\/code>  \u0438 \u0434\u0435\u043b\u0430\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441, \u043a\u043e\u0433\u0434\u0430 \u043d\u0430\u0431\u0438\u0440\u0430\u0435\u043c \u0442\u0435\u043a\u0441\u0442 \u0438\u043b\u0438 \u043d\u0430\u0436\u0438\u043c\u0430\u0435\u043c \u043d\u0430 \u043a\u043d\u043e\u043f\u043a\u0443 \u043f\u043e\u0438\u0441\u043a\u0430.<\/p>\n<p>\u041a\u043b\u0430\u0441\u0441 <code>Debounce<\/code> \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"kotlin\">class Debounce(private val handler: Handler) {      fun run(runnable: Runnable, delay: Long = 500) {<\/code><\/pre>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-326850","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/326850","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=326850"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/326850\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=326850"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=326850"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=326850"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}