在scala中调用mechanize失败:HtmlDocument无法强制转换为scala.runtime.Nothin

时间:2014-06-23 14:48:06

标签: scala

我有以下代码使用mechanize(可以从https://github.com/GistLabs/mechanize下载)

import com.gistlabs.mechanize._
val agent = new MechanizeAgent();
val page = agent.get("http://www.ask.com");

未能说出以下内容,我怎么能让它起作用?

scala> agent.get("http://www.ask.com")
java.lang.ClassCastException: com.gistlabs.mechanize.document.html.HtmlDocument cannot be cast to scala.runtime.Nothing$
    at .<init>(<console>:12)
    at .<clinit>(<console>)
    at .<init>(<console>:7)
    at .<clinit>(<console>)
    at $print(<console>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)

1 个答案:

答案 0 :(得分:1)

问题是MechanizeAgent.get是一种通用方法(参见line 182 in MechanizeAgent.java)。所以你必须告诉Scala期望什么类型。要么是这样的:

import com.gistlabs.mechanize._
import com.gistlabs.mechanize.document._

val agent = new MechanizeAgent();
val page = agent.get[Document]("http://www.ask.com");

或者像这样:

import com.gistlabs.mechanize._
import com.gistlabs.mechanize.document._

val agent = new MechanizeAgent();
val page: Document = agent.get("http://www.ask.com");