2013-07-05 97 views

回答

5

YQL是服务器端解释的,所以Java中没有太多的事情要做。我只需创建一个URL,打开它并阅读数据流。只需复制the PHP example code,主要是:

String baseUrl = "http://query.yahooapis.com/v1/public/yql?q="; 
String query = "select * from upcoming.events where location='San Francisco' and search_text='dance'"; 
String fullUrlStr = baseUrl + URLEncoder.encode(query, "UTF-8") + "&format=json"; 

URL fullUrl = new URL(fullUrlStr); 
InputStream is = fullUrl.openStream(); 

JSONTokener tok = new JSONTokener(is); 
JSONObject result = new JSONObject(tok); 

is.close(); 

取决于你需要什么,你可能会想要写周围的URL建设一些代码,使它不那么凌乱看,你可能会喜欢一个票友JSON解析器像Gson代替我已经在这里使用了org.json

您可能还会从一个更强大的HTTP客户端库中获得一些功能,这将允许在一个连接上进行多个查询等。