Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement YQL in Java? [closed]

Tags:

java

yql

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.

like image 981
maysi Avatar asked Jun 01 '26 15:06

maysi


1 Answers

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.

like image 126
Nathaniel Waisbrot Avatar answered Jun 04 '26 11:06

Nathaniel Waisbrot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!