{"id":463300,"date":"2025-06-13T15:00:47","date_gmt":"2025-06-13T15:00:47","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=463300"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=463300","title":{"rendered":"<span>\u041a\u0430\u043a \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u0435\u0442\u0435\u0432\u044b\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0432 Android: OkHttp \u0438 Retrofit \u0441 Coroutines<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0421\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 Android-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043f\u043e\u0447\u0442\u0438 \u0432\u0441\u0435\u0433\u0434\u0430 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0443\u044e\u0442 \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u043c: \u043f\u043e\u043b\u0443\u0447\u0430\u044e\u0442 \u0434\u0430\u043d\u043d\u044b\u0435, \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u044e\u0442 \u0444\u043e\u0440\u043c\u044b, \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0438\u0440\u0443\u044e\u0442 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u043c \u0432\u0440\u0435\u043c\u0435\u043d\u0438. \u041f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u044d\u0442\u0438\u0445 \u0441\u0435\u0442\u0435\u0432\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0432\u043b\u0438\u044f\u0435\u0442 \u043d\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0439 \u043e\u043f\u044b\u0442 \u2014 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0432 \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u0445 \u043d\u0435\u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u0433\u043e \u0438\u043b\u0438 \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0430.<\/p>\n<p>\u0420\u0430\u0441\u0441\u043a\u0430\u0436\u0443, \u043a\u0430\u043a \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 <strong>OkHttp<\/strong> \u0438 <strong>Retrofit<\/strong> \u2014 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b, \u0441\u0442\u0430\u0432\u0448\u0438\u0435 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043e\u043c \u0434\u0435-\u0444\u0430\u043a\u0442\u043e \u0432 Android-\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435, \u2014 \u0434\u043b\u044f \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u043d\u0430\u0434\u0451\u0436\u043d\u043e\u0439 \u0438 \u0431\u044b\u0441\u0442\u0440\u043e\u0439 \u0441\u0435\u0442\u0435\u0432\u043e\u0439 \u043f\u0440\u043e\u0441\u043b\u043e\u0439\u043a\u0438. \u0421\u0442\u0430\u0442\u044c\u044f \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0430 \u043d\u0430 \u043c\u043e\u0431\u0438\u043b\u044c\u043d\u044b\u0445 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432, \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0442\u0435\u0445, \u043a\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441 Kotlin \u0438 Coroutines.<\/p>\n<h4>\u041f\u043e\u0447\u0435\u043c\u0443 \u0432\u0441\u0451 \u0435\u0449\u0451 OkHttp \u0438 Retrofit?<\/h4>\n<p>\u041d\u0435\u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0435 \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, Ktor), <strong>\u0441\u0432\u044f\u0437\u043a\u0430 OkHttp + Retrofit + Coroutines \u043e\u0441\u0442\u0430\u0451\u0442\u0441\u044f \u0441\u0430\u043c\u043e\u0439 \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u0439 \u0438 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0443\u0435\u043c\u043e\u0439<\/strong>. \u041f\u0440\u0438\u0447\u0438\u043d\u044b \u043f\u0440\u043e\u0441\u0442\u044b:<\/p>\n<ul>\n<li>\n<p><strong>OkHttp<\/strong> \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442 \u043d\u0438\u0437\u043a\u043e\u0443\u0440\u043e\u0432\u043d\u0435\u0432\u0443\u044e \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 HTTP: \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f\u043c\u0438, \u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435, \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0447\u0438\u043a\u0438, \u0442\u0430\u0439\u043c\u0430\u0443\u0442\u044b.<\/p>\n<\/li>\n<li>\n<p><strong>Retrofit<\/strong> \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u0442 API \u0432 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0441 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044f\u043c\u0438, \u0430 \u0441 Coroutines \u043e\u043d \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0438\u0441\u0430\u0442\u044c \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u043a\u043e\u0434 \u043f\u043e\u0447\u0442\u0438 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e.<\/p>\n<\/li>\n<li>\n<p>\u0412\u0441\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u0430\u043a\u0442\u0438\u0432\u043d\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u044e\u0442\u0441\u044f, \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u044b \u0441 AndroidX, Jetpack \u0438 \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u043c\u0438 JSON-\u043f\u0430\u0440\u0441\u0435\u0440\u0430\u043c\u0438 (Moshi, Gson, Kotlinx.serialization).<\/p>\n<p>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u0438 \u0431\u0430\u0437\u043e\u0432\u0430\u044f \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430<\/p>\n<p>\u0412 <code>build.gradle<\/code> \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c:<\/p>\n<p><code>implementation(\"com.squareup.retrofit2:retrofit:2.9.0\")<br \/> implementation(\"com.squareup.okhttp3:okhttp:4.12.0\")<br \/> implementation(\"com.squareup.retrofit2:converter-moshi:2.9.0\")<\/code><\/p>\n<\/li>\n<\/ul>\n<p> \u0418 \u0435\u0441\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 Coroutines:  <\/p>\n<p><code>implementation(\"org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0\")<\/code><\/p>\n<p> \u0414\u043b\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0442\u044b \u0441\u043e\u0437\u0434\u0430\u0451\u043c <code>OkHttpClient<\/code> \u0441 \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c:  <\/p>\n<p><code>val client = OkHttpClient.Builder()<br \/> .addInterceptor(HttpLoggingInterceptor().apply {<br \/> level = HttpLoggingInterceptor.Level.BODY<br \/> })<br \/> .build()<\/code><\/p>\n<p> \u0418 \u043d\u0430 \u0435\u0433\u043e \u043e\u0441\u043d\u043e\u0432\u0435 \u2014 Retrofit:  <\/p>\n<p><code>val retrofit = Retrofit.Builder()<br \/> .baseUrl(\"<\/code><a href=\"https:\/\/api.example.com\/&quot;)%EF%BF%BC\" rel=\"noopener noreferrer nofollow\"><code>https:\/\/api.example.com\/\")<br \/><\/code><\/a><code> .addConverterFactory(MoshiConverterFactory.create())<br \/> .client(client)<br \/> .build()<\/code><\/p>\n<h4>API-\u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0441 Coroutines<\/h4>\n<p>Retrofit \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043a\u0430\u043a \u043e\u0431\u044b\u0447\u043d\u044b\u0435 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u044b:<\/p>\n<p><code>interface ApiService {<br \/> <a class=\"mention\" href=\"\/users\/GET\">@GET<\/a>(\"users\/{id}\")<br \/> suspend fun getUser<a class=\"mention\" href=\"\/users\/Path\">@Path<\/a>h(\"id\") userId: Int): User<br \/> }<\/code><\/p>\n<p> \u0412\u0430\u0436\u043d\u043e: \u043c\u0435\u0442\u043e\u0434 <code>suspend<\/code> \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043e\u043d \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e, \u0431\u0435\u0437 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438 \u043f\u043e\u0442\u043e\u043a\u0430. \u042d\u0442\u043e \u043a\u043b\u044e\u0447\u0435\u0432\u0430\u044f \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044c Coroutines, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0434\u0435\u043b\u0430\u0435\u0442 \u0438\u0445 \u0443\u0434\u043e\u0431\u043d\u044b\u043c\u0438 \u0434\u043b\u044f \u0441\u0435\u0442\u0435\u0432\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439.  <\/p>\n<h4>\u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e OkHttp<\/h4>\n<h3>1. \u041a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435<\/h3>\n<p>\u0414\u0430\u0436\u0435 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u043d\u044b\u0435 API \u043c\u043e\u0436\u043d\u043e \u0447\u0430\u0441\u0442\u0438\u0447\u043d\u043e \u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0435\u0441\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0438 <code>Cache-Control<\/code>. \u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<p><code>val cache = Cache(File(context.cacheDir, \"http_cache\"), 10L <em> 1024 <\/em> 1024)<\/code><\/p>\n<p><code>val client = OkHttpClient.Builder()<br \/> .cache(cache)<br \/> .addNetworkInterceptor { chain -&gt;<br \/> val response = chain.proceed(chain.request())<br \/> response.newBuilder()<br \/> .header(\"Cache-Control\", \"public, max-age=60\")<br \/> .build()<br \/> }<br \/> .build()<\/code><\/p>\n<h3>2. \u0421\u0436\u0430\u0442\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445<\/h3>\n<p>OkHttp \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 <code>gzip<\/code>, \u043d\u043e \u043c\u043e\u0436\u043d\u043e \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0438 \u0432\u0440\u0443\u0447\u043d\u0443\u044e, \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0435\u0441\u043b\u0438 API \u044d\u0442\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442.<\/p>\n<h4>\u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043e\u0448\u0438\u0431\u043e\u043a \u0438 \u0443\u0441\u0442\u043e\u0439\u0447\u0438\u0432\u043e\u0441\u0442\u044c<\/h4>\n<p>\u041f\u0440\u0438 \u043d\u0435\u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u043c \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0438 \u0432\u0430\u0436\u043d\u043e \u0433\u0440\u0430\u043c\u043e\u0442\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0438. \u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<p><code>try {<br \/> val user = api.getUser(1)<br \/> } catch (e: IOException) {<br \/> \/\/ \u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0441 \u0441\u0435\u0442\u044c\u044e<br \/> } catch (e: HttpException) {<br \/> \/\/ \u0421\u0435\u0440\u0432\u0435\u0440 \u0432\u0435\u0440\u043d\u0443\u043b \u043e\u0448\u0438\u0431\u043a\u0443<br \/> }<\/code><\/p>\n<p> \u041c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u0451\u0440\u0442\u043a\u0438 <code>Result<\/code>, <code>sealed class<\/code> \u0438\u043b\u0438 Flow \u0434\u043b\u044f \u0431\u043e\u043b\u0435\u0435 \u044d\u043b\u0435\u0433\u0430\u043d\u0442\u043d\u043e\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438.  <\/p>\n<h4>\u041f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u044b\u0435 \u0438 \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u044b\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b<\/h4>\n<p>\u0415\u0441\u043b\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u0434\u0430\u043d\u043d\u044b\u0445 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 <code>coroutineScope {}<\/code> \u0438\u043b\u0438 <code>async {}<\/code>:<\/p>\n<p><code>coroutineScope {<br \/> val userDeferred = async { api.getUser(1) }<br \/> val postsDeferred = async { api.getPosts(1) }<br \/> val user = userDeferred.await()<br \/> val posts = postsDeferred.await()<br \/> }<\/code><\/p>\n<p> \u041f\u043b\u044e\u0441 \u2014 \u043d\u0438\u043a\u0430\u043a\u0438\u0445 callback-\u0430\u0434\u043e\u0432 \u0438 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0441\u0442\u0438.  <\/p>\n<h4>\u0422\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435: MockWebServer<\/h4>\n<p>\u0414\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0431\u0435\u0437 \u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0435\u0433\u043e \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <code>MockWebServer<\/code> \u043e\u0442 Square:<\/p>\n<p><code>val server = MockWebServer()<br \/> server.enqueue(MockResponse().setBody(\"{ \"id\": 1, \"name\": \"Ivan\" }\"))<\/code><\/p>\n<p> \u042d\u0442\u043e \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u043e \u043f\u0440\u0438 CI\/CD \u0438\u043b\u0438 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u0440\u0435\u0434\u043a\u0438\u0445 \u043e\u0448\u0438\u0431\u043e\u043a (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, 500 \u043e\u0442 API).  <\/p>\n<p>\u0412 2025 \u0433\u043e\u0434\u0443 <strong>\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0435\u0442\u0435\u0432\u043e\u0433\u043e \u0441\u043b\u043e\u044f \u043a\u0440\u0438\u0442\u0438\u0447\u043d\u0430 \u0434\u043b\u044f \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438, \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u0438 \u0438 \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u0441\u0442\u0438<\/strong> \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439. \u041e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0435\u0441\u043b\u0438:<\/p>\n<ul>\n<li>\n<p>\u0432\u044b \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442\u0435 \u0441 \u043e\u0442\u0435\u0447\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c\u0438 API \u0438\u043b\u0438 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u0441\u0435\u0440\u0432\u0438\u0441\u0430\u043c\u0438;<\/p>\n<\/li>\n<li>\n<p>\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0438 \u043d\u0430\u0445\u043e\u0434\u044f\u0442\u0441\u044f \u0432 \u0440\u0435\u0433\u0438\u043e\u043d\u0430\u0445 \u0441 \u043d\u0435\u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u044b\u043c \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435\u043c;<\/p>\n<\/li>\n<li>\n<p>\u0432\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0443\u0435\u0442 \u0441 \u0418\u0418-\u0441\u0435\u0440\u0432\u0438\u0441\u0430\u043c\u0438 \u0438\u043b\u0438 \u043f\u043e\u0442\u043e\u043a\u043e\u0432\u044b\u043c\u0438 API, \u0433\u0434\u0435 \u0432\u0430\u0436\u043d\u0430 \u0440\u0435\u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c.<\/p>\n<\/li>\n<\/ul>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 OkHttp \u0438 Retrofit \u043f\u043e-\u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443 \u043e\u0441\u0442\u0430\u0451\u0442\u0441\u044f \u0440\u0430\u0437\u0443\u043c\u043d\u044b\u043c \u0432\u044b\u0431\u043e\u0440\u043e\u043c, \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0432 \u0441\u043e\u0447\u0435\u0442\u0430\u043d\u0438\u0438 \u0441 \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u043e\u043c \u2014 Kotlin Coroutines, DI (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, Hilt), Flow \u0438 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u043e\u0439 Clean Architecture.<\/p>\n<p>\u0413\u0440\u0430\u043c\u043e\u0442\u043d\u043e \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0439 \u0441\u0435\u0442\u0435\u0432\u043e\u0439 \u0441\u043b\u043e\u0439 \u2014 \u044d\u0442\u043e \u043d\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u043f\u043e\u0441\u043e\u0431 \u00ab\u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u0430\u00bb. \u042d\u0442\u043e <strong>\u0432\u0430\u0436\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 UX<\/strong>, \u043e\u0442 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u0442, \u0431\u0443\u0434\u0435\u0442 \u043b\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0431\u044b\u0441\u0442\u0440\u044b\u043c, \u043d\u0430\u0434\u0451\u0436\u043d\u044b\u043c \u0438 \u043e\u0442\u0437\u044b\u0432\u0447\u0438\u0432\u044b\u043c.<\/p>\n<p>Retrofit \u0438 OkHttp \u2014 \u043f\u0440\u043e\u0432\u0435\u0440\u0435\u043d\u043d\u044b\u0435 \u0432\u0440\u0435\u043c\u0435\u043d\u0435\u043c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b, \u0438, \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0438\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438, \u0432\u044b \u0441\u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u043d\u0430 \u043f\u0440\u043e\u0434\u0435 \u0438 \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c \u043e\u043f\u044b\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0443\u0436\u0435 \u043d\u0430 \u044d\u0442\u0430\u043f\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/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\/articles\/918146\/\"> https:\/\/habr.com\/ru\/articles\/918146\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0421\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 Android-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043f\u043e\u0447\u0442\u0438 \u0432\u0441\u0435\u0433\u0434\u0430 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0443\u044e\u0442 \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u043c: \u043f\u043e\u043b\u0443\u0447\u0430\u044e\u0442 \u0434\u0430\u043d\u043d\u044b\u0435, \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u044e\u0442 \u0444\u043e\u0440\u043c\u044b, \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0438\u0440\u0443\u044e\u0442 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u043c \u0432\u0440\u0435\u043c\u0435\u043d\u0438. \u041f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u044d\u0442\u0438\u0445 \u0441\u0435\u0442\u0435\u0432\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0432\u043b\u0438\u044f\u0435\u0442 \u043d\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0439 \u043e\u043f\u044b\u0442 \u2014 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0432 \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u0445 \u043d\u0435\u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u0433\u043e \u0438\u043b\u0438 \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0430.<\/p>\n<p>\u0420\u0430\u0441\u0441\u043a\u0430\u0436\u0443, \u043a\u0430\u043a \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 <strong>OkHttp<\/strong> \u0438 <strong>Retrofit<\/strong> \u2014 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b, \u0441\u0442\u0430\u0432\u0448\u0438\u0435 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043e\u043c \u0434\u0435-\u0444\u0430\u043a\u0442\u043e \u0432 Android-\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435, \u2014 \u0434\u043b\u044f \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u043d\u0430\u0434\u0451\u0436\u043d\u043e\u0439 \u0438 \u0431\u044b\u0441\u0442\u0440\u043e\u0439 \u0441\u0435\u0442\u0435\u0432\u043e\u0439 \u043f\u0440\u043e\u0441\u043b\u043e\u0439\u043a\u0438. \u0421\u0442\u0430\u0442\u044c\u044f \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0430 \u043d\u0430 \u043c\u043e\u0431\u0438\u043b\u044c\u043d\u044b\u0445 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432, \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0442\u0435\u0445, \u043a\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441 Kotlin \u0438 Coroutines.<\/p>\n<h4>\u041f\u043e\u0447\u0435\u043c\u0443 \u0432\u0441\u0451 \u0435\u0449\u0451 OkHttp \u0438 Retrofit?<\/h4>\n<p>\u041d\u0435\u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0435 \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, Ktor), <strong>\u0441\u0432\u044f\u0437\u043a\u0430 OkHttp + Retrofit + Coroutines \u043e\u0441\u0442\u0430\u0451\u0442\u0441\u044f \u0441\u0430\u043c\u043e\u0439 \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u0439 \u0438 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0443\u0435\u043c\u043e\u0439<\/strong>. \u041f\u0440\u0438\u0447\u0438\u043d\u044b \u043f\u0440\u043e\u0441\u0442\u044b:<\/p>\n<ul>\n<li>\n<p><strong>OkHttp<\/strong> \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442 \u043d\u0438\u0437\u043a\u043e\u0443\u0440\u043e\u0432\u043d\u0435\u0432\u0443\u044e \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 HTTP: \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f\u043c\u0438, \u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435, \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0447\u0438\u043a\u0438, \u0442\u0430\u0439\u043c\u0430\u0443\u0442\u044b.<\/p>\n<\/li>\n<li>\n<p><strong>Retrofit<\/strong> \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u0442 API \u0432 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0441 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044f\u043c\u0438, \u0430 \u0441 Coroutines \u043e\u043d \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0438\u0441\u0430\u0442\u044c \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u043a\u043e\u0434 \u043f\u043e\u0447\u0442\u0438 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e.<\/p>\n<\/li>\n<li>\n<p>\u0412\u0441\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u0430\u043a\u0442\u0438\u0432\u043d\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u044e\u0442\u0441\u044f, \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u044b \u0441 AndroidX, Jetpack \u0438 \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u043c\u0438 JSON-\u043f\u0430\u0440\u0441\u0435\u0440\u0430\u043c\u0438 (Moshi, Gson, Kotlinx.serialization).<\/p>\n<p>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u0438 \u0431\u0430\u0437\u043e\u0432\u0430\u044f \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430<\/p>\n<p>\u0412 <code>build.gradle<\/code> \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c:<\/p>\n<p><code>implementation(\"com.squareup.retrofit2:retrofit:2.9.0\")<br \/> implementation(\"com.squareup.okhttp3:okhttp:4.12.0\")<br \/> implementation(\"com.squareup.retrofit2:converter-moshi:2.9.0\")<\/code><\/p>\n<\/li>\n<\/ul>\n<p> \u0418 \u0435\u0441\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 Coroutines:  <\/p>\n<p><code>implementation(\"org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0\")<\/code><\/p>\n<p> \u0414\u043b\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0442\u044b \u0441\u043e\u0437\u0434\u0430\u0451\u043c <code>OkHttpClient<\/code> \u0441 \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c:  <\/p>\n<p><code>val client = OkHttpClient.Builder()<br \/> .addInterceptor(HttpLoggingInterceptor().apply {<br \/> level = HttpLoggingInterceptor.Level.BODY<br \/> })<br \/> .build()<\/code><\/p>\n<p> \u0418 \u043d\u0430 \u0435\u0433\u043e \u043e\u0441\u043d\u043e\u0432\u0435 \u2014 Retrofit:  <\/p>\n<p><code>val retrofit = Retrofit.Builder()<br \/> .baseUrl(\"<\/code><a href=\"https:\/\/api.example.com\/&quot;)%EF%BF%BC\" rel=\"noopener noreferrer nofollow\"><code>https:\/\/api.example.com\/\")<br \/><\/code><\/a><code> .addConverterFactory(MoshiConverterFactory.create())<br \/> .client(client)<br \/> .build()<\/code><\/p>\n<h4>API-\u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0441 Coroutines<\/h4>\n<p>Retrofit \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043a\u0430\u043a \u043e\u0431\u044b\u0447\u043d\u044b\u0435 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u044b:<\/p>\n<p><code>interface ApiService {<br \/> <a class=\"mention\" href=\"\/users\/GET\">@GET<\/a>(\"users\/{id}\")<br \/> suspend fun getUser<a class=\"mention\" href=\"\/users\/Path\">@Path<\/a>h(\"id\") userId: Int): User<br \/> }<\/code><\/p>\n<p> \u0412\u0430\u0436\u043d\u043e: \u043c\u0435\u0442\u043e\u0434 <code>suspend<\/code> \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043e\u043d \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e, \u0431\u0435\u0437 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438 \u043f\u043e\u0442\u043e\u043a\u0430. \u042d\u0442\u043e \u043a\u043b\u044e\u0447\u0435\u0432\u0430\u044f \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044c Coroutines, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0434\u0435\u043b\u0430\u0435\u0442 \u0438\u0445 \u0443\u0434\u043e\u0431\u043d\u044b\u043c\u0438 \u0434\u043b\u044f \u0441\u0435\u0442\u0435\u0432\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439.  <\/p>\n<h4>\u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e OkHttp<\/h4>\n<h3>1. \u041a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435<\/h3>\n<p>\u0414\u0430\u0436\u0435 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u043d\u044b\u0435 API \u043c\u043e\u0436\u043d\u043e \u0447\u0430\u0441\u0442\u0438\u0447\u043d\u043e \u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0435\u0441\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0438 <code>Cache-Control<\/code>. \u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<p><code>val cache = Cache(File(context.cacheDir, \"http_cache\"), 10L <em> 1024 <\/em> 1024)<\/code><\/p>\n<p><code>val client = OkHttpClient.Builder()<br \/> .cache(cache)<br \/> .addNetworkInterceptor { chain -&gt;<br \/> val response = chain.proceed(chain.request())<br \/> response.newBuilder()<br \/> .header(\"Cache-Control\", \"public, max-age=60\")<br \/> .build()<br \/> }<br \/> .build()<\/code><\/p>\n<h3>2. \u0421\u0436\u0430\u0442\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445<\/h3>\n<p>OkHttp \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 <code>gzip<\/code>, \u043d\u043e \u043c\u043e\u0436\u043d\u043e \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0438 \u0432\u0440\u0443\u0447\u043d\u0443\u044e, \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0435\u0441\u043b\u0438 API \u044d\u0442\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442.<\/p>\n<h4>\u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043e\u0448\u0438\u0431\u043e\u043a \u0438 \u0443\u0441\u0442\u043e\u0439\u0447\u0438\u0432\u043e\u0441\u0442\u044c<\/h4>\n<p>\u041f\u0440\u0438 \u043d\u0435\u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u043c \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0438 \u0432\u0430\u0436\u043d\u043e \u0433\u0440\u0430\u043c\u043e\u0442\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0438. \u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<p><code>try {<br \/> val user = api.getUser(1)<br \/> } catch (e: IOException) {<br \/> \/\/ \u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0441 \u0441\u0435\u0442\u044c\u044e<br \/> } catch (e: HttpException) {<br \/> \/\/ \u0421\u0435\u0440\u0432\u0435\u0440 \u0432\u0435\u0440\u043d\u0443\u043b \u043e\u0448\u0438\u0431\u043a\u0443<br \/> }<\/code><\/p>\n<p> \u041c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u0451\u0440\u0442\u043a\u0438 <code>Result<\/code>, <code>sealed class<\/code> \u0438\u043b\u0438 Flow \u0434\u043b\u044f \u0431\u043e\u043b\u0435\u0435 \u044d\u043b\u0435\u0433\u0430\u043d\u0442\u043d\u043e\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438.  <\/p>\n<h4>\u041f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u044b\u0435 \u0438 \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u044b\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b<\/h4>\n<p>\u0415\u0441\u043b\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u0434\u0430\u043d\u043d\u044b\u0445 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 <code>coroutineScope {}<\/code> \u0438\u043b\u0438 <code>async {}<\/code>:<\/p>\n<p><code>coroutineScope {<br \/> val userDeferred = async { api.getUser(1) }<br \/> val postsDeferred = async { api.getPosts(1) }<br \/> val user = userDeferred.await()<br \/> val posts = postsDeferred.await()<br \/> }<\/code><\/p>\n<p> \u041f\u043b\u044e\u0441 \u2014 \u043d\u0438\u043a\u0430\u043a\u0438\u0445 callback-\u0430\u0434\u043e\u0432 \u0438 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0441\u0442\u0438.  <\/p>\n<h4>\u0422\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435: MockWebServer<\/h4>\n<p>\u0414\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0431\u0435\u0437 \u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0435\u0433\u043e \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <code>MockWebServer<\/code> \u043e\u0442 Square:<\/p>\n<p><code>val server = MockWebServer()<br \/> server.enqueue(MockResponse().setBody(\"{ \"id\": 1, \"name\": \"Ivan\" }\"))<\/code><\/p>\n<p> \u042d\u0442\u043e \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u043e \u043f\u0440\u0438 CI\/CD \u0438\u043b\u0438 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u0440\u0435\u0434\u043a\u0438\u0445 \u043e\u0448\u0438\u0431\u043e\u043a (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, 500 \u043e\u0442 API).  <\/p>\n<p>\u0412 2025 \u0433\u043e\u0434\u0443 <strong>\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0435\u0442\u0435\u0432\u043e\u0433\u043e \u0441\u043b\u043e\u044f \u043a\u0440\u0438\u0442\u0438\u0447\u043d\u0430 \u0434\u043b\u044f \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438, \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u0438 \u0438 \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u0441\u0442\u0438<\/strong> \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439. \u041e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0435\u0441\u043b\u0438:<\/p>\n<ul>\n<li>\n<p>\u0432\u044b \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442\u0435 \u0441 \u043e\u0442\u0435\u0447\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c\u0438 API \u0438\u043b\u0438 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u0441\u0435\u0440\u0432\u0438\u0441\u0430\u043c\u0438;<\/p>\n<\/li>\n<li>\n<p>\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0438 \u043d\u0430\u0445\u043e\u0434\u044f\u0442\u0441\u044f \u0432 \u0440\u0435\u0433\u0438\u043e\u043d\u0430\u0445 \u0441 \u043d\u0435\u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u044b\u043c \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435\u043c;<\/p>\n<\/li>\n<li>\n<p>\u0432\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0443\u0435\u0442 \u0441 \u0418\u0418-\u0441\u0435\u0440\u0432\u0438\u0441\u0430\u043c\u0438 \u0438\u043b\u0438 \u043f\u043e\u0442\u043e\u043a\u043e\u0432\u044b\u043c\u0438 API, \u0433\u0434\u0435 \u0432\u0430\u0436\u043d\u0430 \u0440\u0435\u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c.<\/p>\n<\/li>\n<\/ul>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 OkHttp \u0438 Retrofit \u043f\u043e-\u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443 \u043e\u0441\u0442\u0430\u0451\u0442\u0441\u044f \u0440\u0430\u0437\u0443\u043c\u043d\u044b\u043c \u0432\u044b\u0431\u043e\u0440\u043e\u043c, \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0432 \u0441\u043e\u0447\u0435\u0442\u0430\u043d\u0438\u0438 \u0441 \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u043e\u043c \u2014 Kotlin Coroutines, DI (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, Hilt), Flow \u0438 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u043e\u0439 Clean Architecture.<\/p>\n<p>\u0413\u0440\u0430\u043c\u043e\u0442\u043d\u043e \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0439 \u0441\u0435\u0442\u0435\u0432\u043e\u0439 \u0441\u043b\u043e\u0439 \u2014 \u044d\u0442\u043e \u043d\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u043f\u043e\u0441\u043e\u0431 \u00ab\u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u0430\u00bb. \u042d\u0442\u043e <strong>\u0432\u0430\u0436\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 UX<\/strong>, \u043e\u0442 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u0442, \u0431\u0443\u0434\u0435\u0442 \u043b\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0431\u044b\u0441\u0442\u0440\u044b\u043c, \u043d\u0430\u0434\u0451\u0436\u043d\u044b\u043c \u0438 \u043e\u0442\u0437\u044b\u0432\u0447\u0438\u0432\u044b\u043c.<\/p>\n<p>Retrofit \u0438 OkHttp \u2014 \u043f\u0440\u043e\u0432\u0435\u0440\u0435\u043d\u043d\u044b\u0435 \u0432\u0440\u0435\u043c\u0435\u043d\u0435\u043c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b, \u0438, \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0438\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438, \u0432\u044b \u0441\u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u043d\u0430 \u043f\u0440\u043e\u0434\u0435 \u0438 \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c \u043e\u043f\u044b\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0443\u0436\u0435 \u043d\u0430 \u044d\u0442\u0430\u043f\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/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\/articles\/918146\/\"> https:\/\/habr.com\/ru\/articles\/918146\/<\/a><br \/><\/br><\/br><\/p>\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-463300","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/463300","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=463300"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/463300\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=463300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=463300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=463300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}