gpt4 book ai didi

java - 使用 OpenCSV 解析 csv 文件时未找到类 "java.beans.Introspector"

转载 作者:搜寻专家 更新时间:2023-11-01 09:26:32 24 4
gpt4 key购买 nike

我正在为我的 Android 应用程序使用 OpenCsv 解析 .csv 文件。我在 kotlin 中编写代码。这是我的代码:

这是我解析 csv 文件的方式:

val beans: List<csvDataClass> = CsvToBeanBuilder<csvDataClass>(FileReader(file))
.withType(csvDataClass::class.java).build().parse()

for (i:Int in 0..beans.size-1) {
Log.d("Tag", " NAME = ${beans.get(i).name} Number = ${beans.get(i).phone_one_value}")
}

这是我要将 csv 数据转换成的对象类。

class csvDataClass {
@CsvBindByName (column = "Name")
var name:String = ""
@CsvBindByName (column = "Phone 1 - Value")
var phone_one_value:Int = 0

constructor() {}
constructor(name: String, phone_one_value: Int) {
this.name = name
this.phone_one_value = phone_one_value
}

我在 build.gradle 文件中添加了这一行:

implementation 'com.opencsv:opencsv:4.1'

这是我的 csv 文件内容:

Name,Phone 1 - Value

GT,11111 11111香港,22222 22222K K,33333 33333PK,44444 44444

但是当我运行我得到的代码时:

05-04 16:13:31.821 25829-25829/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.pk.opencsvpoc, PID: 25829
java.lang.NoClassDefFoundError: Failed resolution of: Ljava/beans/Introspector;
at com.opencsv.bean.HeaderColumnNameMappingStrategy.loadDescriptors(HeaderColumnNameMappingStrategy.java:372)
at com.opencsv.bean.HeaderColumnNameMappingStrategy.loadDescriptorMap(HeaderColumnNameMappingStrategy.java:284)
at com.opencsv.bean.HeaderColumnNameMappingStrategy.setType(HeaderColumnNameMappingStrategy.java:415)
at com.opencsv.bean.opencsvUtils.determineMappingStrategy(opencsvUtils.java:83)
at com.opencsv.bean.CsvToBeanBuilder.build(CsvToBeanBuilder.java:156)
at com.example.pardeepkumar.opencsvpoc.MainActivity.readCsvFile(MainActivity.kt:46)
at com.example.pardeepkumar.opencsvpoc.MainActivity.onCreate(MainActivity.kt:38)
at android.app.Activity.performCreate(Activity.java:6726)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2619)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2727)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
Caused by: java.lang.ClassNotFoundException: Didn't find class "java.beans.Introspector" on path: DexPathList[[zip file "/data/app/com.example.pk.opencsvpoc-1/base.apk", zip file "/data/app/com.example.pk.opencsvpoc-1/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.pk.opencsvpoc-1/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.pk.opencsvpoc-1/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.pk.opencsvpoc-1/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.pk.opencsvpoc-1/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.pk.opencsvpoc-1/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.pk.opencsvpoc-1/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.pk.opencsvpoc-1/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.pk.opencsvpoc-1/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.pk.opencsvpoc-1/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.pk.opencsvpoc-1/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.pk.opencsvpoc-1/lib/arm64, /system/lib64, /vendor/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at com.opencsv.bean.HeaderColumnNameMappingStrategy.loadDescriptors(HeaderColumnNameMappingStrategy.java:372) 
at com.opencsv.bean.HeaderColumnNameMappingStrategy.loadDescriptorMap(HeaderColumnNameMappingStrategy.java:284) 
at com.opencsv.bean.HeaderColumnNameMappingStrategy.setType(HeaderColumnNameMappingStrategy.java:415) 
at com.opencsv.bean.opencsvUtils.determineMappingStrategy(opencsvUtils.java:83) 
at com.opencsv.bean.CsvToBeanBuilder.build(CsvToBeanBuilder.java:156) 
at com.example.pardeepkumar.opencsvpoc.MainActivity.readCsvFile(MainActivity.kt:46) 
at com.example.pardeepkumar.opencsvpoc.MainActivity.onCreate(MainActivity.kt:38) 
at android.app.Activity.performCreate(Activity.java:6726) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2619) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2727) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6121) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779) 

最佳答案

Android 上的 Bean 处理需要一个外部库:openbeans .

但是,我不确定如果将此库添加到类路径中 OpenCSV 是否会工作,因为 OpenBeans 中的 Introspector 等类的包名称与原始 JDK 包不匹配。

你也可以试试我的图书馆:univocity-parsers ,它可以在 Android 上与 OpenBeans 一起使用来生成您的类的实例。

像这样使用它:

CsvRoutines routines = new CsvRoutines();

for (csvDataClass bean : routines.iterate(csvDataClass.class, file)) {
System.out.println(bean);
}

希望这对您有所帮助。

关于java - 使用 OpenCSV 解析 csv 文件时未找到类 "java.beans.Introspector",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50173340/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com