Tag: 序列化

无法使用kotlinx.serialization将Json解析为数据类

在Android项目中,我试图使用kotlinx.serialization和Retrofit从远程端点解析Json响应。 我在使用Jake Warthon的https://github.com/JakeWharton/retrofit2-kotlinx-serialization-converter来设置转换器时设置改造实例 Retrofit.Builder() .baseUrl(baseUrl) .addConverterFactory(stringBased(contentType, ::parse, ::stringify)) .addCallAdapterFactory(CoroutineCallAdapterFactory()) .build() 我有以下模型注解@Serializable,我想用来解析Json响应 import kotlinx.serialization.Serializable @Serializable data class Response(val groups: List<GroupsResponse>) 请注意,Response和嵌套的GroupsResponse都定义了一个通用types。 我一直在浏览文档,但是我一直无法理解generics和kotlinx.serialization是如何一起玩的,尽管它提到它支持generics,并且您应该使用生成的序列化方法访问那些模型的序列化程序 。 我已经建立了项目使用正确的插件按照kotlinx.serialization和Jake的库的文档,但问题是,该项目甚至不编译,因为我正在得到以下types的错误,当构建过程执行kaptDebugKotlin gradle任务 e: /Users/nico/Dev/git/edreams/workshops/K-Places/data/build/tmp/kapt3/stubs/debug/com/edreams/android/workshops/kotlin/data/venues/remote/response/Response.java:75: error: non-static type variable T cannot be referenced from a static context public static final class $serializer implements kotlinx.serialization.KSerializer<com.edreams.android.workshops.kotlin.data.venues.remote.response.Response> { e: /Users/nico/Dev/git/edreams/workshops/K-Places/data/build/tmp/kapt3/stubs/debug/com/edreams/android/workshops/kotlin/data/venues/remote/response/GroupsResponse.java:109: error: cannot find symbol kotlinx.serialization.KSerializer typeSerial0) { […]

将Java位图转换为字节数组

Bitmap bmp = intent.getExtras().get(“data”); int size = bmp.getRowBytes() * bmp.getHeight(); ByteBuffer b = ByteBuffer.allocate(size); bmp.copyPixelsToBuffer(b); byte[] bytes = new byte[size]; try { b.get(bytes, 0, bytes.length); } catch (BufferUnderflowException e) { // always happens } // do something with byte[] 在调用copyPixelsToBuffer之后,当我查看缓冲区时,字节全部为0 …从相机返回的位图是不可变的,但这不应该因为它正在进行复制。 这段代码有什么问题?