2016-04-23 377 views
0

無法當我運行WURFL演示應用程序斯卡拉使用WURFL使用Scala

object Demo { 

    def main(args: Array[String]) { 

    // Create WURFL passing a GeneralWURFLEngine object with a wurfl xml 
    val wurflWrapper = new Wurfl(new GeneralWURFLEngine("classpath:/resources/wurfl.zip")) 

    // Set cache provider 
    wurflWrapper.setCacheProvider(new LRUMapCacheProvider) 

    // Set Performance/Accuracy Mode 
    wurflWrapper.setTargetAccuracy 

    // Set Capability Filter 
    wurflWrapper.setFilter(
     "can_assign_phone_number", 
     "marketing_name", 
     "brand_name", 
     "model_name", 
     "is_smarttv", 
     "is_wireless_device", 
     "device_os", 
     "device_os_version", 
     "is_tablet", 
     "ux_full_desktop", 
     "pointing_method", 
     "preferred_markup", 
     "resolution_height", 
     "resolution_width", 
     "xhtml_support_level") 

    // User-Agent here 
    var userAgent = "" 

    // Defining headers 
    var headers = Map("Accept-Datetime"->"Thu, 31 May 2007 20:35:00 GMT") 
    headers += ("Content-Type"-> "application/x-www-form-urlencoded") 

    var device = wurflWrapper.deviceForHeaders(userAgent, headers) 

    val matchType = device.matchType 
    if (matchType == MatchType.conclusive) 
    { 
     println("Match Type is conclusive") 
    } 

    val wireless = device.capability("is_wireless_device") 
    println("Is wireless: " + wireless) 

    } 
} 

我得到這個異常:

[main] ERROR net.sourceforge.wurfl.core.GeneralWURFLEngine - cannot initialize: java.lang.NullPointerException: in is null 
java.lang.NullPointerException: in is null 
    at java.util.zip.ZipInputStream.<init>(ZipInputStream.java:101) 
    at java.util.zip.ZipInputStream.<init>(ZipInputStream.java:80) 
    at net.sourceforge.wurfl.core.resource.FileLoader.fromZipFile(FileLoader.java:248) 
    at net.sourceforge.wurfl.core.resource.FileLoader.openInputStream(FileLoader.java:230) 
    at net.sourceforge.wurfl.core.resource.FileLoader.getStream(FileLoader.java:288) 
    at net.sourceforge.wurfl.core.resource.XMLResource.getData(XMLResource.java:163) 
    at net.sourceforge.wurfl.core.resource.DefaultWURFLModel.init(DefaultWURFLModel.java:115) 
    at net.sourceforge.wurfl.core.resource.DefaultWURFLModel.<init>(DefaultWURFLModel.java:107) 
    at net.sourceforge.wurfl.core.GeneralWURFLEngine.init(GeneralWURFLEngine.java:340) 
    at net.sourceforge.wurfl.core.GeneralWURFLEngine.initIfNeeded(GeneralWURFLEngine.java:319) 
    at net.sourceforge.wurfl.core.GeneralWURFLEngine.getDeviceForRequest(GeneralWURFLEngine.java:451) 
    at com.scientiamobile.wurfl.Wurfl.deviceForHeaders(Wurfl.scala:77) 
    at com.Demo$.main(Demo.scala:49) 
    at com.Demo.main(Demo.scala) 
Exception in thread "main" net.sourceforge.wurfl.core.exc.WURFLRuntimeException: WURFL unexpected exception 
    at net.sourceforge.wurfl.core.GeneralWURFLEngine.initIfNeeded(GeneralWURFLEngine.java:322) 
    at net.sourceforge.wurfl.core.GeneralWURFLEngine.getDeviceForRequest(GeneralWURFLEngine.java:451) 
    at com.scientiamobile.wurfl.Wurfl.deviceForHeaders(Wurfl.scala:77) 
    at com.Demo$.main(Demo.scala:49) 
    at com.Demo.main(Demo.scala) 
Caused by: java.lang.NullPointerException: in is null 
    at java.util.zip.ZipInputStream.<init>(ZipInputStream.java:101) 
    at java.util.zip.ZipInputStream.<init>(ZipInputStream.java:80) 
    at net.sourceforge.wurfl.core.resource.FileLoader.fromZipFile(FileLoader.java:248) 
    at net.sourceforge.wurfl.core.resource.FileLoader.openInputStream(FileLoader.java:230) 
    at net.sourceforge.wurfl.core.resource.FileLoader.getStream(FileLoader.java:288) 
    at net.sourceforge.wurfl.core.resource.XMLResource.getData(XMLResource.java:163) 
    at net.sourceforge.wurfl.core.resource.DefaultWURFLModel.init(DefaultWURFLModel.java:115) 
    at net.sourceforge.wurfl.core.resource.DefaultWURFLModel.<init>(DefaultWURFLModel.java:107) 
    at net.sourceforge.wurfl.core.GeneralWURFLEngine.init(GeneralWURFLEngine.java:340) 
    at net.sourceforge.wurfl.core.GeneralWURFLEngine.initIfNeeded(GeneralWURFLEngine.java:319) 
    ... 4 more 

的 「wurfl.zip」 位置很好,在 「資源」 。 我也嘗試將它添加到主Scala類路徑,但仍然不是運氣。

+0

Luca Passani,ScientiaMobile在這裏的首席技術官。我現在向工程團隊發送一個msg信息,並要求他們在週一早上看看這個信息 –

+0

同時,您可能想要提供更多有關您正在使用的庫和wurfl.xml版本的更多信息 –

+0

Thanks Luca,Java ver:1.7.1.0,Scala ver:1.7.1.0。當我使用直接文件名而不是:classpath:/resources/wurfl.zip它似乎工作正常。 – bashan

回答

1

從代碼的角度

val wurflWrapper = new Wurfl(new GeneralWURFLEngine("classpath:/resources/wurfl.zip"))

是初始化你WURFL引擎有道。

如果您在IDE(IDEA,Eclipse或Netbeans)中運行它,或者使用命令行或其他方式,您可能想要提供有關如何運行演示的信息。告訴你是否使用Maven也是有用的。

如果您使用命令行運行它,請提供您如何啓動Scala應用程序以及如何設置類路徑的示例。

假設這樣一個場景,你正在使用Maven編譯和使用-cp類直接執行項目進入目標目錄,執行會導致你的類路徑錯誤,因爲資源文件不在classes目錄包括在內。

請確保wurfl-scala-example-.jar包含在您的類路徑中。

如果您在IntelliJ IDEA中使用Demo項目,請確保資源目錄被標記爲「資源」,否則IDEA運行工具將不包含wurfl.zip文件作爲資源。

希望這會有所幫助。

+0

正如你注意到的,直接使用文件名或路徑也是初始化WURFL的有效方法。 –