1

I am working on a java project in which I need to generate url to get data i.e open,low,high,close etc prices from yahoo

Similar post here suggests to use URLEncoder.encode(query, "UTF-8") from java.net.URLEncoder but this generates url in a differnt format.

Example: For this query

select * from yahoo.finance.historicaldata where symbol in ("YHOO","AAPL") and startDate = "2014-01-01" and endDate = "2014-02-01"

It gives url:

http://query.yahooapis.com/v1/public/yql?q=select+*+from+yahoo.finance.historicaldata+where+symbol+in+%28%22YHOO%22%2C%22AAPL%22%29+and+startDate+%3D+%222014-01-01%22+and+endDate+%3D+%222014-02-01%22&format=json

whereas I need url in this format

https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.historicaldata%20where%20symbol%20in%20(%22YHOO%22%2C%22AAPL%22)%20and%20startDate%20%3D%20%222014-01-01%22%20and%20endDate%20%3D%20%222014-02-01%22&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=

Please suggest how I convert this query to above mentioned url.

Thanks,

Community
  • 1
  • 1

1 Answers1

0

This question is answered here Java equivalent to JavaScript's encodeURIComponent that produces identical output?.

The solution offers the source code to the EncodingUtil class. You can use that class like this (for your specific example):

String query = "select * from yahoo.finance.historicaldata where symbol in (\"YHOO\",\"AAPL\") and startDate = \"2014-01-01\" and endDate = \"2014-02-01\"";

System.out.println("https://query.yahooapis.com/v1/public/yql?q="+EncodingUtil.encodeURIComponent(query)+"&format=json&diagnostics=true&env="+EncodingUtil.encodeURIComponent("store://datatables.org/alltableswithkeys")+"&callback=");

And unrelated to your quesiton, be careful using select *. If Yahoo adds or removes a field, your code that handles the returned JSON object may break.

Community
  • 1
  • 1
mbmast
  • 960
  • 11
  • 25