Android,帮助解决 simpleframework PersistenceException

发布于 2024-11-04 21:14:27 字数 1632 浏览 1 评论 0原文

我正在尝试使用org.simpleframework.xml。在我的 Android 项目中处理 xml 数据的类。我无法理解如何构建我的类“Point”构造函数以匹配 xml 定义:在运行时我收到此异常:

org.simpleframework.xml.core.PersistenceException: Constructor not matched for class koine.marcos.wifidemo.Point

我的 xml 数据如下所示:


文件points.xml:

<?xml version="1.0" encoding="utf-8"?>
<points>
   <point id="La Gioconda">
      <rssi ssid="beacon1" bssid="00:21:91:d1:36:62">-52</rssi>
      <rssi ssid="beacon2" bssid="00:12:a9:03:23:32">-97</rssi>
   </point>
   <point id="La Pietà">
      <rssi ssid="beacon1" bssid="00:21:91:d1:36:62">-68</rssi>
      <rssi ssid="beacon2" bssid="00:12:a9:03:23:32">-83</rssi>
   </point>
</points>

文件Rssi.java:

@Root
public class Rssi {

    @Attribute(required=false)
    protected String id;

    @Element(required=false)
    protected Integer value;

    ... getters and setters ...
}

文件point.java :

@Root
public class Point {
    @Attribute
    protected String id;

    @ElementMap(entry="rssi", key="id", attribute=false,
                inline=true, required=false)
    private Map<String,Integer> rssiMap;

    public Point(String id, Map<String,Integer>rssi) {
        this.id = id;
        ...
    }

    ...
}

文件点:java:

@Element
public class Points {
    @ElementList(inline=true, required=true)
    private List<Point> list;

    ... getters and setters ...
}

I am trying to use org.simpleframework.xml. classes to handle xml data on my Android project. I can't understand how to build my class "Point" contructor to match the xml definition: at run-time I get this exception:

org.simpleframework.xml.core.PersistenceException: Constructor not matched for class koine.marcos.wifidemo.Point

My xml data is like this:


File points.xml:

<?xml version="1.0" encoding="utf-8"?>
<points>
   <point id="La Gioconda">
      <rssi ssid="beacon1" bssid="00:21:91:d1:36:62">-52</rssi>
      <rssi ssid="beacon2" bssid="00:12:a9:03:23:32">-97</rssi>
   </point>
   <point id="La Pietà">
      <rssi ssid="beacon1" bssid="00:21:91:d1:36:62">-68</rssi>
      <rssi ssid="beacon2" bssid="00:12:a9:03:23:32">-83</rssi>
   </point>
</points>

File Rssi.java:

@Root
public class Rssi {

    @Attribute(required=false)
    protected String id;

    @Element(required=false)
    protected Integer value;

    ... getters and setters ...
}

File point.java:

@Root
public class Point {
    @Attribute
    protected String id;

    @ElementMap(entry="rssi", key="id", attribute=false,
                inline=true, required=false)
    private Map<String,Integer> rssiMap;

    public Point(String id, Map<String,Integer>rssi) {
        this.id = id;
        ...
    }

    ...
}

File Points:java:

@Element
public class Points {
    @ElementList(inline=true, required=true)
    private List<Point> list;

    ... getters and setters ...
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何以笙箫默 2024-11-11 21:14:27

好的,因为我一直是 坚定地宣扬 Simple XML 到底有多棒 我想我会给您这个问题的完整答案,所以就在这里。完整的工作代码。

Points.java

// You can make this non private and more complex at will.
public class Points {
    @ElementList(entry = "point", inline = true) public ArrayList<Point> points;
}

Point.java

public class Point {
    private final String id;
    private final HashMap<String, Integer> rssiMap;

    public Point(@Attribute(name = "id") String id, @ElementMap(attribute = true, entry = "rssi", key = "ssid", valueType = Integer.class, inline = true) HashMap<String, Integer> rssiMap) {
        this.id = id;
        this.rssiMap = rssiMap;
    }

    @Attribute(name = "id") 
    public String getId() {
            return id;
    }

    @ElementMap(attribute = true, entry = "rssi", key = "ssid", valueType = Integer.class, inline = true)
    public HashMap<String, Integer> getRssi() {
            return rssiMap;
    }
}

Main.java

public class Main {
    public static void main(String[] args) throws Exception {
        Serializer serial = new Persister();

        // Warning: You will need to make sure that this file exists or change it.
        File file = new File("data/data.xml");
        Points points = serial.read(Points.class, file);
        for(Point point : points.points) {
            System.out.println(point.getId());
            for(Entry<String, Integer> entry : point.getRssi().entrySet()) {
                System.out.println(" " + entry.getKey() + ": " + entry.getValue());
            }
        }
    }
}

这就是它的全部内容。它应该很容易读取您的数据。如果您要尝试该代码,那么您必须确保的唯一一件事是 Main 函数正确设置您要读取的文件,或者您只需为读取函数提供正确的输入。

PS 我已经在我的电脑上测试过这个,所以我知道它有效。干杯。

Okay, so because I have been a firm advocate of how awesome Simple XML really is I thought that I would give you a complete answer to this question and so here it is. Completely working code.

Points.java

// You can make this non private and more complex at will.
public class Points {
    @ElementList(entry = "point", inline = true) public ArrayList<Point> points;
}

Point.java

public class Point {
    private final String id;
    private final HashMap<String, Integer> rssiMap;

    public Point(@Attribute(name = "id") String id, @ElementMap(attribute = true, entry = "rssi", key = "ssid", valueType = Integer.class, inline = true) HashMap<String, Integer> rssiMap) {
        this.id = id;
        this.rssiMap = rssiMap;
    }

    @Attribute(name = "id") 
    public String getId() {
            return id;
    }

    @ElementMap(attribute = true, entry = "rssi", key = "ssid", valueType = Integer.class, inline = true)
    public HashMap<String, Integer> getRssi() {
            return rssiMap;
    }
}

Main.java

public class Main {
    public static void main(String[] args) throws Exception {
        Serializer serial = new Persister();

        // Warning: You will need to make sure that this file exists or change it.
        File file = new File("data/data.xml");
        Points points = serial.read(Points.class, file);
        for(Point point : points.points) {
            System.out.println(point.getId());
            for(Entry<String, Integer> entry : point.getRssi().entrySet()) {
                System.out.println(" " + entry.getKey() + ": " + entry.getValue());
            }
        }
    }
}

And that is all that there is to it. It should easily read in your data. If you are going to trial that code then the only thing that you must make sure of is that the Main function sets the File correctly that you are going to read from or you just give the read function the right input.

P.S. I have tested this on my computer so I know that it works. Cheers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文