📄 session.java
字号:
/** * Load movie data. * * @param ml the movie list to populate with the data. */ public void loadMovies(MovieList ml) { HttpConnection conn = null; DataInputStream in = null; try { smartTicket.updateGauge(GaugeForm.GAUGE_MAX/3); String message = MessageConstants.DISPLAY_MOVIES + "^"; conn = open(smartTicket.servlet, message); in = conn.openDataInputStream(); smartTicket.updateGauge(2*GaugeForm.GAUGE_MAX/3); int count = in.readInt(); ml.init(count); for (int i = 0; i != count; i++) { ml.addMovie(in.readInt(), in.readUTF(), in.readUTF(), in.readUTF()); } smartTicket.updateGauge(GaugeForm.GAUGE_MAX); smartTicket.stopLoading(); } catch (Exception e) { smartTicket.stopLoading(e); } finally { close(conn, in); } } /** * Load a poster. * * @param ic the ImageCanvas on which to display the poster. * @param posterURL the URL of the poster. */ public void loadPoster(ImageCanvas ic, String posterURL) { HttpConnection conn = null; DataInputStream is = null; try { smartTicket.updateGauge(GaugeForm.GAUGE_MAX/3); conn = open(posterURL); is = conn.openDataInputStream(); smartTicket.updateGauge(2*GaugeForm.GAUGE_MAX/3); int size = (int)(conn.getLength()); if (size < 0) { size = 12000; // Make a guess } byte[] buf = new byte[size]; int offset = 0; int remaining = 0; while ((remaining = size - offset) > 0) { int len = is.read(buf, offset, remaining); if (len < 0) break; offset += len; } if (remaining < 0) { throw new IOException("image not read completely"); } ic.setImage(Image.createImage(buf, 0, offset)); smartTicket.updateGauge(GaugeForm.GAUGE_MAX); smartTicket.stopLoading(); } catch (Exception e) { smartTicket.stopLoading(e); } finally { close(conn, is); } } /** * Load the locations for the given movie. * * @param ll the location list to populate. * @param movieID the ID of the movie. */ public void loadLocations(LocationList ll, int movieID) { HttpConnection conn = null; DataInputStream in = null; try { smartTicket.updateGauge(GaugeForm.GAUGE_MAX/3); String message = MessageConstants.DISPLAY_LOCATIONS + "^" + movieID; conn = open(smartTicket.servlet, message); in = conn.openDataInputStream(); smartTicket.updateGauge(2*GaugeForm.GAUGE_MAX/3); int count = in.readInt(); ll.init(count); for (int i = 0; i != count; i++) { ll.addLocation(in.readInt(), in.readUTF()); } smartTicket.updateGauge(GaugeForm.GAUGE_MAX); smartTicket.stopLoading(); } catch (Exception e) { smartTicket.stopLoading(e); } finally { close(conn, in); } } /** * Load showtimes for the given movie at the given location. * * @param sl the list to populate with showtime data. * @param movieID the ID of the movie. * @param locationID the ID of the location. */ public void loadShowtimes(ShowtimeList sl, int movieID, int locationID) { HttpConnection conn = null; DataInputStream in = null; try { smartTicket.updateGauge(GaugeForm.GAUGE_MAX/3); String message = MessageConstants.DISPLAY_SHOWTIMES + "^" + movieID + "," + locationID; conn = open(smartTicket.servlet, message); in = conn.openDataInputStream(); smartTicket.updateGauge(2*GaugeForm.GAUGE_MAX/3); int count = in.readInt(); sl.init(count); for (int i = 0; i != count; i++) { sl.addShowtime(in.readInt(), in.readUTF()); } smartTicket.updateGauge(GaugeForm.GAUGE_MAX); smartTicket.stopLoading(); } catch (Exception e) { smartTicket.stopLoading(e); } finally { close(conn, in); } } /** * Load the given seating canvas with the data of the seating * plan for the given show. * * @param sc the seating canvas which needs the data. * @param showID the ID of the show. * @param movieTitle the title of the movie (for display). * @param showtime the time of the movie (for display). */ public void loadSeatingPlan(SeatingCanvas sc, int showID, String movieTitle, String showtime) { HttpConnection conn = null; DataInputStream in = null; try { smartTicket.updateGauge(GaugeForm.GAUGE_MAX/3); String message = MessageConstants.DISPLAY_SEATINGPLAN + "^" + showID; conn = open(smartTicket.servlet, message); in = conn.openDataInputStream(); smartTicket.updateGauge(2*GaugeForm.GAUGE_MAX/3); int rows = in.read(); rowSize = in.read(); byte[] seats = new byte[rows*rowSize]; in.readFully(seats); sc.init(seats, rowSize, movieTitle, showtime); smartTicket.updateGauge(GaugeForm.GAUGE_MAX); smartTicket.stopLoading(); } catch (Exception e) { smartTicket.stopLoading(e); } finally { close(conn, in); } } /** * Reserve the selected seats. * * @param cf the confirmation form into which the confirmation * data should be loaded. * @param selectedSeats the indices of the selected seats in the * byte array representing the seating plan. * @param movieTitle the title of the movie (for display). * @param showtime the time of the showing (for display). */ public void reserveSeats(ConfirmForm cf, int[] selectedSeats, String movieTitle, String showtime) { HttpConnection conn = null; DataInputStream in = null; try { smartTicket.updateGauge(GaugeForm.GAUGE_MAX/2); StringBuffer messageBuf = new StringBuffer(30); messageBuf.append(MessageConstants.RESERVE_SEATS); messageBuf.append('^'); if (selectedSeats.length > 0) { messageBuf.append(selectedSeats[0] / rowSize); messageBuf.append(','); messageBuf.append(selectedSeats[0] % rowSize); for (int i = 1; i != selectedSeats.length; i++) { messageBuf.append(','); messageBuf.append(selectedSeats[i] / rowSize); messageBuf.append(','); messageBuf.append(selectedSeats[i] % rowSize); } } conn = open(smartTicket.servlet, messageBuf.toString()); in = conn.openDataInputStream(); cf.init(movieTitle, showtime, selectedSeats.length); smartTicket.updateGauge(GaugeForm.GAUGE_MAX); smartTicket.stopLoading(); } catch (Exception e) { smartTicket.stopLoading(e); } finally { close(conn, in); } } /** * Confirm the purchase. * * @param creditCardCheck the last four digits of the credit card * number, as a security check. */ public void confirmSeats(String creditCardCheck) { HttpConnection conn = null; DataInputStream in = null; try { smartTicket.updateGauge(GaugeForm.GAUGE_MAX/2); String message = MessageConstants.CONFIRM_SEATS + "^" + creditCardCheck; conn = open(smartTicket.servlet, message); in = conn.openDataInputStream(); smartTicket.updateGauge(GaugeForm.GAUGE_MAX); smartTicket.stopLoading(); } catch (Exception e) { smartTicket.stopLoading(e); } finally { close(conn, in); } } /** * Cancel the purchase. */ public void cancelSeats() { HttpConnection conn = null; DataInputStream in = null; try { smartTicket.updateGauge(GaugeForm.GAUGE_MAX/2); String message = MessageConstants.CANCEL_SEATS + "^"; conn = open(smartTicket.servlet, message); in = conn.openDataInputStream(); smartTicket.updateGauge(GaugeForm.GAUGE_MAX); smartTicket.stopLoading(); } catch (Exception e) { smartTicket.stopLoading(e); } finally { close(conn, in); } } /* Helper methods. */ /** * Open the record store for storing localized messages. */ public RecordStore openMessageStore(boolean create) throws RecordStoreException { RecordStore rs; try { rs = RecordStore.openRecordStore(MESSAGE_STORE_NAME, create); } catch (RecordStoreNotFoundException rsnfe) { return null; } return rs; } static HttpConnection open(String url) throws IOException { HttpConnection conn = (HttpConnection) Connector.open(url); conn.setRequestProperty("User-Agent", System.getProperty("microedition.profiles")); return conn; } /** * Open a connection, post a command, and check the response. * * The URL is opened and the command is written to the output * stream and the OutputStream is closed. If there is a cookie * in the response, it is saved. * * @param url to open a connection for * @param command to POST to the stream * @returns an open HttpConnection * @exception IOException if any exception occurs * @exception ApplicationException if servlet reported an error */ static HttpConnection open(String url, String message) throws IOException, ApplicationException { HttpConnection conn = open(url); conn.setRequestMethod(HttpConnection.POST); DataOutputStream os = null; message = message + '\n'; try { os = conn.openDataOutputStream(); os.write(message.getBytes()); } finally { if (os != null) { os.close(); } } // If the server reports an error, let's report it. if (conn.getResponseCode() == HttpConnection.HTTP_INTERNAL_ERROR) { String msg = conn.getHeaderField("Reason-Phrase"); if (msg == null) { // This shouldn't happen (?). msg = "Internal Server Error"; } throw new ApplicationException(Integer.parseInt(msg)); } checkResponseCode(conn); return conn; } /** * Close the connection and streams. */ static void close(HttpConnection conn, InputStream is) { if (is != null) { try { is.close(); } catch (IOException ignore) { // ignore } } if (conn != null) { try { conn.close(); } catch (IOException ignore) { // ignore } } } /** * Get the response code out and throw an error if it is not HTTP_OK. */ static void checkResponseCode(HttpConnection conn) throws IOException { int code = conn.getResponseCode(); if (code != HttpConnection.HTTP_OK) { throw new IOException(code + "; " + conn.getResponseMessage()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -