เพิ่ม Ktor และเลือกเอนจินตามแพลตฟอร์ม
เชื่อมเอนจิน OkHttp และ Darwin ให้แต่ละเป้าหมาย
เพิ่ม Ktor และเลือกเอนจินตามแพลตฟอร์ม เป็นบทเรียน Kotlin Multiplatform Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Kotlin Multiplatform Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Kotlin Multiplatform Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
One HTTP Client Everywhere
Ktor Client is a multiplatform HTTP library, so you write networking once in commonMain and it runs on Android and iOS alike.
Core Plus Engine
Ktor splits into a shared core and per-platform engines. The core gives the API, the engine does the actual socket work.
Add the Core Dependency
In your shared module you add ktor-client-core to commonMain so every target can build requests with the same API.
implementation("io.ktor:ktor-client-core:2.3.12")Android Wants OkHttp
For Android you pick the OkHttp engine, a battle-tested JVM client that Ktor wraps cleanly behind its shared API.
implementation("io.ktor:ktor-client-okhttp:2.3.12")iOS Wants Darwin
For iOS you pick the Darwin engine, which runs on Apple's native URLSession under the hood for true platform networking.
implementation("io.ktor:ktor-client-darwin:2.3.12")Engines Live in Source Sets
Each engine belongs in its own source set: OkHttp in androidMain and Darwin in iosMain, never in commonMain.
androidMain.dependencies { implementation(okhttp) }Create the Client Once
In commonMain you create an HttpClient with no engine argument, and Ktor auto-selects the one bundled for that platform.
val client = HttpClient()Auto Engine Selection
Because only one engine ships per target, Ktor's auto-selection finds it. Your shared code stays free of any platform import.
Or Pass an Engine
You can also pass an engine explicitly, which is handy when you want per-platform config like timeouts or proxies.
val client = HttpClient(OkHttp)Close When Done
The client holds real resources, so call close when you no longer need it to release connections cleanly.
client.close()Why Split This Way
This core-plus-engine design lets Ktor reuse each OS's best networking stack while you keep one shared API for all of them.
Quick Check
Which engine should an iOS target use?
Recap
Ktor shares a core API but needs an engine per platform: OkHttp for Android, Darwin for iOS. Auto-selection keeps shared code clean.
คำถามที่พบบ่อย
บทเรียน “เพิ่ม Ktor และเลือกเอนจินตามแพลตฟอร์ม” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “เพิ่ม Ktor และเลือกเอนจินตามแพลตฟอร์ม” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Kotlin Multiplatform Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Kotlin Multiplatform Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “เพิ่ม Ktor และเลือกเอนจินตามแพลตฟอร์ม”
เชื่อมเอนจิน OkHttp และ Darwin ให้แต่ละเป้าหมาย คุณปฏิบัติ Kotlin Multiplatform Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Kotlin Multiplatform Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Kotlin Multiplatform Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “เพิ่ม Ktor และเลือกเอนจินตามแพลตฟอร์ม” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Kotlin Multiplatform Academy นี้ได้ไหม
ได้ บทเรียน Kotlin Multiplatform Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- เพิ่ม Ktor และเลือกเอนจินตามแพลตฟอร์ม
- คำขอ GET แรกของคุณ
- POST, เฮดเดอร์ และพารามิเตอร์คิวรี
- การหมดเวลา การบันทึก日志 และ URL พื้นฐาน