Androidでは、google-http-java-clientライブラリを使用してgoogleplaceapiからjsonを解析します。これらのウェブサイトgoogle-http-java-client-json とAndroidのGooglePlacesandMapsチュートリアルの操作に関する指示に従いました
私はグーグルの場所の詳細検索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;
}
}
このようなコードを書くと動作しません。「request.execute()。parseAs(PoiDetail.class);」を実行すると、catchブロックに入りますが、手がかりはありません。デバッグすると、動作しません。例外を取得します。
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;
}
}
私は疑問に思う
2つの方法を異なる結果に導くものは何ですか?
IOExceptionをキャッチしないのはなぜですか?
この下の理論は何ですか?