org.json.xml可以导入到Android Studio项目中吗?

时间:2019-01-04 06:24:41

标签: java android-studio

我正在尝试使用org.json.XML在我的Android应用中将XML文件转换为JSON。由于Android提供的包含的org.json类不包含org.json.XML,因此我将其作为依赖项添加到了build.gradle中。

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:design:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.android.volley:volley:1.1.1'
    implementation group: 'org.json', name: 'json', version: '20180813'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso- core:3.0.2'
}  

但是,当我尝试运行我的应用程序时,它失败并显示以下错误:

java.lang.NoSuchMethodError: No direct method <init>(Ljava/io/Reader;)V in class Lorg/json/JSONTokener; or its super classes (declaration of 'org.json.JSONTokener' appears in /system/framework/core-libart.jar)
    at org.json.XMLTokener.<init>(XMLTokener.java:57)
    at org.json.XML.toJSONObject(XML.java:516)
    at org.json.XML.toJSONObject(XML.java:548)
    at org.json.XML.toJSONObject(XML.java:472)

此外build.gradle发出警告:

json defines classes that conflict with classes now provided by Android. Solutions includes finding newer versions or alternative libraries that do not have the same problem (for example, for httpclient use HttpUrlConnection or okhttp instead), or repackaging the library using something like jarjar.

这与this question中提出的问题相同,但提供的答案不适用于org.json。Android默认不包含XML。

鉴于上述情况,是否可以重写我的build.gradle以忽略包含的org.json库或仅导入org.json.XML?还是我不得不考虑使用另一个库,例如https://github.com/smart-fun/XmlToJson

0 个答案:

没有答案