android - 应为 BEGIN_ARRAY 但实际为 BEGIN_OBJECT

标签 android json gson retrofit

我是初学者,我正在我的应用中使用 Retrofit 2。我有这个 JSON。 (我尝试了很多解决方案,但对我没有任何帮助)。感谢您的帮助

我遇到的错误:应为 BEGIN_ARRAY 但在第 1 行第 2 列为 BEGIN_OBJECT

JSON

{
   responseCode: 1,
   responseCodeText: "ok",
   response: [
      {
         lat: 67.2422432322,
         lng: 25.1441441441,
         title: "Point na mapě 1",
         desc: "Lorem ipsum dolor sit amet, consectetur adipisicing elit.\nVero praesentium fugiat nobis pariatur cupiditate saepe dolorum, soluta dignissimos.",
         photo: [
            "http://photo3.jpg",
            "http://photo4.jpg",
         ]
      },
      {
         lat: 39.1234787,
         lng: 25.2242445,
         title: "Point na mapě 2",
         desc: "Possimus veritatis, neque a et odit ad itaque iusto asperiores perspiciatis voluptates,\nvero praesentium fugiat nobis pariatur cupiditate saepe dolorum, soluta dignissimos.",
         photo: [
            "http://photo1.jpg",
            "http://photo2.jpg",
         ]
      }, 
      //other

界面

public interface PointsInterface {

        String BASE_URL = "MY_URL/";

         @POST("getPointsOnMap")
         Call<List<PointsOnMap>> getPointsOnMap();

        class Factory {
            private static PointsInterface service;

            public static PointsInterface getInstance() {
                if (service == null) {
                    Retrofit retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()).baseUrl(BASE_URL).build();
                    service = retrofit.create(PointsInterface.class);
                    return service;
                } else {
                    return service;
                }
            }
        }
    }

API调用

//接口(interface)服务

   public void serviceInit() {
        PointsInterface.Factory.getInstance().getPointsOnMap().enqueue(new Callback<List<PointsOnMap>>() {
            @Override
            public void onResponse(Call<List<PointsOnMap>> call, Response<List<PointsOnMap>> response) {
                List<PointsOnMap> result = response.body();
                Log.d("response", "Number of points received: " + result.size());
            }

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

模型

public class PointsOnMap {

    @SerializedName("lat") private Double lat;
    @SerializedName("lng") private Double lng;
    @SerializedName("title") private String title;
    @SerializedName("desc") private String desc;

    public PointsOnMap(Double lat, Double lng, String title, String desc) {
        this.lat = lat;
        this.lng = lng;
        this.title = title;
        this.desc = desc;
    }

    public Double getLat() {
        return lat;
    }

    public void setLat(Double lat) {
        this.lat = lat;
    }

    public Double getLng() { return lng; }

    public void setLng(Double lng) {
        this.lng = lng;
    }

    public String getTitle() { return title; }

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

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }
}

最佳答案

你的改造界面

     @POST("getPointsOnMap")
     Call<List<PointsOnMap>> getPointsOnMap();

应该是

     @POST("getPointsOnMap")
     Call<PointsOnMapResponse> getPointsOnMap();

PointsOnMapResponse 在哪里

public class PointsOnMapResponse {
    @SerializedName("responseCode")
    long responseCode;

    @SerializedName("responseCodeText")
    String responseCodeText;

    @SerializedName("response")
    List<PointsOnMap> response;

    //getters, setters
}

关于android - 应为 BEGIN_ARRAY 但实际为 BEGIN_OBJECT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39051435/

相关文章:

java - 如何构建具有自定义一周第一天的区域设置?

android - 如何在 android 中使用 Bundle 标记 FB 事件

Android - iphone 风格的 tabhost

json - 使用 ng-repeat 在 AngularJS 中打印嵌套的 JSON 数组值

java - PUT 操作的 Android JSON api 问题

java - Android - 在没有 StringEscapeUtils 的情况下解码 unicode 字符?

java - java比较运算符的工作原理

json - 使用 Go 解码 JSON

android - 使用 GSON 库解析 JSON 时出现问题

java - 按照文件中定义的顺序获取所有 JSON 键