1

我根本无法在 scalaj-http 中执行获取请求:

 Http("https://someUrl.com")
  .option(HttpOptions.connTimeout(6000))
  .option(HttpOptions.readTimeout(15000))
  .asString

Exception in thread "main" java.lang.NoSuchMethodError: scala.collection.mutable.StringBuilder.append([CII)Lscala/collection/mutable/StringBuilder;
    at scalaj.http.Http$.readOnce$1(Http.scala:163)
    at scalaj.http.Http$.readString(Http.scala:167)
    at scalaj.http.Http$Request$$anonfun$asString$1.apply(Http.scala:128)
    at scalaj.http.Http$Request$$anonfun$asString$1.apply(Http.scala:128)
    at scalaj.http.Http$.tryParse(Http.scala:148)
    at scalaj.http.Http$Request$$anonfun$apply$1.apply(Http.scala:85)
    at scalaj.http.Http$Request$$anonfun$apply$1.apply(Http.scala:85)
    at scalaj.http.Http$Request.process(Http.scala:99)
    at scalaj.http.Http$Request.apply(Http.scala:85)
    at scalaj.http.Http$Request.asString(Http.scala:128)
    at com.me.method1(API.scala:21)
    at com.me.Application$delayedInit$body.apply(Application.scala:5)
    at scala.Function0$class.apply$mcV$sp(Function0.scala:40)
    at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
    at scala.App$$anonfun$main$1.apply(App.scala:71)
    at scala.App$$anonfun$main$1.apply(App.scala:71)
    at scala.collection.immutable.List.foreach(List.scala:318)
    at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:32)
    at scala.App$class.main(App.scala:71)
    at com.me.Application$.main(Application.scala:3)
    at com.me.Application.main(Application.scala)
    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)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

如果我添加此选项

Http("https://someUrl.com")
  .option(HttpOptions.connTimeout(6000))
  .option(HttpOptions.readTimeout(15000))
  .option(HttpOptions.allowUnsafeSSL)
  .asString

然后错误变为:

Exception in thread "main" java.lang.NoClassDefFoundError: scala/reflect/ClassManifest$
    at scalaj.http.HttpOptions$$anonfun$allowUnsafeSSL$1.apply(Http.scala:30)
    at scalaj.http.HttpOptions$$anonfun$allowUnsafeSSL$1.apply(Http.scala:23)
    at scalaj.http.Http$Request$$anonfun$process$2.apply(Http.scala:95)
    at scalaj.http.Http$Request$$anonfun$process$2.apply(Http.scala:95)
    at scala.collection.immutable.List.foreach(List.scala:318)
    at scalaj.http.Http$Request.process(Http.scala:95)
    at scalaj.http.Http$Request.apply(Http.scala:85)
    at scalaj.http.Http$Request.asString(Http.scala:128)
    at com.me.API.getJsessionId(API.scala:21)
    at com.me.Application$delayedInit$body.apply(Application.scala:5)
    at scala.Function0$class.apply$mcV$sp(Function0.scala:40)
    at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
    at scala.App$$anonfun$main$1.apply(App.scala:71)
    at scala.App$$anonfun$main$1.apply(App.scala:71)
    at scala.collection.immutable.List.foreach(List.scala:318)
    at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:32)
    at scala.App$class.main(App.scala:71)
    at com.me.Application$.main(Application.scala:3)
    at com.me.Application.main(Application.scala)
    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)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.lang.ClassNotFoundException: scala.reflect.ClassManifest$
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 24 more

该网址肯定可用。事实上,无论我使用什么 url(甚至不是 https),它都会导致错误:

Exception in thread "main" java.lang.NoSuchMethodError: scala.collection.mutable.StringBuilder.append([CII)Lscala/collection/mutable/StringBuilder;
    at scalaj.http.Http$.readOnce$1(Http.scala:163)
    at scalaj.http.Http$.readString(Http.scala:167)
    at scalaj.http.Http$Request$$anonfun$asString$1.apply(Http.scala:128)
    at scalaj.http.Http$Request$$anonfun$asString$1.apply(Http.scala:128)
    at scalaj.http.Http$.tryParse(Http.scala:148)
    at scalaj.http.Http$Request$$anonfun$apply$1.apply(Http.scala:85)
    at scalaj.http.Http$Request$$anonfun$apply$1.apply(Http.scala:85)
    at scalaj.http.Http$Request.process(Http.scala:99)
    at scalaj.http.Http$Request.apply(Http.scala:85)
    at scalaj.http.Http$Request.asString(Http.scala:128)
    at com.me.method(API.scala:22)
    at com.me.Application$delayedInit$body.apply(Application.scala:5)
    at scala.Function0$class.apply$mcV$sp(Function0.scala:40)
    at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
    at scala.App$$anonfun$main$1.apply(App.scala:71)
    at scala.App$$anonfun$main$1.apply(App.scala:71)
    at scala.collection.immutable.List.foreach(List.scala:318)
    at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:32)
    at scala.App$class.main(App.scala:71)

构建.sbt

scalaVersion := "2.10.2"

libraryDependencies += "org.scalaj" % "scalaj-http_2.8.1" % "0.3.2"

PS 如果有更好的 Scala http 库,请继续向我推荐。

4

1 回答 1

5

您正在尝试使用针对 scala 2.8.1 编译的库(该库已于 3 多年前发布并且已经过时),而您使用的是 scala 2.10。主要的 scala 版本是二进制不兼容的。解决方案是使用正确版本的 scalaj 工件或降级您的 scala 版本。

于 2013-10-06T13:25:24.183 回答