在 Android 上,我使用 google-http-java-client 库从 google place api 解析 json。我按照这些网站google-http-java-client-json 和Android 使用 Google Places and Maps 教程的说明进行操作
我从 google place detail search api 获取 json 内容:
{
"html_attributions" : [],
"result" : {
"address_components" : [
{
"long_name" : "10号",
"short_name" : "10号",
"types" : [ "street_number" ]
},
{
"long_name" : "East Chang'an Avenue",
"short_name" : "East Chang'an Avenue",
"types" : [ "route" ]
},
{
"long_name" : "Dongcheng",
"short_name" : "Dongcheng",
"types" : [ "sublocality", "political" ]
},
{
"long_name" : "Beijing",
"short_name" : "Beijing",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Beijing",
"short_name" : "Beijing",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "CN",
"short_name" : "CN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "10号 East Chang'an Avenue, Dongcheng, Beijing, China",
"formatted_phone_number" : "010 6528 8888",
"geometry" : {
"location" : {
"lat" : 39.9074880,
"lng" : 116.4090440
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "33c009586d7d2ac1a8d8392c02f03db6c316710a",
"international_phone_number" : "+86 10 6528 8888",
"name" : "Shanggong Law Firm",
"reference" : "CnRvAAAA25ct_IRN2tU_bHsLhfuwQmvrfRR_WEPvFv30GtadCc6Axe6hK3OkL0B1UrLNu7J2scVzn5qylFTf_YOzhf14ba2GcrkCnGkXeobKxJ-DDUCTJOIqVzuvj1BZ5Vb-OxboQeztU3VI9zBgAFGwe8sg3xIQTxsKMMP88wqEtgmQ3ykp7xoUfXj-wTk2kM9v3aQ65wS3bsn5CI8",
"types" : [ "lawyer", "establishment" ],
"url" : "https://plus.google.com/113831070987505202557/about?hl=en-US",
"utc_offset" : 480,
"vicinity" : "10号 East Chang'an Avenue, Dongcheng, Beijing",
"website" : "http://www.splf.com.cn/"
},
"status" : "OK"
}
这个 json 的 pojo 映射是
public class PoiDetail extends GenericJson implements Serializable{
@Key
public String status;
@Key
public PoiDetailPlace result;
}
public class PoiDetailPlace implements Serializable{
@Key
public String id;
@Key
public String name;
@Key
public String reference;
@Key
public String icon;
@Key
public String vicinity;
@Key
public String formatted_address;
@Key
public String formatted_phone_number;
@Key
public String international_phone_number;
@Key
public String website;
@Key
public Geometry geometry;
public static class Geometry implements Serializable{
@Key
public Location location;
}
public static class Location implements Serializable{
@Key
public double lat;
@Key
public double lng;
}
}
当我编写解析器方法时发生了奇怪的事情。如果我这样写我的代码,没关系。我可以获得详细信息:
public PoiDetail getPoiDetail(String reference) throws IOException{
try {
HttpRequestFactory httpRequestFactory = createRequestFactory(HTTP_TRANSPORT);
HttpRequest request = httpRequestFactory
.buildGetRequest(new GenericUrl(DETAILS_SEARCH_URL));
request.getUrl().put("key", API_KEY);
request.getUrl().put("reference", reference);
request.getUrl().put("sensor", "false");
PoiDetail detail = request.execute().parseAs(PoiDetail.class);
return detail;
} catch (HttpResponseException e) {
// TODO Auto-generated catch block
Log.e("Error in Perform Details", e.getMessage());
throw e;
}
}
如果我这样写代码是不行的了解异常是什么:
public PoiDetail getPoiDetail(String reference){
try {
HttpRequestFactory httpRequestFactory = createRequestFactory(HTTP_TRANSPORT);
HttpRequest request = httpRequestFactory
.buildGetRequest(new GenericUrl(DETAILS_SEARCH_URL));
request.getUrl().put("key", API_KEY);
request.getUrl().put("reference", reference);
request.getUrl().put("sensor", "false");
PoiDetail detail = request.execute().parseAs(PoiDetail.class);
return detail;
} catch (HttpResponseException e) {
e.printStackTrace();
return null;
}catch (IOException e) {
e.printStackTrace();
return null;
}
}
我想知道
是什么导致这两种方法产生不同的结果?
为什么我不应该抓住 IOException ?
这背后的理论是什么?