【问题标题】:Generating URL for YQL为 YQL 生成 URL
【发布时间】:2014-05-20 17:55:55
【问题描述】:

我正在开发一个 java 项目,在该项目中我需要生成 url 来获取数据,即来自 yahoo 的开盘价、低价盘、高盘价、收盘价等

类似的帖子here 建议使用来自 java.net.URLEncoder 的 URLEncoder.encode(query, "UTF-8") 但这会生成不同格式的 url。

示例: 对于这个查询

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

它给出了网址:

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

而我需要这种格式的网址

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=

请建议我如何将此查询转换为上述网址。

谢谢,

【问题讨论】:

    标签: java android json yql


    【解决方案1】:

    这个问题在这里Java equivalent to JavaScript's encodeURIComponent that produces identical output?回答。

    该解决方案为EncodingUtil 类提供源代码。您可以像这样使用该类(对于您的具体示例):

    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=");
    

    与您的问题无关,请小心使用select *。如果 Yahoo 添加或删除字段,则处理返回的 JSON 对象的代码可能会中断。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-26
      相关资源
      最近更新 更多