2013-07-26 94 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你是否正常工作? –