我在公共模块中编写了如下公共代码,并在JS环境中进行了测试
val response = client.post<HttpResponse>(url) {
body = TextContent("""{"a":1,"b":2}""", ContentType.Application.Json)
}
if (response.status != HttpStatusCode.OK) {
logger.error("Error, this one failed bad?")
}
Run Code Online (Sandbox Code Playgroud)
但我的代码以 client.post 结尾,在没有网络的情况下出现取消的 corutineException 。我该如何处理这个异常以及任何其他异常?如果有互联网连接。没有任何失败,我希望能够处理异常。如何?
注意:try、catch 不起作用
我一直在期待如何使用gradle创建一个kotlin-react-app(我知道使用create-kotlin-react-app CLI工具,它不使用radle)我无法获得任何来源指向我通过.我偶然发现了kotlin前端插件(它工作)和npm和webpack插件,但我无法配置它们来运行/创建一个kotlin-react-project.我不是配置webpack的专家,所以它对我来说可能更难.
我打算创建一个多平台项目(是的,在IntelliJ中打包的kotlin经验)
当我失败时,我选择采用这种方法.
有人可以指点我的方向吗?
就像上面的瓷砖一样,我一直在尝试使用neo4j-ogm和kotlin而没有成功.如果我试图坚持我的数据,Neo4j会抛出一个异常,"类xxxx不是一个有效的实体".
package com.asofttz.micros.administrator.users.testmodels
import org.neo4j.ogm.annotation.GeneratedValue
import org.neo4j.ogm.annotation.Id
import org.neo4j.ogm.annotation.NodeEntity
import org.neo4j.ogm.annotation.Relationship
@NodeEntity
class Actor(var name: String = "") {
@Id
@GeneratedValue
open var id: Long? = null
@Relationship(type = "ACTS_IN", direction = "OUTGOING")
open val movies = hashSetOf<Movie>()
fun actsIn(movie: Movie) {
movies.add(movie)
movie.actors.plus(this)
}
}
@NodeEntity
class Movie(var title: String = "", var released: Int = 2000) {
@Id
@GeneratedValue
open var id: Long? = null
@Relationship(type = "ACTS_IN", direction = "INCOMING")
open var actors = setOf<Actor>()
}
Run Code Online (Sandbox Code Playgroud)
有办法吗?是否存在使用kotlin将数据保存到Neo4j数据库的替代方法? …
所以,我有这个类 Item.kt
class Item {
val name = ""
val loc = ""
val price = 0.0
override fun toString() = "$name <$loc> $price"
}
Run Code Online (Sandbox Code Playgroud)
由于此类位于另一个库中(我无法编辑其源代码),因此我有一个外部序列化器。
ItemSerializer.kt
@Serializer(forClass = Item::class)
object ItemSerializer: KSerializer<Item> {
override fun serialize(output: Encoder, obj: Item) {
}
override fun deserialize(input: Decoder): Item {
return df.parse(input.decode())
}
}
Run Code Online (Sandbox Code Playgroud)
现在,困难的部分来了。我可以在另一个类中使用这个类,如下所示
购物车.kt
@Serializable
class Cart {
val id: Long? = null
@Serialize(with=ItemSerializer::class)
val item:Item = Item()
}
Run Code Online (Sandbox Code Playgroud)
但当我使用项目对象列表时,我不知道如何利用我的序列化器。例如
购物车.kt
@Serializable
class Cart {
val id: Long? = null …Run Code Online (Sandbox Code Playgroud) 正如标题所示,runBlocking我刚刚在 build.gradle 中添加的协程库中缺少协程构建器。有趣的是,所有其他东西似乎都可用,GlobalScope全部CoroutineScope.launch CoroutineScope.async存在。runBlocking不是。我究竟做错了什么?
这是我的build.gradle
buildscript {
ext {
ktor_version = "1.1.1"
kotlin_version = "1.3.20-eap-52"
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-frontend-plugin:0.0.44"
classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
}
}
plugins {
id 'kotlin-multiplatform' version '1.3.20-eap-100'
}
repositories {
maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' }
maven { url 'https://dl.bintray.com/kotlin/kotlin-js-wrappers' }
maven { url 'https://dl.bintray.com/kotlinx/kotlinx' }
maven { url "https://kotlin.bintray.com/kotlinx" }
jcenter()
mavenCentral()
}
group 'books'
version '0.0.0'
apply plugin: 'maven-publish'
apply plugin: "org.jetbrains.kotlin.frontend"
kotlin {
jvm() {
compilations.all …Run Code Online (Sandbox Code Playgroud) gradle kotlin build.gradle kotlinx.coroutines kotlin-multiplatform
我最近一直在处理 kotlin 多平台,我完全理解开发的本质。最初,我有自己期望的 Math 类(在一个公共模块中),并且在 JS 和 JVM 环境中有实际的类。
由于我喜欢阅读文档,我发现自 kotlin 1.2 以来,数学库已添加到标准库中。这给我带来了麻烦,因为我使用 kotlin 1.2.51 并且我在尝试从 kotlin.Math 访问我的公共模块和任何平台特定模块中的类时出错。
我没有得到什么?如何访问通用模块中的 kotlin.Math 类?