使用Retrofit从Autocomplete Google Places API获取数据

时间:2016-08-23 19:08:06

标签: java android json retrofit

我一直在阅读多个教程(因为这是我第一次使用Retrofit和Google Places API),我注意到你创建了一个POJO类的模式,所以Retrofit可以将JSON响应数据映射到你的POJO。是否可以仅使用某些字段创建POJO,或者在创建POJO时是否必须在JSON响应中包含每个字段?例如,如果我有这样的JSON响应结构(取自Google Places Autocomplete API):

adb reverse tcp:8081 tcp:3333

显然我需要一个班级来保存{ "status": "OK", "predictions" : [ { "description" : "Paris, France", "id" : "691b237b0322f28988f3ce03e321ff72a12167fd", "matched_substrings" : [ { "length" : 5, "offset" : 0 } ], "place_id" : "ChIJD7fiBh9u5kcRYJSMaMOCCwQ", "reference" : "CjQlAAAA_KB6EEceSTfkteSSF6U0pvumHCoLUboRcDlAH05N1pZJLmOQbYmboEi0SwXBSoI2EhAhj249tFDCVh4R-PXZkPK8GhTBmp_6_lWljaf1joVs1SH2ttB_tw", "terms" : [ { "offset" : 0, "value" : "Paris" }, { "offset" : 7, "value" : "France" } ], "types" : [ "locality", "political", "geocode" ] }, ...additional results ... } status的值:

predictions

然后对于public class Result { String status; @SerializedName("predictions") List<Prediction> predictionList; } 课程,我可以有以下成员:

Prediction

但是我需要public class Prediction { String description; String id; @SerializedName("place_id") String placeID; String reference; List<String> types; } matched_substrings包含单独的对象(即创建单独的类),即使我打算不使用这些值吗?

我遇到的第二个问题是如何只获取JSON响应字符串,以便我可以使用Gson自行解析它。这有助于避免为大型JSON响应创建一堆类。我只是好奇,以防我想要这样做。感谢。

0 个答案:

没有答案