Android, help with a simpleframework PersistenceException

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-08 00:58:47

问题


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 ...
}

回答1:


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.



来源:https://stackoverflow.com/questions/5894320/android-help-with-a-simpleframework-persistenceexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!