错误RetroFit开始ARray对象.IllegalStateException

时间:2018-09-17 11:11:52

标签: java android gson retrofit2

我是Per day = (n/t) * r * b * a ROI (return on investment, in year) = ((n/t) * r * b * a * 365)/1000, 的新手,却遇到了错误

  

Wrongjava.lang.IllegalStateException:应该为opacity,但为   BEGIN_ARRAY在第2行第14列的路径$ .messages。

我遵循了一个教程,但是似乎正确了。

如果您需要其他帮助,请告诉我。

这是context.Model.Where(a => a.Entity == "example").Count()文件的一部分。

GSON

这是我的代码。

Message.java

BEGIN_OBJECT

Attachments.java

JSON

Main.java

{
"messages": [
{
  "id": 0,
  "userId": 3,
  "content": "iusto eius quod necessitatibus culpa ea, odit magnam ut saepe sed non qui\ntempora atque nihil\naccusamus illum doloribus illo dolor\neligendi repudiandae odit magni similique sed cum maiores"
},
{
  "id": 1,
  "userId": 7,
  "content": "beatae enim quia vel"
},
{
  "id": 2,
  "userId": 10,
  "content": "at nam consequatur ea labore ea harum",
  "attachments": [
    {
      "id": "3577cf23-56db-4ce8-92a0-0c1abc5f9402",
      "title": "accusamus beatae ad facilis cum similique qui sunt",
      "url": "http://placehold.it/600/92c952",
      "thumbnailUrl": "http://placehold.it/150/92c952"
    },
    {
      "id": "660bac80-7473-48bc-99ba-a7aed36a743a",
      "title": "reprehenderit est deserunt velit ipsam",
      "url": "http://placehold.it/600/771796",
      "thumbnailUrl": "http://placehold.it/150/771796"
    },
    {
      "id": "3fba42bc-33e8-4451-8433-2c48ec303e30",
      "title": "officia porro iure quia iusto qui ipsa ut modi",
      "url": "http://placehold.it/600/24f355",
      "thumbnailUrl": "http://placehold.it/150/24f355"
    }
  ]
},

Feed

package com.example.rjrod.legendchat.Model;

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

import java.util.ArrayList;

public class Message {

    @SerializedName("id")
    @Expose
    private int id;
    @SerializedName("userdId")
    @Expose
    private int userId;
    @SerializedName("content")
    @Expose
    private String content;
    @SerializedName("attachments")
    @Expose
    private ArrayList<Attachments> attachments;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public ArrayList<Attachments> getAttachments() {
        return attachments;
    }

    public void setAttachments(ArrayList<Attachments> attachments) {
        this.attachments = attachments;
    }

    @Override
    public String toString() {
        return "Message{" +
                "id='" + id + '\'' +
                ", userId='" + userId + '\'' +
                ", content='" + content + '\'' +
                ", attachments=" + attachments +
                '}';
    }
}

您能帮这个新手吗?

4 个答案:

答案 0 :(得分:2)

1)您的Feed.java应该像

<ul>
 <li v-if="flag">
  <myVueComponent                            
   v-bind:list="LIST">
  </myVueComponent>
 </li>
 <li v-else v-for="item in LIST">
  <a :href="url+item.name">@{{item.name}}</a>
 </li>
</ul>

2)您根本无法访问您提到的附件

class Feed {
    List<Message> messages = new ArrayList<>();

    public List<Message> getMessages() {
        return messages;
    }

    public void setMessages(List<Message> messages) {
        this.messages = messages;
    }
}

因为ArrayList<Attachments> attachmentsList=response.body().getMessage().getAttachments(); 是一个数组,因此无法通过attachments直接访问messagesmessages的对象可访问attachments

3)因此,您可以像这样访问messages

attachments

4)尝试对List<Messages> messages = response.body().getMessages(); for (int i = 0; i < messages.size(); i++) { Message message = messages.get(i); Attachments attachment = message.getAttachments(); /* do something with the attachment */ } 进行空检查,因为从您的json看来,并非每个对象都可用。

答案 1 :(得分:2)

在您的json对象中,从最后一行中删除“,”并添加]}。由于json格式格式不正确,因此您会收到此错误。进行更改并签入“在线json编辑器”

答案 2 :(得分:2)

在您的Feed模型类中,您必须将private Message messages;更改为

private ArrayList<Message> messages;

与响应中一样,您将接收到一系列消息,并且改造将接受确切的响应模型。

您的所有消息数据都将在消息 arrayList

答案 3 :(得分:1)

您正在从json获取数组,但是您尝试将结果存储在对象中。您必须将响应存储在对象数组中。