| import com.sun.kjava.*; public final class FundSpotlet extends RequestFormSpotlet { public static void main (String args[]) { new FundSpotlet().draw(); } private void draw() { initForm(); setTitle("Fund Quote Requested"); } public void penDown(int x, int y){ if (getExitButton().pressed(x,y)){ getGraphic().playSound(Graphics.SOUND_CONFIRMATION); System.exit(0); } if (getSymField().pressed(x,y)) getSymField().setFocus(); if (getGetButton().pressed(x,y)) { quoteRequested(); } } private void storeQuote (String fund, String newQuote) { int dbType = 0x46554e44; int dbCreator = 0x43415454; com.sun.kjava.Database quoteDB; try { quoteDB = new com.sun.kjava.Database(dbType, dbCreator, com.sun.kjava.Database.READWRITE); if (!quoteDB .isOpen()) { com.sun.kjava.Database.create(0, "MutualFundQuotes", dbCreator, dbType, false); quoteDB = new com.sun.kjava.Database(dbType, dbCreator, com.sun.kjava.Database.READWRITE); } byte[] data = (fund + "#" + newQuote).getBytes(); quoteDB.addRecord(data); quoteDB.close(); } catch (Exception recordException) { System.out.println("Unable to store quote and/or use Mutual Fund Quote database."); } } private void getAndDisplayQuote() { String fundSymbol = getSymField().getText(); if (fundSymbol.length() > 0) { String theQuote = QuoteService.getQuote(fundSymbol); if (theQuote != null) { storeQuote(fundSymbol, theQuote); message(theQuote); } else message("No quote. Check Symbol"); } } private void quoteRequested() { message(""); getGraphic().playSound(Graphics.SOUND_STARTUP); if ((getSymField().getText().length() > 0)) { getAndDisplayQuote(); } else { message("Symbol required!"); } } } |
| private static String getQuotePage(String symbolString) { StringBuffer quotePage = new StringBuffer(); int ch; try { InputStream in = Connector.openInputStream ( "testhttp://someurl/some_application?page=++&mode=fund&symbol="+ symbolString); while ((ch = in.read()) > 0) { quotePage.append((char)ch); } in.close(); return quotePage.toString(); } catch (IOException ex) { System.out.println("Exception reading quote from HTTP Connection"); return null; } } |