Search code examples
javaparsingyqlhttpconnection

Bad Connection to YQL Query


I am new to parsing and am trying to test it out by retrieving stock data through a YQL statement. I am getting a bad connection each time I run the program and I have searched around but haven't found any solution. The link I am getting from my stringbuilder is:

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

The outputs for my print statements

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

are 400, 200 respectively. I am not getting any actual errors, but my program just terminates at the if statement comparing the two values.

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 {
      } 
    }
}

Solution

  • Try replacing the following line (note the removal of the blanks after ? and the opening bracket at the end ():

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

    After pasting the correct link into my browser I get a response and the same should apply for your snippet.