我们如何在 kotlin mutlplatform 中处理 ios 和 android 平台的 api 响应?

发布于 2025-01-11 06:55:44 字数 774 浏览 3 评论 0原文

嘿,我是 Kotlin 多平台移动领域的新手。我有android领域的开发经验。我正在尝试连接 Android 应用程序Ios 应用程序。我搜索并看到 Ktor 是从服务器获取数据的方式。在 android 中,我们使用 Kotlin 密封类来处理 API 响应 您的应用程序和 ios 端团队中的示例使用 了解 Swift 闭包和异步请求函数 与这些类似。我有点困惑,我如何在公共模块中使用这种方式来使用两个平台,因为两个平台都有拦截器和更多东西。我需要从头开始编写所有内容吗?或者有什么办法我们可以使用现有的方式。我的两个平台应用程序是一个巨大的项目。我不会更换代码。我想使用 Ktor 通过 KMM 启动新的 api 调用。任何建议或项目都会对我有帮助。谢谢

Hey I am new in the world of Kotlin Multiplatform mobile. I have experience in development of android field. I am trying to connect Android application and Ios application. I searched and see Ktor is the way to fetch data from server. In android we use Kotlin Sealed Classes for Handling API Responses example in your application and in ios side team use Understanding Swift closures and asynchronous request functions similar to these. I am little bit confused, How can I use this way in common module to use both platform because both platform have Interceptor and many more things. Do I need to write everything from scratch? or Is there any way we can use existing way. My both platforms application is huge project. I don't won't to replace the code. I want to start new api call throught KMM using Ktor. Any suggestion or project would be helpful for me. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

早乙女 2025-01-18 06:55:44

您不需要从头开始编写任何内容。基本上,您将需要一个位于共享模块中的单独的网络层。它将主要由您的请求/响应模型和 api 调用内容组成。在这里,您只能定义一个密封类,它将在您的 api 回调中为您提供 api 是否成功或失败的信息,并据此您可以处理您的响应。
您可以针对您的用例参考这个令人惊叹的示例。您还可以参考这篇博客进行深入了解。

You don't need to write anything from scratch. Basically you will need a separate Network Layer which will be in shared module. It will mainly consists of your request/response models and api call stuff. Here only you can define a sealed class which will provide you if the api is success or failure in your api callback and based on that you can handle your response.
You can refer to this amazing sample for your usecase. You can also refer to this blog for in depth understanding.

山田美奈子 2025-01-18 06:55:44

如果您想在 Kotlin-Multiplatofrm 项目 中使用旧的 Android 代码,您需要执行一些步骤

第一步: 您应该将代码与业务分离 来了解它

层和表示层,您可以通过阅读干净的Android架构第二步:

,我们将把业务层放入共享模块,然后我们将表示层放入ANDROID APP 第三步: > 你应该更换Retrofit使用 Ktor 代码进行编码

第四步: 您应该用 SqlDlight 代码替换 Room 代码

第五步: 您应该使用 Compose 而不是 Xml 在表示层中编写视图,

然后应用程序就可以准备好使用他的业务逻辑到ios

第六步:然后你可以将你的swift代码写入IOS APP

你可以看一下这段代码这不是完整的代码但它会让你清楚你可以使用什么
我在 Github 上的 Kmm 存储库

注意:

表示层 包含 view 、 viewmodel 和任何Workmanger 业务层等平台相关工具

包含网络和缓存代码,例如 KTOR、SQLDELIGHT 以及可能在 Android 和 Ios 之间共享的任何逻辑

If you want to use old Android Code into Kotlin-Multiplatofrm Project you need to do some steps

First Step : you should separate your code into business layer and presentation layer you can know about it by reading clean architecture Android

Second Step : we will put business layer into Shared Module then we will put presentation layer into ANDROID APP

Third Step : you should replace Retrofit Code with Ktor Code

Fourth Step : You should replace Room Code with SqlDlight Code

fifth Step : You Should Write your view in presentation layer with Compose not Xml

then application will be ready to use his business logic into ios

sixth Step : then you can write your swift code into IOS APP

you can look at this code this is not complete code but it will make it clear to you haw you can use
my Kmm Repo at Github

note :

Presentation layer contain view , viewmodel and any Platform dependent tools like workmanger

business layer contain network and caching code like KTOR , SQLDELIGHT and any logic that may be shared between Android and Ios

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文