【问题标题】:Bad Connection to YQL Query与 YQL 查询的错误连接
【发布时间】:2014-11-03 15:16:28
【问题描述】:

我是解析新手,我正在尝试通过 YQL 语句检索股票数据来测试它。每次运行程序时,我的连接都不好,我四处搜索,但没有找到任何解决方案。我从我的 stringbuilder 获得的链接是:

https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quote%20where%20symbol%20in%20"MSFT"%2C"YHOO"%2C"AAPL"%2C"GOOG")&diagnostics=true&env=store%3A% 2F%2Fdatatables.org%2Falltableswithkeys

我的打印语句的输出

System.out.println(responseCode);
System.out.println(HttpURLConnection.HTTP_OK);

分别为 400、200。我没有收到任何实际错误,但我的程序只是在比较两个值的 if 语句处终止。

import java.*;
import java.awt.List;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import javax.lang.model.element.Element;
import javax.swing.text.Document;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.soap.Node;

import org.omg.CORBA.portable.InputStream;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class Testing
{
    public static void main(String[] args)
    {
        ArrayList<String> stockTicker = new ArrayList<>();
        stockTicker.add("MSFT");
        stockTicker.add("YHOO");
        stockTicker.add("AAPL");
        stockTicker.add("GOOG");
        String YahooURL = "https://query.yahooapis.com/v1/public/yql?    q=select%20*%20from%20yahoo.finance.quote%20where%20symbol%20in%20";
        for(int x=0; x < stockTicker.size()-1; x++)
            {
                YahooURL += "\""+stockTicker.get(x)+"\"%2C";
            }
    YahooURL += "\""+stockTicker.get(stockTicker.size()-1)+"\"";
    YahooURL += ")&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
    System.out.println(YahooURL);

    try {
       URL url = new URL(YahooURL); //Begin Connecting
       URLConnection connection;
       connection = url.openConnection();
       HttpURLConnection httpConnection = (HttpURLConnection)connection;
       int responseCode = httpConnection.getResponseCode();
        // Tests if responseCode == 200 Good Connection
       System.out.println(responseCode);
       System.out.println(HttpURLConnection.HTTP_OK);
       if (responseCode == HttpURLConnection.HTTP_OK) {
          System.out.println("gets past response code");
          java.io.InputStream in = httpConnection.getInputStream();
          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
          DocumentBuilder db = dbf.newDocumentBuilder();
          org.w3c.dom.Document dom = db.parse(in);
          org.w3c.dom.Element docEle = dom.getDocumentElement(); //End of Connecting
          //begin parsing
          NodeList quoteList = docEle.getElementsByTagName("results");
          for (int i = 0 ; i < quoteList.getLength(); i++)
          {
            org.w3c.dom.Node q = quoteList.item(i);
            if (quoteList != null && quoteList.getLength() > 0)
            {
                Element tickerID = (Element) q;
                String tID = (String) ((DocumentBuilderFactory) tickerID).getAttribute("quote symbol");
                NodeList infoList = ((org.w3c.dom.Node) tickerID).getChildNodes();
                for(int j=0; j < infoList.getLength();j++)
                {
                    Node qi = (Node) infoList.item(j);
                    if(qi.getNodeType()==Node.ELEMENT_NODE)
                    {
                        Element info = (Element) qi;
                        System.out.println("TickerID: " + q + ((org.w3c.dom.Element) qi).getTagName() + " = " + qi.getTextContent());
                    }
                }
            }
          }
        }
      } catch (MalformedURLException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      } catch (ParserConfigurationException e) {
        e.printStackTrace();
      } catch (SAXException e) {
        e.printStackTrace();
      }
      finally {
      } 
    }
}

【问题讨论】:

    标签: java parsing yql httpconnection


    【解决方案1】:

    尝试替换以下行(注意删除? 后的空白和末尾的左括号():

    String YahooURL = "https://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.quote where symbol in (";
    

    粘贴correct link into my browser 后,我收到了回复,同样适用于您的 sn-p。

    【讨论】:

    • 感谢您的帮助,但我刚刚尝试了该链接,我得到了 print(responseCode) 的新输出值,505,而不是 400。
    • 感谢您的帮助,原来我只是不小心忘记了 URL 末尾的“(”并在 ?q= 之后添加了这些空格。我能够找到解决方案505 错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 2020-10-23
    相关资源
    最近更新 更多