我们可以将gson模型与领域一起使用吗?当我尝试它显示构建时间错误

时间:2016-12-08 05:25:15

标签: android gradle realm build.gradle

当我尝试构建或运行项目时显示错误时,GSON模型意味着我将在webservice响应中使用,我正在使用领域数据库FirstTime所以

我的模特课

InstaSave.class

    package model;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

import io.realm.RealmObject;

// i extend realm in my gson model 
public class InstaSave extends RealmObject {

    @SerializedName("provider_url")
    @Expose
    private String providerUrl;
    @SerializedName("media_id")
    @Expose
    private String mediaId;
    @SerializedName("author_name")
    @Expose
    private String authorName;
    @SerializedName("height")
    @Expose
    private Object height;
    @SerializedName("thumbnail_url")
    @Expose
    private String thumbnailUrl;
    @SerializedName("thumbnail_width")
    @Expose
    private Integer thumbnailWidth;
    @SerializedName("thumbnail_height")
    @Expose
    private Integer thumbnailHeight;
    @SerializedName("provider_name")
    @Expose
    private String providerName;
    @SerializedName("title")
    @Expose
    private String title;
    @SerializedName("html")
    @Expose
    private String html;
    @SerializedName("width")
    @Expose
    private Integer width;
    @SerializedName("version")
    @Expose
    private String version;
    @SerializedName("author_url")
    @Expose
    private String authorUrl;
    @SerializedName("author_id")
    @Expose
    private Double authorId;
    @SerializedName("type")
    @Expose
    private String type;

    /**
     * 
     * @return
     *     The providerUrl
     */
    public String getProviderUrl() {
        return providerUrl;
    }

    /**
     * 
     * @param providerUrl
     *     The provider_url
     */
    public void setProviderUrl(String providerUrl) {
        this.providerUrl = providerUrl;
    }

    /**
     * 
     * @return
     *     The mediaId
     */
    public String getMediaId() {
        return mediaId;
    }

    /**
     * 
     * @param mediaId
     *     The media_id
     */
    public void setMediaId(String mediaId) {
        this.mediaId = mediaId;
    }

    /**
     * 
     * @return
     *     The authorName
     */
    public String getAuthorName() {
        return authorName;
    }

    /**
     * 
     * @param authorName
     *     The author_name
     */
    public void setAuthorName(String authorName) {
        this.authorName = authorName;
    }

    /**
     * 
     * @return
     *     The height
     */
    public Object getHeight() {
        return height;
    }

    /**
     * 
     * @param height
     *     The height
     */
    public void setHeight(Object height) {
        this.height = height;
    }

    /**
     * 
     * @return
     *     The thumbnailUrl
     */
    public String getThumbnailUrl() {
        return thumbnailUrl;
    }

    /**
     * 
     * @param thumbnailUrl
     *     The thumbnail_url
     */
    public void setThumbnailUrl(String thumbnailUrl) {
        this.thumbnailUrl = thumbnailUrl;
    }

    /**
     * 
     * @return
     *     The thumbnailWidth
     */
    public Integer getThumbnailWidth() {
        return thumbnailWidth;
    }

    /**
     * 
     * @param thumbnailWidth
     *     The thumbnail_width
     */
    public void setThumbnailWidth(Integer thumbnailWidth) {
        this.thumbnailWidth = thumbnailWidth;
    }

    /**
     * 
     * @return
     *     The thumbnailHeight
     */
    public Integer getThumbnailHeight() {
        return thumbnailHeight;
    }

    /**
     * 
     * @param thumbnailHeight
     *     The thumbnail_height
     */
    public void setThumbnailHeight(Integer thumbnailHeight) {
        this.thumbnailHeight = thumbnailHeight;
    }

    /**
     * 
     * @return
     *     The providerName
     */
    public String getProviderName() {
        return providerName;
    }

    /**
     * 
     * @param providerName
     *     The provider_name
     */
    public void setProviderName(String providerName) {
        this.providerName = providerName;
    }

    /**
     * 
     * @return
     *     The title
     */
    public String getTitle() {
        return title;
    }

    /**
     * 
     * @param title
     *     The title
     */
    public void setTitle(String title) {
        this.title = title;
    }

    /**
     * 
     * @return
     *     The html
     */
    public String getHtml() {
        return html;
    }

    /**
     * 
     * @param html
     *     The html
     */
    public void setHtml(String html) {
        this.html = html;
    }

    /**
     * 
     * @return
     *     The width
     */
    public Integer getWidth() {
        return width;
    }

    /**
     * 
     * @param width
     *     The width
     */
    public void setWidth(Integer width) {
        this.width = width;
    }

    /**
     * 
     * @return
     *     The version
     */
    public String getVersion() {
        return version;
    }

    /**
     * 
     * @param version
     *     The version
     */
    public void setVersion(String version) {
        this.version = version;
    }

    /**
     * 
     * @return
     *     The authorUrl
     */
    public String getAuthorUrl() {
        return authorUrl;
    }

    /**
     * 
     * @param authorUrl
     *     The author_url
     */
    public void setAuthorUrl(String authorUrl) {
        this.authorUrl = authorUrl;
    }

    /**
     * 
     * @return
     *     The authorId
     */
    public Double getAuthorId() {
        return authorId;
    }

    /**
     * 
     * @param authorId
     *     The author_id
     */
    public void setAuthorId(Double authorId) {
        this.authorId = authorId;
    }

    /**
     * 
     * @return
     *     The type
     */
    public String getType() {
        return type;
    }

    /**
     * 
     * @param type
     *     The type
     */
    public void setType(String type) {
        this.type = type;
    }

}

错误

1) D:\Android_Studio_Task\InstaSave\app\src\main\java\model\InstaSave.java
   -Error:(10, 8) error: Type java.lang.Object of field height is not supported

2) Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

2 个答案:

答案 0 :(得分:4)

错误指出,您不能将对象类型作为字段

引自https://realm.io/docs/java/latest/#field-types

  

Realm支持以下字段类型:boolean,byte,short,int,long,float,double,String,Date和byte []。整数类型byte,short,int和long都映射到Realm中的相同类型(实际上很长)。此外,支持RealmObject和RealmList的子类来建模关系。

     

盒装类型Boolean,Byte,Short,Integer,Long,Float和Double也可用于模型类。使用这些类型,可以将字段的值设置为null。

您需要将该字段声明为这些类型之一,但是如果您可以序列化/反序列化使用允许类型不允许的类型,那么您可以在getters / setter中执行此操作并将具有所需对象类型的API公开给用户。

你也可以使用@Ingnore注释,这样这个特定的字段不会被带入一个帐户,你可以在创建对象时自己填充它,但它是一种安全性低得多的方法,因为你需要记住每一个事实你使用它的时间。

答案 1 :(得分:1)

您可能需要为序列化模型和领域模型创建单独的类,然后映射它们。