Android - 获取错误“无法使用Retrofit构建实例(尽管至少存在一个创建)”

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android - 获取错误“无法使用Retrofit构建实例(尽管至少存在一个创建)”相关的知识,希望对你有一定的参考价值。

我有一个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;
    }

}

感谢任何反馈。

答案

从杰克逊的观点来看,Json中的字段details只是一个字符串,即使它恰好是一些实例转义为字符串的Json演示文稿。

错误实际上是说杰克逊不知道如何将此字符串转换为Details实例。幸运的是,通过在Details类中实现一个将该字符串转换为Details实例的方法,可以通过添加JsonCreater轻松解决此问题:

@JsonCreator
public static Details createDetail(String s) throws IOException {
    return new ObjectMapper().readValue(s, Details.class);
}

这只是告诉Jackson在获得字符串的时候使用这个方法,因为它期望Json呈现细节。

注意:如果您不使用以下内容:

 mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES,
                 false);

你接下来会碰到那里:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException

因为string包含Details类不包含的属性。

以上是关于Android - 获取错误“无法使用Retrofit构建实例(尽管至少存在一个创建)”的主要内容,如果未能解决你的问题,请参考以下文章

React native(android) assembleRelease 获取捆绑组装错误

如何为 android API 级别不一致获取编译时错误而不是 NoSuchMethodError 运行时错误?

在 Android 上的 ListFragment 中使用 ArrayAdapter 获取构造函数错误

Android Studio错误尝试获取屏幕截图:“从设备获取屏幕截图时出现意外错误:EOF”

获取通知密钥错误 401 gcm https://android.googleapis.com/gcm/googlenotification

在Android中获取加载包装器属性错误