我只是在为我的项目试用 Android 上的 Firebase。
我遇到的问题是,每次拍摄快照并将其“转换”回 POJO 时,我都会得到这个 -
“在反序列化时映射,但得到一个类 java.util.ArrayList”异常。
我一直在四处寻找,甚至使用 HashMap 更改了我所有的模型实现,根本没有任何 ArrayList,但仍然得到了相同的结果。
这是我的模型:
public class DevicesController {
public DevicesCollection devices;
public int numberOfport;
String timerStatus;
public DevicesController(){
devices = new DevicesCollection();
timerStatus = "UPDATED";
}
public DevicesController(int numberOfport){
devices = new DevicesCollection();
this.numberOfport = numberOfport;
timerStatus = "UPDATED";
}
public ArrayList<Integer> getCurrentState(String in){
ArrayList<Integer> currentState = new ArrayList<Integer>();
for(int i = 0; i < numberOfport; i++){
currentState.add(0);
}
Iterator it = this.getDevices().getAllDevices().entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
Device temp =(Device)pair.getValue();
if(temp.isOn()){
currentState.set(temp.getPort(),1);
}else if(!temp.isOn()){
currentState.set(temp.getPort(),0);
}
}
return currentState;
}
public String getStringCurrentState(String in){
ArrayList<Integer> currentState = getCurrentState("");
String temp ="";
for(int i = 0; i < currentState.size();i++){
temp.concat(""+currentState.get(i));
}
return temp;
}
public void updateToDb(){
Global.dbMRoot.child("devicesController").setValue(this);
}
public DevicesCollection getDevices() {
return devices;
}
public int getNumberOfport() {
return numberOfport;
}
}
public class Category {
public String name;
public HashMap<String,String> devicesId;
public DeviceAlarm categoryAlarm;
public Category(){
}
public Category(String name) {
devicesId = new HashMap<String,String>();
this.name = name;
}
public void addDevice(Device dev){
devicesId.put(dev.getId(),dev.getId());
}
public void removeDevice(Device dev){
devicesId.remove(dev.getId());
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public HashMap<String,String> getDevicesId() {
return devicesId;
}
public void setDevicesId(HashMap<String,String> devicesId) {
this.devicesId = devicesId;
}
}
public class Device {
public String id;
public int port;
public String name;
public boolean on;
public DeviceAlarm alarm;
public Device(){
}
public Device(String id,String name){
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
updateToDb();
}
public String getName() {
return name;
}
public void setName(String name) {
updateToDb();
this.name = name;
}
public boolean isOn() {
return on;
}
public void setOn(boolean on) {
updateToDb();
this.on = on;
}
public void updateToDb(){
Global.dbMRoot.child("devicesController").child("devices").child("allDevice").child(""+id).setValue(this);
}
public DeviceAlarm getTimer() {
return alarm;
}
public int getPort() {
return port;
}
public void setPort(int port) {
updateToDb();
this.port = port;
}
public DeviceAlarm getAlarm() {
return alarm;
}
public void setAlarm(DeviceAlarm alarm) {
Global.dbMRoot.child("devicesController").child("timerStatus").setValue("CHANGED");
this.alarm = alarm;
updateToDb();
}
public void setAlarm(boolean active, Calendar timeOn, Calendar timeOff) {
Global.dbMRoot.child("devicesController").child("timerStatus").setValue("CHANGED");
DeviceAlarm temp = new DeviceAlarm();
temp.setTimeOff(timeOff);
temp.setTimeOn(timeOn);
this.alarm = temp;
updateToDb();
}
}
package com.lucerna.afgadev.lucerna_maincontroller.Models;
import com.lucerna.afgadev.lucerna_maincontroller.Global;
import java.util.ArrayList;
import java.util.HashMap;
/**
* Created by aufa on 06/07/2016.
*/
public class DevicesCollection {
public HashMap<String,Device> allDevices;
public HashMap<String,Category> categories;
int lastId;
public DevicesCollection(){
categories = new HashMap<String,Category>();
lastId = 0;
allDevices = new HashMap<String, Device>();
}
public HashMap<String,Category> getCategories() {
return categories;
}
public HashMap<String,Device> getAllDevices() {
return allDevices;
}
public void addCategory(String name){
categories.put(name,new Category(name));
}
public void removeCategory(String name) throws Exception{
int index = -1;
for(int i = 0; i< categories.size(); i++){
if(categories.get(i).getName().equals(name)){
index = 1;
}
}
if(index == -1){
throw new Exception("Category not found");
}else{
categories.remove(index);
}
updateToDB();
}
public void updateToDB(){
Global.dbMRoot.child("devicesController").child("devices").setValue(this);
}
public Category findCategory(String name){
Category temp = null;
for(int i = 0; i< this.getCategories().size(); i++){
if(this.getCategories().get(i).getName().equals(name)){
temp = this.getCategories().get(i);
}
}
return temp;
}
public void switchCategory(String name, boolean on){
Category cat = findCategory(name);
for(int i = 0; i < cat.getDevicesId().size();i++){
}
}
public void addDevice(String name, int port){
Device temp = new Device();
temp.setPort(port);
temp.setName(name);
allDevices.put(""+lastId,temp);
this.lastId++;
Global.dbMRoot.child("devicesController").child("devices").child(""+lastId).setValue(allDevices.get(lastId));
}
public void removeDevice(int id){
allDevices.remove(id);
updateToDB();
}
public void setCategoryDevicesAlarm(DeviceAlarm da){
}
}
示例 JSON 树 GDrive
我仍然不知道它为什么会发生,我已经重新检查以确保没有一个是 ArrayList(我知道 firebase 应该支持 arrayList)
感谢所有评论和回答!
最佳答案
我有部分答案,但需要 Frank 的专业知识来解释真正发生的事情。
运行发布的 9.2.0 版代码,我观察到 allDevices 的 HashMap 存储为 JSON 数组:
public class DevicesCollection {
public HashMap<String,Device> allDevices; <-- this is stored as array
public HashMap<String,Category> categories;
int lastId;
这在 Aufa 发布的 JSON 中可见:
{
"devicesController" : {
"devices" : {
"allDevices" : [ { <-- note bracket
"name" : "",
"on" : true,
"port" : 0
}, {
这似乎是因为 allDevices 的元素键是整数形式的字符串。这是向 allDevices 的映射添加条目的代码:
public void addDevice(String name, int port){
Device temp = new Device();
temp.setPort(port);
temp.setName(name);
allDevices.put(""+lastId,temp); <-- Note that lastId is an integer
this.lastId++;
Global.dbMRoot.child("devicesController").child("devices").child(""+lastId).setValue(allDevices.get(lastId));
}
如果将此代码修改为使用非整数格式的字符串键,例如:
allDevices.put("ID"+lastId,temp);
然后将 map 写成 map ,可以使用读取
getValue(DevicesCollection.class);
关于java - 火力地堡 "Map while deserializing, but got a class java.util.ArrayList",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38253501/
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file
当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www