在Retrofit GET请求之后如何使用GSON解析此JSON响应?

时间:2015-04-09 15:26:01

标签: android json rest gson retrofit

我正在尝试在Retrofit GET请求之后使用GSON解析JSON响应。我不需要所有的键和值,所以我只是@Expose我需要的那些并指示解析器这样做。请求激活并且响应变得干净,但是查看logcat我发现这个错误,这显然表明POJO模型格式化或实现不好:

04-09 12:16:01.679 5604-5604 /? V / Retrofit错误:retrofit.converter.ConversionException:com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_ARRAY,但在第1行第2列路径为BEGIN_OBJECT $

这是JSON响应(在GET请求后返回ok):

{
   objects: [
   {
     rating: 0.97,
     name: "High Line",
     ranking: 0,
     url: "http://www.thehighline.org",
     price: null,
     phone: "2125006035",
     last_sync: 1428328869,
     photos: [
    "https://irs3.4sqi.net/img/general/original/11402168_2zKtnTfWXPJJJAaX7N6g1EMPTR7ahNqSAOsMotN-jNU.jpg"
     ],
     local_id: 13778,
     likes: 0,
     city_id: 2621,
     address: "btwn Gansevoort & W 34th St",
     resource_uri: "/api/v1/venues/40f1d480f964a5206a0a1fe3/",
     id: "40f1d480f964a5206a0a1fe3",
     categories: [
     {
       name: "Park",
       parent: {
            local_id: 7,
            name_id: "sights",
            name: "Landmarks",
           id: "4d4b7105d754a06377d81259"
       },
       local_id: 494,
       name_id: "park",
       category_id: 7,
       id: "4bf58dd8d48988d163941735"
    }
   ],
   location: {
      lat: 40.7470618874989,
      lng: -74.0051937103271
   }
  },
  {
    rating: 0.97,
    name: "Central Park",
    ranking: 0,
    url: "http://www.centralparknyc.org",
    price: null,
    phone: "2123106600",
    last_sync: 1428521923,
    photos: [
  "https://irs2.4sqi.net/img/general/original/655018_Zp3vA90Sy4IIDApvfAo5KnDItoV0uEDZeST7bWT-qzk.jpg"
    ],
    local_id: 13826,
    likes: 0,
    city_id: 2621,
    address: "59th St to 110th St",
    resource_uri: "/api/v1/venues/412d2800f964a520df0c1fe3/",
    id: "412d2800f964a520df0c1fe3",
    categories: [
    {
       name: "Park",
       parent: {
          local_id: 7,
          name_id: "sights",
          name: "Landmarks",
          id: "4d4b7105d754a06377d81259"
       },
       local_id: 494,
       name_id: "park",
       category_id: 7,
       id: "4bf58dd8d48988d163941735"
     }
    ],
    location: {
      lat: 40.7888599444948,
      lng: -73.9611625671387
    }
   }
 ],
  meta: {
    total_count: 1344,
    next: "/api/v1/venues/?city_id=2621&category=topPicks&offset=2&limit=2&format=json",
    limit: 2,
    offset: 0
    }
}

这是对Retrofit服务的主要活动调用:

   Map<String, String> params = new HashMap<String, String>();
    params.put("city_id", "2621");
    params.put("offset", "0");
    params.put("limit", "2"); 
    params.put("category", "topPicks");
    params.put("format", "json");


    ApiClient.getApiClient().listVenues(params, new Callback<List<ApiResponse>>() {

        @Override
        public void success(List<ApiResponse> venues, Response response) {

            //consumir venues
            Log.v("RETROFIT SUCCESS", response.getBody().toString());

            mAdapter = new MainCustomAdapter(venues);
            mRecyclerView.setAdapter(mAdapter);

        }

        @Override
        public void failure(RetrofitError retrofitError) {

            if (retrofitError.getResponse() != null) {
                Log.v("Retrofit error", retrofitError.getCause().toString());
            }
            //manejar el fallo


        }
    });

这是Api客户:

public class ApiClient {
private static ApiVenuesInterface apiVenues;

public static ApiVenuesInterface getApiClient() {

   Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();

    if (apiVenues == null) {
        RestAdapter restAdapter = new RestAdapter.Builder()

                .setEndpoint("http://endpoint.com")
                .setConverter(new GsonConverter(gson))
                .setLogLevel(RestAdapter.LogLevel.FULL).setLog(new AndroidLog("RETROFIT"))
                .build();

        apiVenues = restAdapter.create(ApiVenuesInterface.class);
    }

    return apiVenues;
}

 public interface ApiVenuesInterface {
     //llamada asíncrona al querystring de venues
     @GET("/api/v1/venues")
     void listVenues(@QueryMap Map<String, String> params, Callback<List<ApiResponse>> callback);
}}

最后这是我的POJO模型(我认为是主要问题的地方):

    public class ApiResponse {
    private List<Object> objects = new ArrayList<Object>();

}
class Object {

    @Expose
    private String name;
    @Expose
    private List<String> photos = new ArrayList<String>();
    @Expose
    private String address;
    @Expose
    private List<Category> categories = new ArrayList<Category>();

    /**
     *
     * @return
     * The name
     */
    public String getName() {
        return name;
    }


    /**
     *
     * @return
     * The photos
     */
    public List<String> getPhotos() {
        return photos;
    }

    /**
     *
     * @return
     * The address
     */
    public String getAddress() {
        return address;
    }

    /**
     *
     * @return
     * The categories
     */
    public List<Category> getCategories() {
        return categories;
    }

    class Category {
        @Expose
        private String name;

        /**
         *
         * @return
         * The name
         */
        public String getName() {
            return name;
        }

    }}

那么,我怎么必须建模我的POJO来解析我需要的数据呢?提前致谢。

重要编辑:此问题适用于Retrofit 1.x.请注意,Retrofit 2.x与此略有不同,因为它使用注释和调用方法。

2 个答案:

答案 0 :(得分:2)

根据之前的评论:

根据快速查看,您的Callback返回类型不是List<ApiResponse>类型,而只是ApiResponse。如果您更改以下内容,则应该开始工作:

public interface ApiVenuesInterface {
     //llamada asíncrona al querystring de venues
     @GET("/api/v1/venues")
     void listVenues(@QueryMap Map<String, String> params, Callback<ApiResponse> callback);
}

目前,您告诉Gson将响应转换为ApiResponse个对象的列表,但实际上它只是一个ApiResponse包装列表/项目数组。您正确地对POJO进行了建模,但不是改造回调。


[edit]回答您的后续问题:只需在ApiResponse课程中添加一个getter即可访问包装的对象:

public List<Object> getObjects() {
    return objects;
}

小提示:为Object类提供一个不同的名称会很好,因为它的名称与Java中所有对象的“母亲”相同:java.lang.Object。这必然会导致混淆并且非常容易导入/引用错误。尝试提出一些更具描述性的内容,例如Venue(因为它看起来就像你正在处理的那样,虽然我可能会弄错)。


[edit2] Gson生成一个ApiResponse对象,它包围List<Object>。迭代该列表可以像任何其他Java迭代一样完成;即使用增强的for循环:

for (Object object : getObjects()) {
    // get the name for this object
    String name = object.getName();
    // get the address for this object
    String address = object.getAddress();
    // get all the photo urls for this object
    List<String> photos = object.getPhotos();
    // etc...
}

您看到ie myjavapackage.Object@3c4a86e1的原因是因为Java不知道如何将对象表示为打印字符串。您可以通过覆盖Object类中的以下方法来更改此表示形式:

@Override public String toString() {
    // return a string representation for this object; i.e. its name:
    return name;
}

我仍然建议您将Object课程重命名为更合理的内容,以避免将其与内置java.lang.Object混淆。

答案 1 :(得分:-2)

尝试从json字符串中删除此部分

  

{   对象:

和最后一个大括号。