与YQL查询的连接错误

时间:2014-11-03 15:16:28

标签: java parsing yql httpconnection

我是解析的新手,我试图通过YQL语句检索库存数据来测试它。每次运行程序时我都会收到错误的连接,而且我已经搜索过但没有找到任何解决方案。我从我的stringbuilder获得的链接是:

https://query.yahooapis.com/v1/public/yql?q=select%20 *%20from%20yahoo.finance.quote%20where%20symbol%20英寸%20 “MSFT” %2C “YHOO” %2C “AAPL” %2C “歌”)及诊断=真安培; ENV =商店%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 {
      } 
    }
}

1 个答案:

答案 0 :(得分:0)

尝试替换以下行(请注意?之后删除空格以及结束(处的左括号):

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

粘贴correct link into my browser后,我收到回复,同样适用于您的代码段。