How to implement YQL in Java? [closed]

笑着哭i 提交于 2019-12-06 03:15:01

YQL is interpreted server-side, so there's not much to do in Java. I'd just make a URL, open it, and read the data stream. Just copy the PHP example code, mostly:

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();

Depending on what you need, you might want to write some code around the URL construction to make it less messy-looking, and you might like a fancier JSON parser like Gson instead of org.json as I've used here.

You might also get some milage out of a more robust HTTP client library that would allow multiple queries on one connection, etc.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!