当前位置: 首页 > 知识库问答 >
问题:

treverfit2:应为BEGIN_ARRAY,但在第1行第2列路径$处为BEGIN_OBJECT

年健
2023-03-14

我有以下JSON结构:

[
  {
    "userId": 1,
    "id": 1,
    "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
    "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
  },
  {
    "userId": 1,
    "id": 2,
    "title": "qui est esse",
    "body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"
  }
]

以下是我的APIService接口:

public interface APIService {

    @POST("posts")
    Call<List<FakeJSON>> getItems();
}

下面是我的POJO类:

public class FakeJSON {

    @SerializedName("userId")
    @Expose
    private int userId;
    @SerializedName("id")
    @Expose
    private int id;
    @SerializedName("title")
    @Expose
    private String title;
    @SerializedName("body")
    @Expose
    private String body;

    /**
     * No args constructor for use in serialization
     *
     */
    public FakeJSON() {
    }

    /**
     *
     * @param id
     * @param body
     * @param title
     * @param userId
     */
    public FakeJSON(int userId, int id, String title, String body) {
        super();
        this.userId = userId;
        this.id = id;
        this.title = title;
        this.body = body;
    }

    public int getUserId() {
        return userId;
    }

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

    public int getId() {
        return id;
    }

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

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

}

最后,这里是我正在进行网络调用的activity代码:

public class NetworkActivity extends AppCompatActivity {

    private static Retrofit.Builder builder = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_network);
        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
        builder = new Retrofit.Builder()
                .baseUrl("https://jsonplaceholder.typicode.com/")
                .addConverterFactory(GsonConverterFactory.create());

        Retrofit retrofit =
                builder
                        .client(
                                httpClient.build()
                        )
                        .build();
        APIService apiService = retrofit.create(APIService.class);

        Call<List<FakeJSON>> listCall = apiService.getItems();

        listCall.enqueue(new Callback<List<FakeJSON>>() {
            @Override
            public void onResponse(Call<List<FakeJSON>> call, Response<List<FakeJSON>> response) {
                try {
                    Log.e("List Size", response.body().size()+"");
                }catch (NullPointerException e){
                    e.printStackTrace();;
                }
            }

            @Override
            public void onFailure(Call<List<FakeJSON>> call, Throwable t) {
                Log.e("Error", t.getMessage());
            }
        });
    }
}

共有1个答案

松琦
2023-03-14

'E/错误:应为BEGIN_ARRAY,但为BEGIN_OBJECT

这意味着一个改型面向的是JSON对象,而不是数组。也许你从服务器上收到了一些错误?尝试手动执行相同的查询并查看响应。也许通过这种方式,您可以发现从代码中完成的请求是否有问题。

 类似资料: