当前位置:   article > 正文

IP地址解析

机器学习ip地址解析代码

说明

本篇用于记录一个简单的通过ip地址解析定位的方法,主要是为了便于记忆,代码为主.

ip解析

通过ip解析地址,我采用的是网上免费的ip解析接口freegeoip,为github开源项目,采用的是免费的开源ip库。解析准确率较高,ip库也较为丰富(就目前而言,比淘宝等的接口的ip库包含数量大)。

freegeoip介绍

freegeoip的实现是通过go完成的,可以直接安装在自己的机器,也可以使用其提供的免费接口:http://freegeoip.net/ 。格式如下:

  1. /*
  2. *data_type:为请求返回数据格式,目前支持:json和xml
  3. *ip_address:请求解析ip地址,可以为域名格式,也可以不填,默认为发起
  4. *请求地址
  5. *http://freegeoip.net/json/www.baidu.com
  6. */
  7. String URL=
  8. http://freegeoip.net/(data_type)/(ip_address)”
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

1) 返回数据格式
数据返回格式为两种json或xml,如下:

  • json格式
{"ip":"103.235.46.39","country_code":"HK","country_name":"Hong Kong","region_code":"","region_name":"","city":"Central District","zip_code":"","time_zone":"Asia/Hong_Kong","latitude":22.291,"longitude":114.15,"metro_code":0}
  • 1
  • xml格式
  1. <Response>
  2. <IP>103.235.46.39</IP>
  3. <CountryCode>HK</CountryCode>
  4. <CountryName>Hong Kong</CountryName>
  5. <RegionCode/>
  6. <RegionName/>
  7. <City>Central District</City>
  8. <ZipCode/>
  9. <TimeZone>Asia/Hong_Kong</TimeZone>
  10. <Latitude>22.291</Latitude>
  11. <Longitude>114.15</Longitude>
  12. <MetroCode>0</MetroCode>
  13. </Response>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

2) 返回参数
返回数据参数包括:请求IP、经纬度、当前的城市名称、城市编码、时区等信息。如下,为一些基本的参数信息介绍(以json格式为例,xml类似)。

参数名数据格式参数说明
ip103.235.46.39请求IP
country_codeCN国家(地区)编码
country_nameChina国家(地区)名称
region_code31地区(省级,Hong_Kong不包含)编码
region_nameShanghai地区名称(省级)
cityShanghai城市名称
zip_code(基本没有获取成功过)邮政编码
time_zoneAsia/Shanghai时区
latitude31.0456纬度
longitude121.3997经度
metro_code0(基本都是0)地铁代码
ip解析实现

如下,为基本的ip地址解析使用(为实现的主要代码).

  1. /**
  2. *位置信息
  3. */
  4. public class LatLng {
  5. private double latitude; //纬度
  6. private double longitude;//经度
  7. private String country;
  8. private String enCountry;
  9. private String countryCode;
  10. private String city;
  11. private String enCity;
  12. private String cityCode;
  13. public String getCountry() {
  14. return country;
  15. }
  16. public void setCountry(String country) {
  17. this.country = country;
  18. }
  19. public String getEnCountry() {
  20. return enCountry;
  21. }
  22. public void setEnCountry(String enCountry) {
  23. this.enCountry = enCountry;
  24. }
  25. public String getCountryCode() {
  26. return countryCode;
  27. }
  28. public void setCountryCode(String countryCode) {
  29. this.countryCode = countryCode;
  30. }
  31. public String getCity() {
  32. return city;
  33. }
  34. public void setCity(String city) {
  35. this.city = city;
  36. }
  37. public String getEnCity() {
  38. return enCity;
  39. }
  40. public void setEnCity(String enCity) {
  41. this.enCity = enCity;
  42. }
  43. public String getCityCode() {
  44. return cityCode;
  45. }
  46. public void setCityCode(String cityCode) {
  47. this.cityCode = cityCode;
  48. }
  49. public Type getType() {
  50. return type;
  51. }
  52. public void setType(Type type) {
  53. this.type = type;
  54. }
  55. private Type type=Type.NORMAL;
  56. /**
  57. * 控制LatLng的类型
  58. */
  59. public enum Type{
  60. NORMAL("normal"),
  61. GEO_LATLNG("geo");
  62. private final String type;
  63. private Type(String type){
  64. this.type=type;
  65. }
  66. @Override
  67. public String toString() {
  68. return this.type;
  69. }
  70. }
  71. public LatLng(){
  72. }
  73. /**
  74. *
  75. * @param latitude 纬度
  76. * @param longitude 经度
  77. */
  78. public LatLng(double latitude,double longitude){
  79. this(latitude,longitude,Type.NORMAL);
  80. }
  81. public LatLng(double latitude,double longitude,Type type){
  82. this(latitude,longitude,"","",type);
  83. }
  84. public LatLng(double latitude,double longitude,String country,String city){
  85. this(latitude,longitude,country,city,Type.NORMAL);
  86. }
  87. public LatLng(double latitude,double longitude,String country,String city,Type type){
  88. this(latitude,longitude,country,city,"","",type);
  89. }
  90. public LatLng(double latitude,double longitude,String country,String city,String enCountry,String enCity){
  91. this(latitude,longitude,country,city,enCountry,enCity,Type.NORMAL);
  92. }
  93. public LatLng(double latitude,double longitude,String country,String city,String enCountry,String enCity,Type type){
  94. this.latitude=latitude;
  95. this.longitude=longitude;
  96. this.country=country;
  97. this.city=city;
  98. this.enCountry=enCountry;
  99. this.enCity=enCity;
  100. this.type=type;
  101. }
  102. /**
  103. * @return
  104. */
  105. public boolean isGeoType(){
  106. if (type==Type.GEO_LATLNG){
  107. return true;
  108. }
  109. return false;
  110. }
  111. /**
  112. *
  113. * @return
  114. * 返回String格式,用","分割
  115. */
  116. public String geLocation(){
  117. if (type==Type.NORMAL){
  118. return latitude+","+longitude;
  119. }else if (type==Type.GEO_LATLNG){
  120. return longitude+","+latitude;
  121. }else {
  122. return null;
  123. }
  124. }
  125. /**
  126. * 自定义获取location格式
  127. * @param type
  128. * @return
  129. */
  130. public String getLocation(Type type){
  131. if (type==Type.NORMAL){
  132. return latitude+","+longitude;
  133. }else if (type==Type.GEO_LATLNG){
  134. return longitude+","+latitude;
  135. }else {
  136. return null;
  137. }
  138. }
  139. @Override
  140. public String toString() {
  141. return "LatLng{" +
  142. "latitude=" + latitude +
  143. ", longitude=" + longitude +
  144. ", type=" + type +
  145. '}';
  146. }
  147. public double getLatitude() {
  148. return latitude;
  149. }
  150. public void setLatitude(double latitude) {
  151. this.latitude = latitude;
  152. }
  153. public double getLongitude() {
  154. return longitude;
  155. }
  156. public void setLongitude(double longitude) {
  157. this.longitude = longitude;
  158. }
  159. }
  160. //如下为解析部分代码
  161. /**
  162. *
  163. * @param urlPath
  164. * @return get方式http请求获取返回值
  165. */
  166. private ReqMessage getHttpGet(String urlPath){
  167. HttpURLConnection connection=null;
  168. ReqMessage reqMessage=null;
  169. try {
  170. URL url = new URL(urlPath);
  171. connection = (HttpURLConnection) url.openConnection();
  172. connection.setRequestProperty("accept", "*/*");
  173. connection.setRequestProperty("connection", "Keep-Alive");
  174. connection.setRequestProperty("Accept-Charset", "utf-8");
  175. connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  176. // 建立实际的连接
  177. connection.connect();
  178. reqMessage=new ReqMessage(connection.getResponseCode(),connection.getResponseMessage(),connection);
  179. // HllLogUtils.e(TAG,"reqMessage:"+reqMessage.toString());
  180. if (reqMessage.getReq_code()==connection.HTTP_OK){
  181. // HllLogUtils.e(TAG,"HTTP Request is success.");
  182. HllLogUtils.i(TAG,"message content is:"+reqMessage.getContent());
  183. }else {
  184. HllLogUtils.e(TAG," HTTP Request is not success, Response code is "+reqMessage.getReq_code()+" ,and failed message is:" + reqMessage.getReq_message());
  185. }
  186. }catch (Exception e){
  187. e.printStackTrace();
  188. if (TextUtils.isEmpty(e.getMessage())){
  189. // HllLogUtils.e(TAG,e.getMessage());
  190. }
  191. }finally {
  192. if (connection!=null){
  193. connection.disconnect();
  194. }
  195. }
  196. if (reqMessage==null){
  197. reqMessage=new ReqMessage();
  198. }
  199. return reqMessage;
  200. }
  201. /**
  202. * 通过ip反向定位
  203. * @return
  204. */
  205. @Override
  206. public LatLng loadIpGeoLocationInfo() {
  207. ReqMessage reqMessage=getHttpGet(IP_GEO_LOCATION_DOMAIN);
  208. LatLng latLng=null;
  209. String country_code=null;
  210. String country_name=null;
  211. String city_name=null;
  212. if (reqMessage!=null && !TextUtils.isEmpty(reqMessage.getContent())){
  213. //获取解析返回
  214. try {
  215. JSONObject jsonObject = new JSONObject(reqMessage.getContent());
  216. String latitude=jsonObject.getString("latitude");
  217. String longitude=jsonObject.getString("longitude");
  218. country_code=jsonObject.getString("country_code");
  219. country_name=jsonObject.getString("country_name");
  220. city_name=jsonObject.getString("city");
  221. if (TextUtils.isEmpty(latitude) || TextUtils.isEmpty(longitude)) {
  222. // HllLogUtils.e(TAG,"ip parse location error.");
  223. }else if(this.latLng==null) {
  224. latLng=new LatLng(Double.parseDouble(latitude.trim()), Double.parseDouble(longitude.trim()));
  225. }else {
  226. latLng=this.latLng;
  227. }
  228. }catch (JSONException jsonException){
  229. jsonException.printStackTrace();
  230. // HllLogUtils.e(TAG,jsonException.getMessage());
  231. }
  232. if (latLng!=null){
  233. HllSharedPreferences hllSharedPreferences =new HllSharedPreferences(mContext);
  234. hllSharedPreferences.setLatitude(latLng.getLatitude()+"");
  235. hllSharedPreferences.setLongitude(latLng.getLongitude()+"");
  236. this.latLng=latLng;
  237. /**
  238. * 处理国内外情况
  239. */
  240. if (!TextUtils.isEmpty(country_code) && country_code.equals("CN")){
  241. /**
  242. * 中国
  243. */
  244. hllSharedPreferences.setCountry("中国");
  245. hllSharedPreferences.setEnCountry(country_name);
  246. hllSharedPreferences.setEnCity(city_name);
  247. loadLocationInfoCN();
  248. }else {
  249. /**
  250. * 国外
  251. */
  252. loadLocationInfoOverSeas();
  253. }
  254. }
  255. }
  256. return this.latLng;
  257. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322

Enjoytoday,Enjoytcoding

转载于:https://www.cnblogs.com/amiko/p/7906216.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/449111
推荐阅读
相关标签
  

闽ICP备14008679号