Question

Hey what do I have to do to implement the Yahoo Query Language in Java? Or is this generally possible? I want to parse the JSON after that.

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top