Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package minor.stockQuote; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; import java.util.logging.Level; import java.util.logging.Logger; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; import java.util.ArrayList; import java.util.List; /** * * @author chanakya */ public class StockData { public List<StockDetails> getStockData(String ticker) { /* if(ticker.length() > 5 || ticker.length() < 3){ //System.out.println("-----------------------------------------------------------------------------------------------------"+ticker); return null; } */ //else { String requestQuery = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22" + ticker + "%22)%0A%09%09" + "&format=json&diagnostics=true&env=http%3A%2F%2Fdatatables.org%2Falltables.env&callback="; //String requestQuery ="http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20%3D%20"+ticker+"%0A%09%09&format=json&diagnostics=true&env=http%3A%2F%2Fdatatables.org%2Falltables.env&callback="; //System.out.println(requestQuery); ArrayList<String> stockdetails = new ArrayList<String>(); URL url; try { url = new URL(requestQuery); URLConnection connection = url.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); //System.out.println("jjjj"+in.readLine()); JSONParser jsonParser = new JSONParser(); try { JSONObject jsonObject = (JSONObject) jsonParser.parse(in); //System.out.println(jsonObject); JSONObject query = (JSONObject) jsonObject.get("query"); JSONObject results = (JSONObject) query.get("results"); JSONObject quote = (JSONObject) results.get("quote"); //System.out.println("iiiii"+quote.toJSONString()); stockdetails.add(quote.get("Change").toString()); stockdetails.add(quote.get("LastTradePriceOnly").toString()); stockdetails.add(quote.get("PERatio").toString()); stockdetails.add(quote.get("DaysLow").toString()); stockdetails.add(quote.get("DaysHigh").toString()); stockdetails.add(quote.get("Name").toString()); stockdetails.add(quote.get("Open").toString()); stockdetails.add(quote.get("Volume").toString()); stockdetails.add(quote.get("Ask").toString()); stockdetails.add(quote.get("AverageDailyVolume").toString()); stockdetails.add(quote.get("LastTradeDate").toString()); stockdetails.add(quote.get("EBITDA").toString()); stockdetails.add(quote.get("DaysRange").toString()); stockdetails.add(quote.get("FiftydayMovingAverage").toString()); stockdetails.add(quote.get("PreviousClose").toString()); stockdetails.add(quote.get("ChangeinPercent").toString()); stockdetails.add(quote.get("PEGRatio").toString()); stockdetails.add(quote.get("Symbol").toString()); stockdetails.add(quote.get("YearRange").toString()); stockdetails.add(quote.get("DaysValueChange").toString()); stockdetails.add(quote.get("StockExchange").toString()); stockdetails.add(quote.get("PercentChange").toString()); } catch (ParseException ex) { Logger.getLogger(StockData.class.getName()).log(Level.SEVERE, null, ex); } catch (NullPointerException ne) { } } catch (IOException e) { System.out.println(e); } for (String s : stockdetails) { System.out.println(s); } StockDetails obj = new StockDetails(stockdetails); //System.out.println("dddddD"+obj.toString()); List<StockDetails> sdList = new ArrayList<StockDetails>(); sdList.add(obj); /* for(StockDetails s: sdList){ System.out.println("ddfdfdf"+s.getChange() + s.getDaysHigh()); } */ return sdList; //} } }