Android - 使用改造时出现错误“无法构造实例(尽管至少存在一个创建)”
Posted
技术标签:
【中文标题】Android - 使用改造时出现错误“无法构造实例(尽管至少存在一个创建)”【英文标题】:Android - Getting error "Cannot construct instance of (although at least one create exist)" using Retrofit 【发布时间】:2019-06-01 04:07:24 【问题描述】:我有一个 json,它的标签被视为字符串,尽管字符串也是 json 格式。
"details": "\"max_keep_days\":\"\",\"notes\":\"\",\"dir\":\"\",\"auto_host_enable\":\"1\",\"auto_host\":\"https://videos3.earthcam.com/fecnetwork/9189.flv/playlist.m3u8\",\"rtsp_transport\":\"tcp\",\"muser\":\"\",\"mpass\":\"\",\"port_force\":\"0\",\"fatal_max\":\"0\",\"skip_ping\":null,\"is_onvif\":null,\"onvif_port\":\"\",\"aduration\":\"1000000\",\"probesize\":\"1000000\",\"stream_loop\":\"0\",\"sfps\":\"\",\"accelerator\":\"0\",\"hwaccel\":\"auto\",\"hwaccel_vcodec\":\"\",\"hwaccel_device\":\"\",\"stream_type\":\"mp4\",\"stream_flv_type\":\"ws\",\"stream_flv_maxLatency\":\"\",\"stream_mjpeg_clients\":\"\",\"stream_vcodec\":\"copy\",\"stream_acodec\":\"no\",\"hls_time\":\"2\",\"hls_list_size\":\"3\",\"preset_stream\":\"ultrafast\",\"signal_check\":\"10\",\"signal_check_log\":\"0\",\"stream_quality\":\"15\",\"stream_fps\":\"2\",\"stream_scale_x\":\"\",\"stream_scale_y\":\"\",\"rotate_stream\":\"no\",\"svf\":\"\",\"tv_channel\":\"0\",\"tv_channel_id\":\"\",\"tv_channel_group_title\":\"\",\"stream_timestamp\":\"0\",\"stream_timestamp_font\":\"\",\"stream_timestamp_font_size\":\"\",\"stream_timestamp_color\":\"\",\"stream_timestamp_box_color\":\"\",\"stream_timestamp_x\":\"\",\"stream_timestamp_y\":\"\",\"stream_watermark\":\"0\",\"stream_watermark_location\":\"\",\"stream_watermark_position\":\"tr\",\"snap\":\"0\",\"snap_fps\":\"\",\"snap_scale_x\":\"\",\"snap_scale_y\":\"\",\"snap_vf\":\"\",\"vcodec\":\"copy\",\"crf\":\"1\",\"preset_record\":\"\",\"acodec\":\"no\",\"dqf\":\"0\",\"cutoff\":\"15\",\"rotate_record\":\"no\",\"vf\":\"\",\"timestamp\":\"0\",\"timestamp_font\":\"\",\"timestamp_font_size\":\"10\",\"timestamp_color\":\"white\",\"timestamp_box_color\":\"0x00000000@1\",\"timestamp_x\":\"(w-tw)/2\",\"timestamp_y\":\"0\",\"watermark\":\"0\",\"watermark_location\":\"\",\"watermark_position\":\"tr\",\"cust_input\":\"\",\"cust_snap\":\"\",\"cust_rtmp\":\"\",\"cust_rawh264\":\"\",\"cust_detect\":\"\",\"cust_stream\":\"\",\"cust_stream_server\":\"\",\"cust_record\":\"\",\"custom_output\":\"\",\"detector\":\"0\",\"detector_send_frames\":\"1\",\"detector_lock_timeout\":\"\",\"detector_save\":\"0\",\"detector_fps\":\"\",\"detector_scale_x\":\"640\",\"detector_scale_y\":\"480\",\"detector_record_method\":\"sip\",\"detector_trigger\":\"1\",\"detector_trigger_record_fps\":\"\",\"detector_timeout\":\"10\",\"watchdog_reset\":\"0\",\"detector_delete_motionless_videos\":\"0\",\"detector_webhook\":\"0\",\"detector_webhook_url\":\"\",\"detector_command_enable\":\"0\",\"detector_command\":\"\",\"detector_command_timeout\":\"\",\"detector_mail\":\"0\",\"detector_mail_send_video\":null,\"detector_mail_timeout\":\"\",\"detector_discordbot\":null,\"detector_discordbot_send_video\":null,\"detector_discordbot_timeout\":\"\",\"use_detector_filters\":null,\"use_detector_filters_object\":null,\"cords\":\"[]\",\"detector_filters\":\"\",\"detector_pam\":\"1\",\"detector_show_matrix\":null,\"detector_sensitivity\":\"\",\"detector_max_sensitivity\":\"\",\"detector_threshold\":\"1\",\"detector_color_threshold\":\"\",\"detector_frame\":\"0\",\"detector_noise_filter\":null,\"detector_noise_filter_range\":\"\",\"detector_notrigger\":\"0\",\"detector_notrigger_mail\":\"0\",\"detector_notrigger_timeout\":\"\",\"detector_use_detect_object\":\"0\",\"detector_use_motion\":\"1\",\"detector_fps_object\":\"\",\"detector_scale_x_object\":\"\",\"detector_scale_y_object\":\"\",\"detector_lisence_plate\":\"0\",\"detector_lisence_plate_country\":\"us\",\"detector_buffer_vcodec\":\"auto\",\"detector_buffer_acodec\":null,\"detector_buffer_fps\":\"\",\"detector_buffer_hls_time\":\"\",\"detector_buffer_hls_list_size\":\"\",\"detector_buffer_start_number\":\"\",\"detector_buffer_live_start_index\":\"\",\"control\":\"0\",\"control_base_url\":\"\",\"control_url_method\":null,\"control_digest_auth\":null,\"control_stop\":\"0\",\"control_url_stop_timeout\":\"\",\"control_url_center\":\"\",\"control_url_left\":\"\",\"control_url_left_stop\":\"\",\"control_url_right\":\"\"\"control_url_right_stop\":\"\",\"control_url_up\":\"\",\"control_url_up_stop\":\"\",\"control_url_down\":\"\",\"control_url_down_stop\":\"\",\"control_url_enable_nv\":\"\",\"control_url_disable_nv\":\"\",\"control_url_zoom_out\":\"\",\"control_url_zoom_out_stop\":\"\",\"control_url_zoom_in\":\"\",\"control_url_zoom_in_stop\":\"\",\"groups\":\"[]\",\"loglevel\":\"warning\",\"sqllog\":\"0\",\"detector_cascades\":\"\",\"stream_channels\":\"\",\"input_maps\":\"\",\"input_map_choices\":\"\""
我认为这可能与改造没有直接关系。可能更像是一个杰克逊,但有没有一种方法可以让我通过改造获得这种反序列化。
我已经为上述细节创建了一个 POJO。我不得不删除一些属性,因为它太大而无法在此处发布。
import java.io.Serializable;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder(
"max_keep_days",
"notes",
"dir",
"auto_host_enable",
"auto_host",
"rtsp_transport",
"muser",
"mpass",
"port_force"
)
public class Details implements Serializable
@JsonProperty("max_keep_days")
private String max_keep_days;
@JsonProperty("notes")
private String notes;
@JsonProperty("dir")
private String dir;
@JsonProperty("auto_host_enable")
private String auto_host_enable;
@JsonProperty("auto_host")
private String auto_host;
@JsonProperty("rtsp_transport")
private String rtsp_transport;
@JsonProperty("muser")
private String muser;
@JsonProperty("mpass")
private String mpass;
@JsonProperty("port_force")
private String port_force;
private final static long serialVersionUID = 4253775945877731727L;
@JsonProperty("max_keep_days")
public String getMax_keep_days()
return max_keep_days;
@JsonProperty("max_keep_days")
public void setMax_keep_days(String max_keep_days)
this.max_keep_days = max_keep_days;
@JsonProperty("notes")
public String getNotes()
return notes;
@JsonProperty("notes")
public void setNotes(String notes)
this.notes = notes;
@JsonProperty("dir")
public String getDir()
return dir;
@JsonProperty("dir")
public void setDir(String dir)
this.dir = dir;
@JsonProperty("auto_host_enable")
public String getAuto_host_enable()
return auto_host_enable;
@JsonProperty("auto_host_enable")
public void setAuto_host_enable(String auto_host_enable)
this.auto_host_enable = auto_host_enable;
@JsonProperty("auto_host")
public String getAuto_host()
return auto_host;
@JsonProperty("auto_host")
public void setAuto_host(String auto_host)
this.auto_host = auto_host;
@JsonProperty("rtsp_transport")
public String getRtsp_transport()
return rtsp_transport;
@JsonProperty("rtsp_transport")
public void setRtsp_transport(String rtsp_transport)
this.rtsp_transport = rtsp_transport;
@JsonProperty("muser")
public String getMuser()
return muser;
@JsonProperty("muser")
public void setMuser(String muser)
this.muser = muser;
@JsonProperty("mpass")
public String getMpass()
return mpass;
@JsonProperty("mpass")
public void setMpass(String mpass)
this.mpass = mpass;
@JsonProperty("port_force")
public String getPort_force()
return port_force;
@JsonProperty("port_force")
public void setPort_force(String port_force)
this.port_force = port_force;
但我仍然收到错误消息。以下是我的使用方法。
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import java.io.Serializable;
import java.util.List;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder(
"mid",
"ke",
"name",
"shto",
"shfr",
"details",
"type",
"ext",
"protocol",
"host",
"path",
"port",
"fps",
"mode",
"width",
"height",
"currentlyWatching",
"currentCpuUsage",
"status",
"streams",
"streamsSortedByType"
)
@JsonIgnoreProperties(ignoreUnknown = true)
public class Monitor implements Serializable
@JsonProperty("mid")
private String mid;
@JsonProperty("ke")
private String ke;
@JsonProperty("name")
private String name;
@JsonProperty("shto")
private String shto;
@JsonProperty("shfr")
private String shfr;
@JsonProperty("details")
private Details details;
@JsonProperty("type")
private String type;
@JsonProperty("ext")
private String ext;
@JsonProperty("protocol")
private String protocol;
@JsonProperty("host")
private String host;
@JsonProperty("path")
private String path;
@JsonProperty("port")
private Long port;
@JsonProperty("fps")
private Long fps;
@JsonProperty("mode")
private String mode;
@JsonProperty("width")
private Long width;
@JsonProperty("height")
private Long height;
@JsonProperty("currentlyWatching")
private Long currentlyWatching;
@JsonProperty("currentCpuUsage")
private Float currentCpuUsage;
@JsonProperty("status")
private String status;
@JsonProperty("snapshot")
private String snapshot;
@JsonProperty("streams")
private List<String> streams = null;
@JsonProperty("streamsSortedByType")
private StreamsSortedByType streamsSortedByType;
private final static long serialVersionUID = -7389140931424648308L;
@JsonProperty("mid")
public String getMid()
return mid;
@JsonProperty("mid")
public void setMid(String mid)
this.mid = mid;
@JsonProperty("ke")
public String getKe()
return ke;
@JsonProperty("ke")
public void setKe(String ke)
this.ke = ke;
@JsonProperty("name")
public String getName()
return name;
@JsonProperty("name")
public void setName(String name)
this.name = name;
@JsonProperty("shto")
public String getShto()
return shto;
@JsonProperty("shto")
public void setShto(String shto)
this.shto = shto;
@JsonProperty("shfr")
public String getShfr()
return shfr;
@JsonProperty("shfr")
public void setShfr(String shfr)
this.shfr = shfr;
@JsonProperty("details")
public Details getDetails()
return details;
@JsonProperty("details")
public void setDetails(Details details)
this.details = details;
@JsonProperty("type")
public String getType()
return type;
@JsonProperty("type")
public void setType(String type)
this.type = type;
@JsonProperty("ext")
public String getExt()
return ext;
@JsonProperty("ext")
public void setExt(String ext)
this.ext = ext;
@JsonProperty("protocol")
public String getProtocol()
return protocol;
@JsonProperty("protocol")
public void setProtocol(String protocol)
this.protocol = protocol;
@JsonProperty("host")
public String getHost()
return host;
@JsonProperty("host")
public void setHost(String host)
this.host = host;
@JsonProperty("path")
public String getPath()
return path;
@JsonProperty("path")
public void setPath(String path)
this.path = path;
@JsonProperty("port")
public Long getPort()
return port;
@JsonProperty("port")
public void setPort(Long port)
this.port = port;
@JsonProperty("fps")
public Long getFps()
return fps;
@JsonProperty("fps")
public void setFps(Long fps)
this.fps = fps;
@JsonProperty("mode")
public String getMode()
return mode;
@JsonProperty("mode")
public void setMode(String mode)
this.mode = mode;
@JsonProperty("width")
public Long getWidth()
return width;
@JsonProperty("width")
public void setWidth(Long width)
this.width = width;
@JsonProperty("height")
public Long getHeight()
return height;
@JsonProperty("height")
public void setHeight(Long height)
this.height = height;
@JsonProperty("currentlyWatching")
public Long getCurrentlyWatching()
return currentlyWatching;
@JsonProperty("currentlyWatching")
public void setCurrentlyWatching(Long currentlyWatching)
this.currentlyWatching = currentlyWatching;
@JsonProperty("currentCpuUsage")
public Float getCurrentCpuUsage()
return currentCpuUsage;
@JsonProperty("currentCpuUsage")
public void setCurrentCpuUsage(Float currentCpuUsage)
this.currentCpuUsage = currentCpuUsage;
@JsonProperty("status")
public String getStatus()
return status;
@JsonProperty("status")
public void setStatus(String status)
this.status = status;
@JsonProperty("snapshot")
public String getSnapshot()
return snapshot;
@JsonProperty("snapshot")
public void setSnapshot(String snapshot)
this.snapshot = snapshot;
@JsonProperty("streams")
public List<String> getStreams()
return streams;
@JsonProperty("streams")
public void setStreams(List<String> streams)
this.streams = streams;
@JsonProperty("streamsSortedByType")
public StreamsSortedByType getStreamsSortedByType()
return streamsSortedByType;
@JsonProperty("streamsSortedByType")
public void setStreamsSortedByType(StreamsSortedByType streamsSortedByType)
this.streamsSortedByType = streamsSortedByType;
感谢任何反馈。
【问题讨论】:
【参考方案1】:从 Jackson 的角度来看,Json 中的字段 details
只是一个字符串,即使它恰好是某个转义为字符串的实例的 Json 表示。
错误实际上是说 Jackson 不知道如何将此字符串转换为 Details 实例。幸运的是,通过添加 JsonCreater 可以轻松解决这个问题,因此通过在 Details
类中实现一个方法,将该字符串转换为 Details
的实例:
@JsonCreator
public static Details createDetail(String s) throws IOException
return new ObjectMapper().readValue(s, Details.class);
这只是告诉杰克逊在它期望 Json 呈现详细信息时获得一个字符串时使用此方法。
注意:如果你不使用类似的东西:
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES,
false);
你接下来会碰到那里:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException
因为字符串包含您的Details
类不包含的属性。
【讨论】:
以上是关于Android - 使用改造时出现错误“无法构造实例(尽管至少存在一个创建)”的主要内容,如果未能解决你的问题,请参考以下文章
使用 Multidex 构建 xamarin.android 时出现 CREATEMULTIDEXMAINDEXCLASSLIST 错误
Android 错误:使用 android-json-rpc 时出现 NoClassDefFound 错误
使用串行插件时出现 Ionic Capacitor Android 错误 - 连接到外部设备时出现 Cordova_not_available
使用 Android 库 (AAR) 时出现 AndroidRuntime 错误 ClassNotFoundException