首先先介绍java方法:
首先要增加一个关于异步IO需要的包:
1、async-http-client包,可以在这里下载:https://oss.sonatype.org/content/repositories/releases/com/ning/async-http-client/1.6.2/
2、log4j的包,这个不用我说了,都知道在哪里
3、slf4j-api 的包,目前用1.5以上的版本比较多。
4、slf4j-log4j 的包,可以看出,slf4j是在log4j基础上包装的。
OK,就这几个了,弄好后再看看下面这段代码,通过使用它,性能可以得到明显改善:
AsyncHttpClient client = new AsyncHttpClient();
try {
Future<Response> f = client.prepareGet("http://www.google.com.hk/").execute();
System.out.println(f.get().getResponseBody("Big5"));//谷歌的输出编码集为Big5,反向解析结果的时候使用
}catch(...) {....}
这段代码是不是超级简单,可以通过上面描述的三种方式:
1、直接调用
2、将GetMethod或PostMethod对象作为共享对象反复使用。
3、使用AsyncHttpClient
2,下面介绍如何通过经纬度获取地址信息?
Google Maps API Web Services,是一个为您的地图应用程序提供地理数据的 Google 服务的 HTTP 接口集合。具体包括:Google Geocoding API、Google Directions API、Google Elevation API、Google Places API。本文将探讨如何通过Google Geocoding API服务来获取地址信息。
Google Maps API 提供这些网络服务作为从外部服务中请求 Google Maps API 数据以及在您的地图应用程序中使用它们的接口。这些网络服务使用特定网址的 HTTP 请求并将网址参数作为参数提供给服务。一般来讲,这些服务会在 HTTP 请求中以 JSON 或 XML 的形式传回数据,供您的应用程序进行解析和/或处理。
一个典型的网络服务请求通常采用以下形式:
http://maps.google.com/maps/api/{service}/{output}?{parameters}
其中 service 表示所请求的特定服务,output 表示响应格式(通常为 json 或 xml)。
地址查询(反向地址解析)请求
Google Geocoding API 请求必须采用以下形式:
http://maps.google.com/maps/api/geocode/output?parameters
其中,output 可以是以下值之一:
json(建议)表示以 JavaScript 对象表示法 (JSON) 的形式输出
xml 表示以 XML 的形式输出
有些参数是必需的,有些是可选的。根据网址的标准,所有参数均使用字符 & (&) 分隔。下面枚举了这些参数及其可能的值。
Google Geocoding API 使用以下网址参数定义地址查询请求:
latlng(必需)- 您希望获取的、距离最近的、可人工读取地址的纬度/经度文本值。
bounds(可选)- 要在其中更显著地偏移地址解析结果的可视区域的边框。
region(可选)- 区域代码,指定为 ccTLD(“顶级域”)双字符值。
language(可选)- 传回结果时所使用的语言。请注意,我们会经常更新支持的语言,因此该列表可能并不详尽。如果未提供 language,地址解析器将尝试尽可能使用发送请求的区域的本地语言。
sensor(必需)- 指示地址解析请求是否来自装有位置传感器的设备。该值必须为 true 或 false。
注意:bounds 和 region 参数只会影响地址解析器返回的结果,但不能对其进行完全限制。
实例一:创建查询坐标(39.910093,116.403945)的地址信息的请求,要求以xml格式输出响应,语言为简体中文(zh-CN)。
http://maps.google.com/maps/api/geocode/xml?latlng=39.910093,116.403945&language=zh-CN&sensor=false
注意:经纬度书写的顺序为(纬度,经度)
返回json格式数据如下:
{
"results" : [
{
"address_components" : [
{
"long_name" : "128号",
"short_name" : "128号",
"types" : [ "street_number" ]
},
{
"long_name" : "南池子大街",
"short_name" : "南池子大街",
"types" : [ "route" ]
},
{
"long_name" : "东城区",
"short_name" : "东城区",
"types" : [ "sublocality", "political" ]
},
{
"long_name" : "北京",
"short_name" : "北京",
"types" : [ "locality", "political" ]
},
{
"long_name" : "北京市",
"short_name" : "北京市",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "中国",
"short_name" : "CN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "中国北京市东城区南池子大街128号",
"geometry" : {
"location" : {
"lat" : 39.9102420,
"lng" : 116.4039110
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 39.91159098029149,
"lng" : 116.4052599802915
},
"southwest" : {
"lat" : 39.90889301970849,
"lng" : 116.4025620197085
}
}
},
"postcode_localities" : [],
"types" : [ "street_address" ]
},
{
"address_components" : [
{
"long_name" : "皇史宬站",
"short_name" : "皇史宬站",
"types" : [ "bus_station", "transit_station", "establishment" ]
},
{
"long_name" : "东城区",
"short_name" : "东城区",
"types" : [ "sublocality", "political" ]
},
{
"long_name" : "北京",
"short_name" : "北京",
"types" : [ "locality", "political" ]
},
{
"long_name" : "北京市",
"short_name" : "北京市",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "中国",
"short_name" : "CN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "中国北京市东城区皇史宬",
"geometry" : {
"location" : {
"lat" : 39.9099750,
"lng" : 116.4032030
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 39.91132398029149,
"lng" : 116.4045519802915
},
"southwest" : {
"lat" : 39.90862601970849,
"lng" : 116.4018540197085
}
}
},
"postcode_localities" : [],
"types" : [ "bus_station", "transit_station", "establishment" ]
},
{
"address_components" : [
{
"long_name" : "东城区",
"short_name" : "东城区",
"types" : [ "sublocality", "political" ]
},
{
"long_name" : "北京",
"short_name" : "北京",
"types" : [ "locality", "political" ]
},
{
"long_name" : "北京市",
"short_name" : "北京市",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "中国",
"short_name" : "CN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "中国北京市东城区",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 39.97403660,
"lng" : 116.45239070
},
"southwest" : {
"lat" : 39.85862530,
"lng" : 116.37713420
}
},
"location" : {
"lat" : 39.92835300000001,
"lng" : 116.4163570
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 39.97403660,
"lng" : 116.45239070
},
"southwest" : {
"lat" : 39.85862530,
"lng" : 116.37713420
}
}
},
"postcode_localities" : [],
"types" : [ "sublocality", "political" ]
},
{
"address_components" : [
{
"long_name" : "北京市",
"short_name" : "北京市",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "中国",
"short_name" : "CN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "中国北京市",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 41.06081590,
"lng" : 117.5146250
},
"southwest" : {
"lat" : 39.44275810,
"lng" : 115.42341160
}
},
"location" : {
"lat" : 39.904030,
"lng" : 116.4075260
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 41.06081590,
"lng" : 117.5146250
},
"southwest" : {
"lat" : 39.44275810,
"lng" : 115.42341160
}
}
},
"postcode_localities" : [],
"types" : [ "administrative_area_level_1", "political" ]
},
{
"address_components" : [
{
"long_name" : "北京",
"short_name" : "北京",
"types" : [ "locality", "political" ]
},
{
"long_name" : "北京市",
"short_name" : "北京市",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "中国",
"short_name" : "CN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "中国北京市",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 41.06081590,
"lng" : 117.5146250
},
"southwest" : {
"lat" : 39.44275810,
"lng" : 115.42341160
}
},
"location" : {
"lat" : 39.904030,
"lng" : 116.4075260
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 40.21649620,
"lng" : 116.78298350
},
"southwest" : {
"lat" : 39.66127140,
"lng" : 116.01193430
}
}
},
"postcode_localities" : [],
"types" : [ "locality", "political" ]
},
{
"address_components" : [
{
"long_name" : "中国",
"short_name" : "CN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "中国",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 53.56097399999999,
"lng" : 134.77280990
},
"southwest" : {
"lat" : 18.15352160,
"lng" : 73.49941360
}
},
"location" : {
"lat" : 35.861660,
"lng" : 104.1953970
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 53.56097399999999,
"lng" : 134.77280990
},
"southwest" : {
"lat" : 18.15352160,
"lng" : 73.49941360
}
}
},
"postcode_localities" : [],
"types" : [ "country", "political" ]
}
],
"status" : "OK"
}