java - 简单XML : ConstructorException

标签 java android kotlin rss simple-framework

我正在尝试解析一些 RSS 2.0 提要,如下所示: https://audioboom.com/channels/4682117.rss

我的模型看起来像这样

Rss Class(Java文件,Kotlin也给我带来了麻烦)

@Root(name = "rss", strict = false)
@Namespace(prefix = "itunes", reference = "http://www.itunes.com/dtds/podcast-1.0.dtd")
public class RSSFeed {

    @Element(name = "title", required = false)
    @Path("channel")
    public String channelTitle = null;

    @Element(name = "description", required = false)
    @Path("channel")
    public String description = null;

    @Element(name = "url", required = false)
    @Path("channel/image")
    String imageUrl = null;

    @ElementList(name = "item", inline = true, required = false)
    @Path("channel")
    public List<Article> articleList = null;
}

项目类(Kotlin 文件)

@Root(name = "item", strict = false)
@Namespace(prefix = "itunes", reference = "http://www.itunes.com/dtds/podcast-1.0.dtd")
class Article{

    @set:Path("title")
    @get:Path("title")
    @set:Text(required = false)
    @get:Text(required = false)
    var title: String? = null

    @set:Element(name = "link", required = false)
    @get:Element(name = "link", required = false)
    var url: String? = null

    @set:Element(name = "enclosure", required = false)
    @get:Element(name = "enclosure", required = false)
    var enclosure: Enclosure? = null

    @set:Element(name = "guid", required = false, data = true)
    @get:Element(name = "guid", required = false, data = true)
    var id: String? = null

    @set:Namespace(reference = "http://www.itunes.com/dtds/podcast-1.0.dtd")
    @get:Namespace(reference = "http://www.itunes.com/dtds/podcast-1.0.dtd")
    @set:Element(name = "image", required = false)
    @get:Element(name = "image", required = false)
    var image: Image? = null

    @set:Namespace(reference = "http://www.itunes.com/dtds/podcast-1.0.dtd")
    @get:Namespace(reference = "http://www.itunes.com/dtds/podcast-1.0.dtd")
    @set:Element(name = "duration", required = false)
    @get:Element(name = "duration", required = false)
    var duration: String? = null

    fun getEpisodeUrl(): String? = enclosure?.url ?: url
}

自从我启用了 Progaurd。我开始收到此错误:

java.lang.RuntimeException: org.simpleframework.xml.core.ConstructorException: Default constructor can not accept read only @org.simpleframework.xml.Element(data=false, name=duration, required=false, type=void) on method 'duration' in class com.myapp.model.rss.Article

最佳答案

我从 here 获得了正确的 progaurd 配置

# SimpleXML
# Save the obfuscation mapping to a file, so we can de-obfuscate any stack
# traces later on. Keep a fixed source file attribute and all line number
# tables to get line numbers in the stack traces.
# You can comment this out if you're not interested in stack traces.

-printmapping out.map
-keepparameternames
-renamesourcefileattribute SourceFile
-keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,EnclosingMethod

# Preserve all annotations.

-keepattributes *Annotation*

# Add optimizations

-allowaccessmodification
-optimizationpasses 3

# Preserve all public classes, and their public and protected fields and
# methods.

-keep public class * {
    public protected *;
}

# Preserve all .class method names.

-keepclassmembernames class * {
    java.lang.Class class$(java.lang.String);
    java.lang.Class class$(java.lang.String, boolean);
}

# Preserve all native method names and the names of their classes.

-keepclasseswithmembernames class * {
    native <methods>;
}

# Preserve the special static methods that are required in all enumeration
# classes.

-keepclassmembers class * extends java.lang.Enum {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

# Explicitly preserve all serialization members. The Serializable interface
# is only a marker interface, so it wouldn't save them.
# You can comment this out if your library doesn't use serialization.
# If your code contains serializable classes that have to be backward
# compatible, please refer to the manual.

-keepclassmembers class * implements java.io.Serializable {
    static final long serialVersionUID;
    static final java.io.ObjectStreamField[] serialPersistentFields;
    private void writeObject(java.io.ObjectOutputStream);
    private void readObject(java.io.ObjectInputStream);
    java.lang.Object writeReplace();
    java.lang.Object readResolve();
}

# Your library may contain more items that need to be preserved;
# typically classes that are dynamically created using Class.forName:

-keep interface org.simpleframework.xml.core.Label {
   public *;
}
-keep class * implements org.simpleframework.xml.core.Label {
   public *;
}
-keep interface org.simpleframework.xml.core.Parameter {
   public *;
}
-keep class * implements org.simpleframework.xml.core.Parameter {
   public *;
}
-keep interface org.simpleframework.xml.core.Extractor {
   public *;
}
-keep class * implements org.simpleframework.xml.core.Extractor {
   public *;
}

-dontwarn com.bea.xml.stream.**
-dontwarn org.simpleframework.xml.stream.**
-keep class org.simpleframework.xml.**{ *; }
-keepclassmembers,allowobfuscation class * {
    @org.simpleframework.xml.* <fields>;
    @org.simpleframework.xml.* <init>(...);
}

关于java - 简单XML : ConstructorException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51040272/

相关文章:

java.awt.桌面类

kotlin - Kotlin 如何使用其 get 函数在内部检索映射值

android - SMS检索器api android在vivo v15 pro,redmi note 4中不起作用

java - 使用 Java Streams 将 Java ArrayList 转换为 Vector

java - Gradle中的发布jar

java - hybris 通过网络服务应用全局折扣

java - 当 View 在 ListView 中变为回收时,如何知道何时取消异步任务

java - 如何以编程方式打开我在 Android 应用程序中创建的文件夹?

android - android绑定(bind)数据的通用形式

java - 如何在嵌套类中恢复对 this$0 的引用?