2013-07-26 95 views
1

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

我试图hostipgeoip都引发异常,对我没有工作。有没有其他方法可以获得地理定位?

当我使用geoip的我:

Config.groovy中:

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

在我的控制器:

GeoIpService geoIpService 

index() { 
    def location = geoIpService.getLocation("85.176.52.75") 
    render location.countryName + " " + location.city  
} 

唯一的例外是:

| 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 
+1

你获得geoip的例外是什么?我们在我们的java/spring应用程序中使用它,没有任何问题。 –

+0

@JohnFarrelly我把例外置于我的问题中。任何想法? – confile

+0

你确定你的DAT文件没有被损坏吗?也许重新下载它? –

回答

2

下载后你的样品来自github的ils应用程序,经过一段时间的调试后,我发现问题是什么。那么,有2个问题真:

  • 你的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 

那么,它实际上是在寻找的是整数值,而不是它们旁边的字符串。例如,用于GEOIP_STANDARD 0,所以你用它配置:

geoip.data.cache=0 

当我改变了您的示例应用程序的配置上面,我不再得到例外。

+0

我尝试了你所说的,但它会导致与以前一样的错误。我创建了一个示例项目来向你展示我的意思。你会在这里找到这个项目:https://github.com/confile/geoip只需启动项目,你会看到错误。我确实如你所说。 – confile

+0

@confile我想到了我们的问题,我已经更新了我的回答 –

+0

@confile你是否正常工作? –