0
我是新來解析的,我試圖通過YQL語句檢索庫存數據來進行測試。每次運行程序時,我都會遇到一個錯誤的連接,並且我已經搜索過但尚未找到任何解決方案。我從我的字符串構建器獲得的鏈接是:與YQL查詢的連接不好
https://query.yahooapis.com/v1/public/yql?q=select%20 *%20from%20yahoo.finance.quote%20where%20symbol%20in%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 {
}
}
}
感謝您的幫助,但是我只是試了一下這個鏈接,我得到了一個新的打印輸出值(responseCode),505,而不是400. – Frodgers 2014-11-03 20:30:55
謝謝你的幫助,事實證明我只是不小心忘記了「 (「在URL的末尾,並在?q =之後添加了那些空格。我能夠找到505錯誤的解決方案。 – Frodgers 2014-11-03 20:44:14
不客氣,很高興我能提供幫助。 – Matt 2014-11-04 09:34:20