coroutine-request.html 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  1. <!doctype html>
  2. <html lang="zh" class="no-js">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width,initial-scale=1">
  6. <meta name="description" content="Net document">
  7. <meta name="author" content="劉強東">
  8. <link rel="prev" href="index.html">
  9. <link rel="next" href="scope.html">
  10. <link rel="icon" href="img/book-open.svg">
  11. <meta name="generator" content="mkdocs-1.6.1, mkdocs-material-9.5.48">
  12. <title>协程请求 - Net</title>
  13. <link rel="stylesheet" href="assets/stylesheets/main.6f8fc17f.min.css">
  14. <link rel="stylesheet" href="assets/stylesheets/palette.06af60db.min.css">
  15. <script src="https://unpkg.com/iframe-worker/shim"></script>
  16. <link rel="stylesheet" href="css/extra.css">
  17. <script>__md_scope=new URL(".",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script>
  18. </head>
  19. <body dir="ltr" data-md-color-scheme="default" data-md-color-primary="white" data-md-color-accent="indigo">
  20. <input class="md-toggle" data-md-toggle="drawer" type="checkbox" id="__drawer" autocomplete="off">
  21. <input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search" autocomplete="off">
  22. <label class="md-overlay" for="__drawer"></label>
  23. <div data-md-component="skip">
  24. </div>
  25. <div data-md-component="announce">
  26. </div>
  27. <header class="md-header md-header--shadow" data-md-component="header">
  28. <nav class="md-header__inner md-grid" aria-label="页眉">
  29. <a href="index.html" title="Net" class="md-header__button md-logo" aria-label="Net" data-md-component="logo">
  30. <img src="img/book-open.svg" alt="logo">
  31. </a>
  32. <label class="md-header__button md-icon" for="__drawer">
  33. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg>
  34. </label>
  35. <div class="md-header__title" data-md-component="header-title">
  36. <div class="md-header__ellipsis">
  37. <div class="md-header__topic">
  38. <span class="md-ellipsis">
  39. Net
  40. </span>
  41. </div>
  42. <div class="md-header__topic" data-md-component="header-topic">
  43. <span class="md-ellipsis">
  44. 协程请求
  45. </span>
  46. </div>
  47. </div>
  48. </div>
  49. <form class="md-header__option" data-md-component="palette">
  50. <input class="md-option" data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme="default" data-md-color-primary="white" data-md-color-accent="indigo" aria-hidden="true" type="radio" name="__palette" id="__palette_0">
  51. </form>
  52. <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script>
  53. <label class="md-header__button md-icon" for="__search">
  54. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5"/></svg>
  55. </label>
  56. <div class="md-search" data-md-component="search" role="dialog">
  57. <label class="md-search__overlay" for="__search"></label>
  58. <div class="md-search__inner" role="search">
  59. <form class="md-search__form" name="search">
  60. <input type="text" class="md-search__input" name="query" aria-label="搜索" placeholder="搜索" autocapitalize="off" autocorrect="off" autocomplete="off" spellcheck="false" data-md-component="search-query" required>
  61. <label class="md-search__icon md-icon" for="__search">
  62. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5"/></svg>
  63. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11z"/></svg>
  64. </label>
  65. <nav class="md-search__options" aria-label="查找">
  66. <a href="javascript:void(0)" class="md-search__icon md-icon" title="分享" aria-label="分享" data-clipboard data-clipboard-text="" data-md-component="search-share" tabindex="-1">
  67. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M18 16.08c-.76 0-1.44.3-1.96.77L8.91 12.7c.05-.23.09-.46.09-.7s-.04-.47-.09-.7l7.05-4.11c.54.5 1.25.81 2.04.81a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3c0 .24.04.47.09.7L8.04 9.81C7.5 9.31 6.79 9 6 9a3 3 0 0 0-3 3 3 3 0 0 0 3 3c.79 0 1.5-.31 2.04-.81l7.12 4.15c-.05.21-.08.43-.08.66 0 1.61 1.31 2.91 2.92 2.91s2.92-1.3 2.92-2.91A2.92 2.92 0 0 0 18 16.08"/></svg>
  68. </a>
  69. <button type="reset" class="md-search__icon md-icon" title="清空当前内容" aria-label="清空当前内容" tabindex="-1">
  70. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M19 6.41 17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z"/></svg>
  71. </button>
  72. </nav>
  73. <div class="md-search__suggest" data-md-component="search-suggest"></div>
  74. </form>
  75. <div class="md-search__output">
  76. <div class="md-search__scrollwrap" tabindex="0" data-md-scrollfix>
  77. <div class="md-search-result" data-md-component="search-result">
  78. <div class="md-search-result__meta">
  79. 正在初始化搜索引擎
  80. </div>
  81. <ol class="md-search-result__list" role="presentation"></ol>
  82. </div>
  83. </div>
  84. </div>
  85. </div>
  86. </div>
  87. <div class="md-header__source">
  88. <a href="https://github.com/liangjingkanji/Net" title="前往仓库" class="md-source" data-md-component="source">
  89. <div class="md-source__icon md-icon">
  90. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><!--! Font Awesome Free 6.7.1 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2024 Fonticons, Inc.--><path d="M439.55 236.05 244 40.45a28.87 28.87 0 0 0-40.81 0l-40.66 40.63 51.52 51.52c27.06-9.14 52.68 16.77 43.39 43.68l49.66 49.66c34.23-11.8 61.18 31 35.47 56.69-26.49 26.49-70.21-2.87-56-37.34L240.22 199v121.85c25.3 12.54 22.26 41.85 9.08 55a34.34 34.34 0 0 1-48.55 0c-17.57-17.6-11.07-46.91 11.25-56v-123c-20.8-8.51-24.6-30.74-18.64-45L142.57 101 8.45 235.14a28.86 28.86 0 0 0 0 40.81l195.61 195.6a28.86 28.86 0 0 0 40.8 0l194.69-194.69a28.86 28.86 0 0 0 0-40.81"/></svg>
  91. </div>
  92. <div class="md-source__repository">
  93. GitHub
  94. </div>
  95. </a>
  96. </div>
  97. </nav>
  98. </header>
  99. <div class="md-container" data-md-component="container">
  100. <main class="md-main" data-md-component="main">
  101. <div class="md-main__inner md-grid">
  102. <div class="md-sidebar md-sidebar--primary" data-md-component="sidebar" data-md-type="navigation" >
  103. <div class="md-sidebar__scrollwrap">
  104. <div class="md-sidebar__inner">
  105. <nav class="md-nav md-nav--primary" aria-label="导航栏" data-md-level="0">
  106. <label class="md-nav__title" for="__drawer">
  107. <a href="index.html" title="Net" class="md-nav__button md-logo" aria-label="Net" data-md-component="logo">
  108. <img src="img/book-open.svg" alt="logo">
  109. </a>
  110. Net
  111. </label>
  112. <div class="md-nav__source">
  113. <a href="https://github.com/liangjingkanji/Net" title="前往仓库" class="md-source" data-md-component="source">
  114. <div class="md-source__icon md-icon">
  115. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><!--! Font Awesome Free 6.7.1 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2024 Fonticons, Inc.--><path d="M439.55 236.05 244 40.45a28.87 28.87 0 0 0-40.81 0l-40.66 40.63 51.52 51.52c27.06-9.14 52.68 16.77 43.39 43.68l49.66 49.66c34.23-11.8 61.18 31 35.47 56.69-26.49 26.49-70.21-2.87-56-37.34L240.22 199v121.85c25.3 12.54 22.26 41.85 9.08 55a34.34 34.34 0 0 1-48.55 0c-17.57-17.6-11.07-46.91 11.25-56v-123c-20.8-8.51-24.6-30.74-18.64-45L142.57 101 8.45 235.14a28.86 28.86 0 0 0 0 40.81l195.61 195.6a28.86 28.86 0 0 0 40.8 0l194.69-194.69a28.86 28.86 0 0 0 0-40.81"/></svg>
  116. </div>
  117. <div class="md-source__repository">
  118. GitHub
  119. </div>
  120. </a>
  121. </div>
  122. <ul class="md-nav__list" data-md-scrollfix>
  123. <li class="md-nav__item">
  124. <a href="index.html" class="md-nav__link">
  125. <span class="md-ellipsis">
  126. 使用
  127. </span>
  128. </a>
  129. </li>
  130. <li class="md-nav__item md-nav__item--active">
  131. <input class="md-nav__toggle md-toggle" type="checkbox" id="__toc">
  132. <a href="coroutine-request.html" class="md-nav__link md-nav__link--active">
  133. <span class="md-ellipsis">
  134. 协程请求
  135. </span>
  136. </a>
  137. </li>
  138. <li class="md-nav__item">
  139. <a href="scope.html" class="md-nav__link">
  140. <span class="md-ellipsis">
  141. 作用域
  142. </span>
  143. </a>
  144. </li>
  145. <li class="md-nav__item">
  146. <a href="sync-request.html" class="md-nav__link">
  147. <span class="md-ellipsis">
  148. 同步请求
  149. </span>
  150. </a>
  151. </li>
  152. <li class="md-nav__item">
  153. <a href="request.html" class="md-nav__link">
  154. <span class="md-ellipsis">
  155. 请求参数
  156. </span>
  157. </a>
  158. </li>
  159. <li class="md-nav__item">
  160. <a href="config.html" class="md-nav__link">
  161. <span class="md-ellipsis">
  162. 全局配置
  163. </span>
  164. </a>
  165. </li>
  166. <li class="md-nav__item md-nav__item--pruned md-nav__item--nested">
  167. <a href="converter.html" class="md-nav__link">
  168. <span class="md-ellipsis">
  169. 请求结果
  170. </span>
  171. <span class="md-nav__icon md-icon"></span>
  172. </a>
  173. </li>
  174. <li class="md-nav__item md-nav__item--pruned md-nav__item--nested">
  175. <a href="auto-dialog.html" class="md-nav__link">
  176. <span class="md-ellipsis">
  177. 自动化
  178. </span>
  179. <span class="md-nav__icon md-icon"></span>
  180. </a>
  181. </li>
  182. <li class="md-nav__item">
  183. <a href="thread.html" class="md-nav__link">
  184. <span class="md-ellipsis">
  185. 切换线程
  186. </span>
  187. </a>
  188. </li>
  189. <li class="md-nav__item">
  190. <a href="view-model.html" class="md-nav__link">
  191. <span class="md-ellipsis">
  192. ViewModel
  193. </span>
  194. </a>
  195. </li>
  196. <li class="md-nav__item">
  197. <a href="track.html" class="md-nav__link">
  198. <span class="md-ellipsis">
  199. 异常追踪
  200. </span>
  201. </a>
  202. </li>
  203. <li class="md-nav__item md-nav__item--pruned md-nav__item--nested">
  204. <a href="error.html" class="md-nav__link">
  205. <span class="md-ellipsis">
  206. 错误处理
  207. </span>
  208. <span class="md-nav__icon md-icon"></span>
  209. </a>
  210. </li>
  211. <li class="md-nav__item">
  212. <a href="okhttp-client.html" class="md-nav__link">
  213. <span class="md-ellipsis">
  214. OkHttpClient
  215. </span>
  216. </a>
  217. </li>
  218. <li class="md-nav__item">
  219. <a href="interceptor.html" class="md-nav__link">
  220. <span class="md-ellipsis">
  221. 拦截器
  222. </span>
  223. </a>
  224. </li>
  225. <li class="md-nav__item">
  226. <a href="https.html" class="md-nav__link">
  227. <span class="md-ellipsis">
  228. Https证书
  229. </span>
  230. </a>
  231. </li>
  232. <li class="md-nav__item">
  233. <a href="cookie.html" class="md-nav__link">
  234. <span class="md-ellipsis">
  235. Cookie
  236. </span>
  237. </a>
  238. </li>
  239. <li class="md-nav__item">
  240. <a href="cache.html" class="md-nav__link">
  241. <span class="md-ellipsis">
  242. 缓存
  243. </span>
  244. </a>
  245. </li>
  246. <li class="md-nav__item">
  247. <a href="tag.html" class="md-nav__link">
  248. <span class="md-ellipsis">
  249. 标签/额外
  250. </span>
  251. </a>
  252. </li>
  253. <li class="md-nav__item">
  254. <a href="upload-file.html" class="md-nav__link">
  255. <span class="md-ellipsis">
  256. 上传文件
  257. </span>
  258. </a>
  259. </li>
  260. <li class="md-nav__item">
  261. <a href="download-file.html" class="md-nav__link">
  262. <span class="md-ellipsis">
  263. 下载文件
  264. </span>
  265. </a>
  266. </li>
  267. <li class="md-nav__item">
  268. <a href="progress.html" class="md-nav__link">
  269. <span class="md-ellipsis">
  270. 进度监听
  271. </span>
  272. </a>
  273. </li>
  274. <li class="md-nav__item">
  275. <a href="cancel.html" class="md-nav__link">
  276. <span class="md-ellipsis">
  277. 取消请求
  278. </span>
  279. </a>
  280. </li>
  281. <li class="md-nav__item">
  282. <a href="repeat-request.html" class="md-nav__link">
  283. <span class="md-ellipsis">
  284. 重复请求
  285. </span>
  286. </a>
  287. </li>
  288. <li class="md-nav__item">
  289. <a href="debounce.html" class="md-nav__link">
  290. <span class="md-ellipsis">
  291. 搜索节流
  292. </span>
  293. </a>
  294. </li>
  295. <li class="md-nav__item">
  296. <a href="fastest.html" class="md-nav__link">
  297. <span class="md-ellipsis">
  298. 最快请求结果
  299. </span>
  300. </a>
  301. </li>
  302. <li class="md-nav__item">
  303. <a href="log-recorder.html" class="md-nav__link">
  304. <span class="md-ellipsis">
  305. 日志插件
  306. </span>
  307. </a>
  308. </li>
  309. <li class="md-nav__item">
  310. <a href="log-notice.html" class="md-nav__link">
  311. <span class="md-ellipsis">
  312. 日志通知
  313. </span>
  314. </a>
  315. </li>
  316. <li class="md-nav__item">
  317. <a href="timing.html" class="md-nav__link">
  318. <span class="md-ellipsis">
  319. 限时/定时请求
  320. </span>
  321. </a>
  322. </li>
  323. <li class="md-nav__item">
  324. <a href="callback.html" class="md-nav__link">
  325. <span class="md-ellipsis">
  326. Callback
  327. </span>
  328. </a>
  329. </li>
  330. <li class="md-nav__item">
  331. <a href="interval.html" class="md-nav__link">
  332. <span class="md-ellipsis">
  333. 轮询器/倒计时
  334. </span>
  335. </a>
  336. </li>
  337. <li class="md-nav__item">
  338. <a href="https://github.com/liangjingkanji/Net/discussions" class="md-nav__link">
  339. <span class="md-ellipsis">
  340. 社区讨论
  341. </span>
  342. </a>
  343. </li>
  344. <li class="md-nav__item">
  345. <a href="issues.html" class="md-nav__link">
  346. <span class="md-ellipsis">
  347. 常见问题
  348. </span>
  349. </a>
  350. </li>
  351. <li class="md-nav__item">
  352. <a href="updates.html" class="md-nav__link">
  353. <span class="md-ellipsis">
  354. 更新日志
  355. </span>
  356. </a>
  357. </li>
  358. <li class="md-nav__item">
  359. <a href="api/index.html" class="md-nav__link">
  360. <span class="md-ellipsis">
  361. 3.x文档
  362. </span>
  363. </a>
  364. </li>
  365. </ul>
  366. </nav>
  367. </div>
  368. </div>
  369. </div>
  370. <div class="md-sidebar md-sidebar--secondary" data-md-component="sidebar" data-md-type="toc" >
  371. <div class="md-sidebar__scrollwrap">
  372. <div class="md-sidebar__inner">
  373. <nav class="md-nav md-nav--secondary" aria-label="目录">
  374. </nav>
  375. </div>
  376. </div>
  377. </div>
  378. <div class="md-content" data-md-component="content">
  379. <article class="md-content__inner md-typeset">
  380. <h1>协程请求</h1>
  381. <p>Net的协程作用域会自动处理协程生命周期</p>
  382. <p>在上章节已经介绍如何发起并发网络请求, 这里演示同时(并发)请求<code>一万次</code>, 然后取消全部</p>
  383. <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-0-1"><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="kd">val</span><span class="w"> </span><span class="nv">job</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">scopeNetLife</span><span class="w"> </span><span class="p">{</span>
  384. </span><span id="__span-0-2"><a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a><span class="w"> </span><span class="n">repeat</span><span class="p">(</span><span class="m">10000</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
  385. </span><span id="__span-0-3"><a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a><span class="w"> </span><span class="c1">// 这里将返回的数据显示在TextView上</span>
  386. </span><span id="__span-0-4"><a id="__codelineno-0-4" name="__codelineno-0-4" href="#__codelineno-0-4"></a><span class="w"> </span><span class="n">launch</span><span class="w"> </span><span class="p">{</span>
  387. </span><span id="__span-0-5"><a id="__codelineno-0-5" name="__codelineno-0-5" href="#__codelineno-0-5"></a><span class="w"> </span><span class="n">tv</span><span class="p">.</span><span class="na">text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Get</span><span class="o">&lt;</span><span class="kt">String</span><span class="o">&gt;</span><span class="p">(</span><span class="n">Api</span><span class="p">.</span><span class="na">PATH</span><span class="p">).</span><span class="na">await</span><span class="p">()</span>
  388. </span><span id="__span-0-6"><a id="__codelineno-0-6" name="__codelineno-0-6" href="#__codelineno-0-6"></a><span class="w"> </span><span class="p">}</span>
  389. </span><span id="__span-0-7"><a id="__codelineno-0-7" name="__codelineno-0-7" href="#__codelineno-0-7"></a><span class="w"> </span><span class="p">}</span>
  390. </span><span id="__span-0-8"><a id="__codelineno-0-8" name="__codelineno-0-8" href="#__codelineno-0-8"></a><span class="p">}</span>
  391. </span></code></pre></div>
  392. <p>等待五秒后取消请求
  393. <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-1-1"><a id="__codelineno-1-1" name="__codelineno-1-1" href="#__codelineno-1-1"></a><span class="n">thread</span><span class="w"> </span><span class="p">{</span>
  394. </span><span id="__span-1-2"><a id="__codelineno-1-2" name="__codelineno-1-2" href="#__codelineno-1-2"></a><span class="w"> </span><span class="n">Thread</span><span class="p">.</span><span class="na">sleep</span><span class="p">(</span><span class="m">5000</span><span class="p">)</span><span class="w"> </span><span class="c1">// 等待五秒</span>
  395. </span><span id="__span-1-3"><a id="__codelineno-1-3" name="__codelineno-1-3" href="#__codelineno-1-3"></a><span class="w"> </span><span class="n">job</span><span class="p">.</span><span class="na">cancel</span><span class="p">()</span>
  396. </span><span id="__span-1-4"><a id="__codelineno-1-4" name="__codelineno-1-4" href="#__codelineno-1-4"></a><span class="p">}</span>
  397. </span></code></pre></div></p>
  398. <p><br></p>
  399. <p>Net主要使用协程请求, 但也支持其他方式发起请求</p>
  400. <ul>
  401. <li><a href="sync-request.html">同步请求</a></li>
  402. <li><a href="callback.html">回调请求</a></li>
  403. <li>协程请求</li>
  404. </ul>
  405. </article>
  406. </div>
  407. <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script>
  408. </div>
  409. <button type="button" class="md-top md-icon" data-md-component="top" hidden>
  410. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg>
  411. 回到页面顶部
  412. </button>
  413. </main>
  414. <!-- Footer -->
  415. <footer class="md-footer">
  416. <!-- Link to previous and/or next page -->
  417. <nav class="md-footer__inner md-grid" aria-label="footer.title">
  418. <!-- Link to previous page -->
  419. <a href="index.html" class="md-footer__link md-footer__link--prev" rel="prev">
  420. <div class="md-footer__button md-icon">
  421. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11z"/></svg>
  422. </div>
  423. <div class="md-footer__title">
  424. <div class="md-ellipsis">
  425. <span class="md-footer__direction">
  426. 上一页
  427. </span>
  428. 使用
  429. </div>
  430. </div>
  431. </a>
  432. <!-- Link to next page -->
  433. <a href="scope.html" class="md-footer__link md-footer__link--next" rel="next">
  434. <div class="md-footer__title">
  435. <div class="md-ellipsis">
  436. <span class="md-footer__direction">
  437. 下一页
  438. </span>
  439. 作用域
  440. </div>
  441. </div>
  442. <div class="md-footer__button md-icon">
  443. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11z"/></svg>
  444. </div>
  445. </a>
  446. </nav>
  447. <!-- Further information -->
  448. <div class="md-footer-meta md-typeset">
  449. <div class="md-footer-meta__inner md-grid">
  450. <!-- Copyright and theme information -->
  451. <div class="md-footer-copyright">
  452. <a href="https://github.com/liangjingkanji" target="_blank" rel="noopener">
  453. <div class="md-footer-copyright__highlight">
  454. Copyright &copy; 2018 - 2023 劉強東
  455. </div>
  456. </a>
  457. </div>
  458. <!-- Social links -->
  459. <div class="md-social">
  460. <a href="https://github.com/liangjingkanji" target="_blank" rel="noopener" title="github.com" class="md-social__link">
  461. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 496 512"><!--! Font Awesome Free 6.7.1 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2024 Fonticons, Inc.--><path d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8M97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg>
  462. </a>
  463. <a href="https://raw.githubusercontent.com/liangjingkanji/liangjingkanji/master/img/group-qrcode.png" target="_blank" rel="noopener" title="raw.githubusercontent.com" class="md-social__link">
  464. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><!--! Font Awesome Free 6.7.1 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2024 Fonticons, Inc.--><path d="M433.754 420.445c-11.526 1.393-44.86-52.741-44.86-52.741 0 31.345-16.136 72.247-51.051 101.786 16.842 5.192 54.843 19.167 45.803 34.421-7.316 12.343-125.51 7.881-159.632 4.037-34.122 3.844-152.316 8.306-159.632-4.037-9.045-15.25 28.918-29.214 45.783-34.415-34.92-29.539-51.059-70.445-51.059-101.792 0 0-33.334 54.134-44.859 52.741-5.37-.65-12.424-29.644 9.347-99.704 10.261-33.024 21.995-60.478 40.144-105.779C60.683 98.063 108.982.006 224 0c113.737.006 163.156 96.133 160.264 214.963 18.118 45.223 29.912 72.85 40.144 105.778 21.768 70.06 14.716 99.053 9.346 99.704"/></svg>
  465. </a>
  466. <a href="https://github.com/liangjingkanji/Net/discussions" target="_blank" rel="noopener" title="github.com" class="md-social__link">
  467. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.7.1 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2024 Fonticons, Inc.--><path d="M391.17 103.47h-38.63v109.7h38.63ZM285 103h-38.63v109.75H285ZM120.83 0 24.31 91.42v329.16h115.83V512l96.53-91.42h77.25L487.69 256V0Zm328.24 237.75-77.22 73.12h-77.24l-67.6 64v-64h-86.87V36.58h308.93Z"/></svg>
  468. </a>
  469. </div>
  470. </div>
  471. </div>
  472. </footer>
  473. </div>
  474. <div class="md-dialog" data-md-component="dialog">
  475. <div class="md-dialog__inner md-typeset"></div>
  476. </div>
  477. <script id="__config" type="application/json">{"base": ".", "features": ["navigation.top", "navigation.prune", "navigation.footer", "navigation.instant", "search.highlight", "search.suggest", "search.share", "content.code.copy", "content.code.annotate"], "search": "assets/javascripts/workers/search.6ce7567c.min.js", "translations": {"clipboard.copied": "\u5df2\u590d\u5236", "clipboard.copy": "\u590d\u5236", "search.result.more.one": "\u5728\u8be5\u9875\u4e0a\u8fd8\u6709 1 \u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.more.other": "\u5728\u8be5\u9875\u4e0a\u8fd8\u6709 # \u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.none": "\u6ca1\u6709\u627e\u5230\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.one": "\u627e\u5230 1 \u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.other": "# \u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.placeholder": "\u952e\u5165\u4ee5\u5f00\u59cb\u641c\u7d22", "search.result.term.missing": "\u7f3a\u5c11", "select.version": "\u9009\u62e9\u5f53\u524d\u7248\u672c"}}</script>
  478. <script src="assets/javascripts/bundle.83f73b43.min.js"></script>
  479. </body>
  480. </html>