fix: 解决导入数据时将int类型转换为Double的问题
This commit is contained in:
parent
8e9bd3bc5d
commit
0a1e6c6f2b
@ -31,7 +31,7 @@ android {
|
||||
targetCompatibility JavaVersion.VERSION_11
|
||||
}
|
||||
kotlinOptions {
|
||||
jvmTarget = '1.8'
|
||||
jvmTarget = '11'
|
||||
}
|
||||
buildFeatures {
|
||||
viewBinding true
|
||||
@ -77,7 +77,6 @@ dependencies {
|
||||
|
||||
// 读取spatialite文件
|
||||
implementation 'com.github.sevar83:android-spatialite:2.0.1'
|
||||
|
||||
}
|
||||
//允许引用生成的代码
|
||||
kapt {
|
||||
|
@ -111,47 +111,53 @@ class ImportOMDBHelper @AssistedInject constructor(@Assisted("context") val cont
|
||||
unZipFolder.mkdirs()
|
||||
// 开始解压zip文件
|
||||
val unZipFiles = ZipUtils.unzipFile(omdbZipFile, unZipFolder)
|
||||
// 将listResult数据插入到Realm数据库中
|
||||
Realm.getDefaultInstance().beginTransaction()
|
||||
// 遍历解压后的文件,读取该数据返回
|
||||
for ((index, currentConfig) in importConfig.tables.withIndex()) {
|
||||
val txtFile = unZipFiles.find {
|
||||
it.name == currentConfig.table
|
||||
}
|
||||
|
||||
val listResult: MutableList<Map<String, Any>> = mutableListOf()
|
||||
val listResult = mutableListOf<Map<String, Any?>>()
|
||||
currentConfig?.let {
|
||||
val list = FileIOUtils.readFile2List(txtFile, "UTF-8")
|
||||
if (list!=null) {
|
||||
// 将list数据转换为map
|
||||
for (line in list) {
|
||||
val map = gson.fromJson<Map<String, Any>>(line, object : TypeToken<MutableMap<String, Any>>() {}.type)
|
||||
val map = gson.fromJson<Map<String, Any?>>(line, object:TypeToken<Map<String, Any?>>(){}.getType())
|
||||
.toMutableMap()
|
||||
map["QItable"] = currentConfig.table
|
||||
map["QIname"] = currentConfig.name
|
||||
map["QIcode"] = currentConfig.code
|
||||
map["qi_table"] = currentConfig.table
|
||||
map["qi_name"] = currentConfig.name
|
||||
map["qi_code"] = currentConfig.code
|
||||
listResult.add(map)
|
||||
}
|
||||
}
|
||||
}
|
||||
// 将listResult数据插入到Realm数据库中
|
||||
Realm.getDefaultInstance().beginTransaction()
|
||||
for (map in listResult) { // 每一个map就是Realm的一条数据
|
||||
// 先查询这个mesh下有没有数据,如果有则跳过即可
|
||||
// val meshEntity = Realm.getDefaultInstance().where(RenderEntity::class.java).equalTo("properties['mesh']", map["mesh"].toString()).findFirst()
|
||||
val renderEntity = RenderEntity()
|
||||
renderEntity.code = map["QIcode"].toString().toInt()
|
||||
renderEntity.name = map["QIname"].toString()
|
||||
renderEntity.table = map["QItable"].toString()
|
||||
renderEntity.code = map["qi_code"].toString().toInt()
|
||||
renderEntity.name = map["qi_name"].toString()
|
||||
renderEntity.table = map["qi_table"].toString()
|
||||
// 其他数据插入到Properties中
|
||||
renderEntity.geometry = map["geometry"].toString()
|
||||
for (entry in map) {
|
||||
renderEntity.properties[entry.key] = entry.value.toString()
|
||||
for ((key, value) in map) {
|
||||
when (value) {
|
||||
is String -> renderEntity.properties[key.toString()] = value
|
||||
is Int -> renderEntity.properties[key.toString()] = value.toInt().toString()
|
||||
is Double -> renderEntity.properties[key.toString()] = value.toDouble().toString()
|
||||
else -> renderEntity.properties[key.toString()] = value.toString()
|
||||
}
|
||||
}
|
||||
Realm.getDefaultInstance().insert(renderEntity)
|
||||
}
|
||||
Realm.getDefaultInstance().commitTransaction()
|
||||
// 1个文件发送一次flow流
|
||||
emit("${index+1}/${importConfig.tables.size}")
|
||||
}
|
||||
Realm.getDefaultInstance().commitTransaction()
|
||||
emit("finish")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,14 +1,16 @@
|
||||
package com.navinfo.omqs.hilt
|
||||
|
||||
import android.app.Application
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import androidx.room.Room
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.GsonBuilder
|
||||
import com.google.gson.reflect.TypeToken
|
||||
import com.navinfo.omqs.Constant
|
||||
import com.navinfo.omqs.OMQSApplication
|
||||
import com.navinfo.omqs.db.RoomAppDatabase
|
||||
import com.navinfo.omqs.http.RetrofitNetworkServiceAPI
|
||||
import com.navinfo.omqs.tools.IntTypeAdapter
|
||||
import com.tencent.wcdb.database.SQLiteCipherSpec
|
||||
import com.tencent.wcdb.room.db.WCDBOpenHelperFactory
|
||||
import dagger.Lazy
|
||||
@ -86,7 +88,12 @@ class GlobalModule {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideGson(): Gson = Gson()
|
||||
fun provideGson(): Gson = GsonBuilder()
|
||||
// 解决解析Json时将int类型自动转换为Double的问题
|
||||
.registerTypeAdapter(object : TypeToken<Map<String, Any?>>() {}.getType(), IntTypeAdapter())
|
||||
.registerTypeAdapter(object : TypeToken<Map<String, Any>>() {}.getType(), IntTypeAdapter())
|
||||
.registerTypeAdapter(object : TypeToken<Map<Any, Any>>() {}.getType(), IntTypeAdapter())
|
||||
.create()
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
|
69
app/src/main/java/com/navinfo/omqs/tools/IntTypeAdapter.kt
Normal file
69
app/src/main/java/com/navinfo/omqs/tools/IntTypeAdapter.kt
Normal file
@ -0,0 +1,69 @@
|
||||
package com.navinfo.omqs.tools
|
||||
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.TypeAdapter
|
||||
import com.google.gson.internal.LinkedTreeMap
|
||||
import com.google.gson.stream.JsonReader
|
||||
import com.google.gson.stream.JsonToken
|
||||
import com.google.gson.stream.JsonWriter
|
||||
import java.io.IOException
|
||||
|
||||
class IntTypeAdapter : TypeAdapter<Any>() {
|
||||
private val delegate: TypeAdapter<Any> = Gson().getAdapter(Any::class.java)
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun read(`in`: JsonReader): Any? {
|
||||
val token = `in`.peek()
|
||||
when (token) {
|
||||
JsonToken.BEGIN_ARRAY -> {
|
||||
val list: MutableList<Any?> = ArrayList()
|
||||
`in`.beginArray()
|
||||
while (`in`.hasNext()) {
|
||||
list.add(read(`in`))
|
||||
}
|
||||
`in`.endArray()
|
||||
return list
|
||||
}
|
||||
JsonToken.BEGIN_OBJECT -> {
|
||||
val map: MutableMap<String, Any?> = LinkedTreeMap()
|
||||
`in`.beginObject()
|
||||
while (`in`.hasNext()) {
|
||||
map[`in`.nextName()] = read(`in`)
|
||||
}
|
||||
`in`.endObject()
|
||||
return map
|
||||
}
|
||||
JsonToken.STRING -> return `in`.nextString()
|
||||
JsonToken.NUMBER -> {
|
||||
// 改写数字的处理逻辑,将数字值分为整型与浮点型。
|
||||
val dbNum = `in`.nextDouble()
|
||||
// 数字超过long的最大值,返回浮点类型
|
||||
if (dbNum > Long.MAX_VALUE) {
|
||||
return dbNum
|
||||
}
|
||||
// 判断数字是否为整数值
|
||||
val lngNum = dbNum.toLong()
|
||||
return if (dbNum == lngNum.toDouble()) {
|
||||
try {
|
||||
lngNum.toInt()
|
||||
} catch (e: Exception) {
|
||||
lngNum
|
||||
}
|
||||
} else {
|
||||
dbNum
|
||||
}
|
||||
}
|
||||
JsonToken.BOOLEAN -> return `in`.nextBoolean()
|
||||
JsonToken.NULL -> {
|
||||
`in`.nextNull()
|
||||
return null
|
||||
}
|
||||
else -> throw IllegalStateException()
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun write(out: JsonWriter, value: Any?) {
|
||||
delegate.write(out, value)
|
||||
}
|
||||
}
|
@ -98,19 +98,11 @@ class PersonalCenterFragment : BaseFragment(), FSAFActivityCallbacks {
|
||||
|
||||
override fun onResult(uri: Uri) {
|
||||
val file = UriUtils.uri2File(uri)
|
||||
val importOMDBHelper: ImportOMDBHelper =
|
||||
importOMDBHiltFactory.obtainImportOMDBHelper(
|
||||
val importOMDBHelper: ImportOMDBHelper = importOMDBHiltFactory.obtainImportOMDBHelper(
|
||||
requireContext(),
|
||||
file
|
||||
)
|
||||
viewModel.importOMDBData(importOMDBHelper)
|
||||
// // 开始导入数据
|
||||
// CoroutineUtils.launchWithLoading(
|
||||
// requireContext(),
|
||||
// loadingMessage = "导入数据..."
|
||||
// ) {
|
||||
//
|
||||
// }
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -122,14 +114,13 @@ class PersonalCenterFragment : BaseFragment(), FSAFActivityCallbacks {
|
||||
|
||||
override fun onResult(uri: Uri) {
|
||||
viewModel.importScProblemData(uri)
|
||||
|
||||
}
|
||||
})
|
||||
}
|
||||
R.id.personal_center_menu_test -> {
|
||||
viewModel.readRealmData()
|
||||
// 定位到指定位置
|
||||
niMapController.mMapView.vtmMap.animator().animateTo(GeoPoint(28.608398, 115.67901))
|
||||
niMapController.mMapView.vtmMap.animator().animateTo(GeoPoint(30.21137798479949, 113.84832672274896))
|
||||
}
|
||||
R.id.personal_center_menu_task_list -> {
|
||||
findNavController().navigate(R.id.TaskListFragment)
|
||||
|
@ -44,8 +44,8 @@ android {
|
||||
}
|
||||
}
|
||||
compileOptions {
|
||||
sourceCompatibility JavaVersion.VERSION_1_8
|
||||
targetCompatibility JavaVersion.VERSION_1_8
|
||||
sourceCompatibility JavaVersion.VERSION_11
|
||||
targetCompatibility JavaVersion.VERSION_11
|
||||
}
|
||||
|
||||
sourceSets {
|
||||
|
@ -1549,4 +1549,9 @@
|
||||
<symbol src="assets:symbols/dot_blue.svg" />
|
||||
</m>
|
||||
</m>
|
||||
|
||||
<m k="QIcode">
|
||||
<!-- 道路线 -->
|
||||
<m v="2001"></m>
|
||||
</m>
|
||||
</rendertheme>
|
@ -42,7 +42,7 @@ class GeometryToolsKt {
|
||||
val tileY1 = MercatorProjection.latitudeToTileY(minMaxY[1], Constant.OVER_ZOOM.toByte())
|
||||
val minTileY = if (tileY0 <= tileY1) tileY0 else tileY1
|
||||
val maxTileY = if (tileY0 <= tileY1) tileY1 else tileY0
|
||||
println("getTileYByGeometry$envelope===$minTileY===$maxTileY")
|
||||
// println("getTileYByGeometry$envelope===$minTileY===$maxTileY")
|
||||
|
||||
for (i in minTileY..maxTileY) {
|
||||
tileYSet.add(i)
|
||||
@ -86,7 +86,7 @@ class GeometryToolsKt {
|
||||
val tileX1 = MercatorProjection.longitudeToTileX(minMaxX[1], Constant.OVER_ZOOM.toByte())
|
||||
val minTileX = if (tileX0 <= tileX1) tileX0 else tileX1
|
||||
val maxTileX = if (tileX0 <= tileX1) tileX1 else tileX0
|
||||
println("getTileXByGeometry$envelope$minTileX===$maxTileX")
|
||||
// println("getTileXByGeometry$envelope$minTileX===$maxTileX")
|
||||
for (i in minTileX..maxTileX) {
|
||||
tileXSet.add(i)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user