我需要实现一个调用 Web 服务并返回响应的函数。
我试过
public String getFolderJson(String path) {
String result="initial_value";
StringBuilder param = new StringBuilder();
param.append("?sessionId=").append(getSessionId());
param.append("&path=").append(path);
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, "https://localhost/folder" + param);
try {
builder.sendRequest(null, new RequestCallback() {
@Override
public void onResponseReceived(Request request,
Response response) {
result = response.getText();
System.out.println(response.getText());
//I need to return "result"
}
@Override
public void onError(Request request, Throwable exception) {}
});
return result; //the result get returned before the response is recieved.So i am getting the return value "initial_value".
}
catch (RequestException e) {}
return null;
}
在调用 getFolderJson()
Web 服务时成功调用。但是result
在收到响应之前返回。所以我得到了返回值“initial_value”。
函数时如何从响应中返回值getFolderJson()
?