我想使用以下代码查询Wikipedia API:
class WikiArticle(url : String) {
var text : WSRequest = null
def queryApi(): Unit = {
text = WS.url(url)
println(text);
}
}
我从这里调用代码:
val wikiArticle = new WikiArticle("https://en.wikipedia.org/w/api.php?format=xml&action=query&prop=extracts&titles=George%20Washington&redirects=true");
据我理解PlayWS的文档https://www.playframework.com/documentation/2.2.x/ScalaWS,上面的代码应该返回一个包含我正在访问的页面文本的对象。
但是,我正在取回以下对象,它不包含文本:
NingWSRequest(NingWSClient(com.ning.http.client.AsyncHttpClientConfig@40ac2d48),https://en.wikipedia.org/w/api.php?format=xml&action=query&prop=extracts&titles=George%20Washington&redirects=true,GET,EmptyBody,Map(),Map(),None,None,None,None,None,None,None)
我还尝试使用Future,遵循Scala WS文档: 包装模型
import com.ning.http.client.Response
import play.api.libs.ws.{WSRequest, WS}
import play.api.Play.current
import scala.concurrent.Future
class WikiArticle(url : String) {
var text : WSRequest = null
def queryApi(): Unit = {
text = WS.url(url)
val futureResponse : Future[Response] = text.get();
println(futureResponse);
}
}
但是,这会引发以下错误:
Error:(17, 53) type mismatch;
found : scala.concurrent.Future[play.api.libs.ws.WSResponse]
required: scala.concurrent.Future[com.ning.http.client.Response]
val futureResponse : Future[Response] = text.get();
如何使用Play2查询API?