我想从Grails应用程序的IP地址中检索地理位置。

我尝试过hostipgeoip都引发异常,但对我不起作用。还有其他方法可以获取地理位置信息吗?

当我使用geoip时,我有:

config.groovy:

geoip.data.resource= "/WEB-INF/GeoLiteCity.dat" 
geoip.data.cache="GEOIP_STANDARD" 

在我的 Controller 中:
GeoIpService geoIpService 
 
index() { 
    def location = geoIpService.getLocation("85.176.52.75") 
    render location.countryName + " " + location.city        
} 

异常(exception)是:
| Error 2013-07-26 14:04:22,236 [http-bio-8090-exec-1] ERROR    
errors.GrailsExceptionResolver  - NullPointerException occurred when processing request:   [GET] /test/home/index 
Stacktrace follows: 
Message: null 
Line | Method 
->>  199 | <init>                    in java.util.StringTokenizer 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
|    221 | <init>                    in     '' 
|    624 | getLocationwithdnsservice in com.maxmind.geoip.LookupService 
|    593 | getLocation               in     '' 
|     42 | getLocation . . . . . . . in org.grails.geoip.service.GeoIpService 
|     12 | index                     in test.HomeController 
|    195 | doFilter . . . . . . . .  in     
grails.plugin.cache.web.filter.PageFragmentCachingFilter 
|     63 | doFilter                  in grails.plugin.cache.web.filter.AbstractFilter 
|   1145 | runWorker . . . . . . . . in java.util.concurrent.ThreadPoolExecutor 
|    615 | run                       in java.util.concurrent.ThreadPoolExecutor$Worker 
^    722 | run . . . . . . . . . . . in java.lang.Thread 

请您参考如下方法:

从github下载示例grails应用程序,并调试了一段时间后,我发现了问题所在。好吧,确实有两个问题:

  • 您的geoip插件配置不正确
  • geoip插件的文档使您看起来像您所拥有的配置正确,反之则不正确。

  • 如果您查看插件的文档: http://grails.org/plugin/geoip,您将看到它讨论了 geoip.data.cache的配置,如下所示:
    geoip.data.cache - There are 4 possible values for this: 
    0 - GEOIP_STANDARD 
    1 - GEOIP_MEMORY_CACHE 
    2 - GEOIP_CHECK_CACHE 
    4 - GEOIP_INDEX_CACHE 
    

    So what it is actually looking for is the integer values, not the strings beside them. For example, 0 is used for GEOIP_STANDARD, so you configure it with:

    geoip.data.cache=0 
    

    当我将示例应用程序的配置更改为上述配置时,我不再遇到异常。


    评论关闭
    IT序号网

    微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!