在Play 2.0应用程序中调用Web服务时遇到问题。这是我的代码看起来如何
Future<Object> promise = WS.url("http://myurl").get().map(testFunc1, null);
Function1 testFunc1 = new Function1(){
public void $init$() {}
public Object apply(Object v1) {
System.out.println("apply");
return "";
}
public Function1 andThen(Function1 g) { return null; }
public Function1 compose(Function1 g) {return null;}
};
但我的意思是给我一个编译时异常说
error: <anonymous MyClass$1> is not abstract and does not override abstract method andThen$mcVJ$sp(Function1) in Function1
Function1 testFunc1 = new Function1(){
我已导入这些软件包
import play.api.libs.ws.WS;
import scala.Function1;
import scala.concurrent.Future;
显然,我似乎在这里遗漏了一些东西。任何人都可以告诉我它是什么。或者我甚至需要使用Function1映射promise对象?
由于 KARTHIK
答案 0 :(得分:2)
您的代码看起来像Java,但您使用的是Scala库。
包play.api
用于Scala API。
使用
import play.libs.WS;
import play.libs.F.Function
而不是
import play.api.libs.ws.WS;
import scala.Function1;
实施例
//checkout https://github.com/schleichardt/stackoverflow-answers/tree/so18491305
package controllers;
import play.libs.F.Function;
import play.libs.F.Promise;
import play.mvc.*;
import play.libs.WS;
public class Application extends Controller {
/**
* This action serves as proxy for the Google start page
*/
public static Result index() {
//Phase 1 get promise of the webservice request
final Promise<WS.Response> responsePromise = WS.url("http://google.de").get();
//phase 2 extract the usable data from the response
final Promise<String> bodyPromise = responsePromise.map(new Function<WS.Response, String>() {
@Override
public String apply(WS.Response response) throws Throwable {
final int statusCode = response.getStatus();
return response.getBody();//assumed you checked the response code for 200
}
});
//phase 3 transform the promise into a result/HTTP answer
return async(
bodyPromise.map(
new Function<String,Result>() {
public Result apply(String s) {
return ok(s).as("text/html");
}
}
)
);
}
}