Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用改装到Room嵌入式类反序列化JSON_Android_Retrofit2_Android Room_Gson - Fatal编程技术网

Android 使用改装到Room嵌入式类反序列化JSON

Android 使用改装到Room嵌入式类反序列化JSON,android,retrofit2,android-room,gson,Android,Retrofit2,Android Room,Gson,我正在从我的服务器接收这个JSON。我的需求与通常的用例略有不同,因此出现了这个问题。就我而言,我使用的是Room library提供的@Embedded [ { "id": 105, "title": "Clear notification", "message": "Opening the app should automatically clear the notifications", "klass": "Demo", "priority": 0,

我正在从我的服务器接收这个JSON。我的需求与通常的用例略有不同,因此出现了这个问题。就我而言,我使用的是Room library提供的
@Embedded

[
{
    "id": 105,
    "title": "Clear notification",
    "message": "Opening the app should automatically clear the notifications",
    "klass": "Demo",
    "priority": 0,
    "timestamp": "2017-09-03T07:20:56.130500Z",
    "teacher": "ABC",
    "attachments": []
},
{
    "id": 104,
    "title": "Attachment",
    "message": "File upload take 1",
    "attachments": [
        {
            "id": 5,
            "name": "Technology List.txt",
            "url": "https://cdn.filestackcontent.com/",
            "type": "text/plain",
            "size": 954
        }
    ]
}
]

POJO用于上述JSON。我正在将JSON反序列化为
FeedWithAttachment
class

public class FeedWithAttachment {
    @NotNull @Embedded
    public Feed feed; 
    @Relation(parentColumn = "id", entityColumn = "feedId", entity = Attachment.class)       
    public List<Attachment> attachments;

}
我的实习班:

data class Attachment(
        @PrimaryKey @SerializedName("id")
        var attachmentId: Int = 0,
        var name: String = "",
        var url: String = "",
        var type: String = "",
        var size: Long = 0,
        @Transient
        var feedId: Long = 0
)

如果我们在FeedWithAttachment中编写feed变量时放入
List,那么改型尝试在json中的键中查找“feed”。
类中定义的每个变量名必须存在于json的键中。
您使用这个类:

public class FeedWithAttachment {
    int id;
    String title;
    String message;
    String klass;
    int priority;
    String timestamp;
    String teacher;

    List<Attachment> attachments;
}

您在API调用响应中做了什么?发布您的答案。@Mehulkbaria我得到的是
FeedWithAttachment
,其中
feed
null
attachments
与预期一致。您似乎试图使用相同的Java类来表示服务器响应和数据库结构。这些都是完全独立的:服务器可以更改所服务JSON的性质,并且您仅限于数据库上的关系结构(以及Room支持的特定SQLite功能子集)。IMHO,您不应该费心尝试为这些应用程序使用相同的类,因为您不一定能够在将来为它们使用相同的类。使用单独的类来建模服务器响应和数据库结构。@Commonware我一直想用同一个类来表示服务器响应和数据库结构,因为它使我的生活更轻松。现在我知道这不是最好的做法。谢谢你的提示。在理想的世界里,他们应该是同一个班级。在理想的世界里,我会有头发。:-)除了JSON风格的树结构和相应的关系结构之间的自然“阻抗不匹配”之外,在许多情况下,您无法控制服务器,服务器团队可能会决定完全重新组织其JSON(例如,为了更好地支持iOS客户机、Web客户机等)。考虑到一方不受控制的更改(JSON)和另一方的结构限制(关系/房间),IMHO更好地为您单独建模。我知道这个解决方案。但是我们可以在
Feed
中嵌入
FeedWithAttachment
@AkshayChordiya吗据我所知,这是不可能的。
interface FeedService {
    public Call<List<FeedWithAttachment>> getFeeds();
}
public class FeedWithAttachment {
    int id;
    String title;
    String message;
    String klass;
    int priority;
    String timestamp;
    String teacher;

    List<Attachment> attachments;
}
public class Attachment{
    int id;
    String name;
    String url;
    String type;
    int size;
}