📄 20ca9dd1ec9a001b1a36b107245978e8
字号:
package com.chapter16.bankbillClent;import javax.microedition.io.*;import javax.microedition.lcdui.*;import com.chapter16.shared.BillInfo;import com.chapter16.shared.MessageConstants;import com.chapter16.shared.AccountInfo;import java.io.*;public class HTTPCommunication { private String serviceURL; private boolean offline = true; private ProgressObserverUI progressObserverUI = null; public HTTPCommunication(String serviceURL, ProgressObserverUI progressObserverUI) { this.serviceURL = serviceURL; this.progressObserverUI = progressObserverUI; return; } public boolean isOffline() { return offline; } public void setOffline(boolean offline) { this.offline = offline; return; } public boolean login(String userName, String password) { int passed = 0; HttpConnection connection = null; DataOutputStream outputStream = null; DataInputStream inputStream = null; try { connection = openConnection(); updateProgress(); outputStream = openConnectionOutputStream(connection); outputStream.writeByte(MessageConstants.OPERATION_LOGIN_USER); outputStream.writeUTF(userName); outputStream.writeUTF(password); outputStream.close(); updateProgress(); inputStream = openConnectionInputStream(connection); passed = inputStream.readInt(); System.out.println(passed); updateProgress(); //如果服务器返回1,则表示登陆成功 } catch (IOException ioe) { } finally { //释放资源 closeConnection(connection, outputStream, inputStream); } if (passed == 1) return true; else return false; } public BillInfo[] getBillData() throws IOException { HttpConnection conn = null; DataOutputStream outputStream = null; DataInputStream inputStream = null; try { conn = openConnection(); //更新进度条 updateProgress(); outputStream = openConnectionOutputStream(conn); outputStream.writeByte(MessageConstants.OPERATION_LIST_ACCOUNT); if (conn.getResponseCode() == HttpConnection.HTTP_OK) { inputStream = openConnectionInputStream(conn); //更新进度条 updateProgress(); //获得缴费信息 int length = (int) conn.getLength(); if (length > 0) { //读取传输过来的缴费清单的数目 int num = inputStream.readInt(); BillInfo[] binfo = new BillInfo[num]; for (int i = 0; i < num; i++) { byte[] data = new byte[inputStream.readInt()]; //读取数据 inputStream.read(data); binfo[i] = new BillInfo(); binfo[i].deserialize(data); //显示返回信息 } //更新进度条 updateProgress(); return binfo; } } } catch (Exception e) { System.out.println(e.toString()); } finally { //释放资源 if (progressObserverUI != null) { progressObserverUI = null; } closeConnection(conn, outputStream, inputStream); } return null; } public boolean updateAccount(AccountInfo accountInfo) { int passed = 0; HttpConnection connection = null; DataOutputStream outputStream = null; DataInputStream inputStream = null; try { connection = openConnection(); //更新进度条 updateProgress(); outputStream = openConnectionOutputStream(connection); //发送更新信息的消息 outputStream.writeByte(MessageConstants.OPERATION_UPDATE_ACCOUNT); //发送用户信息 byte[] data = accountInfo.serialize(); //发送数据的长度 outputStream.writeInt(data.length); //发送数据 outputStream.write(data); outputStream.close(); updateProgress(); //从服务器上获得是否成功发送的结果 inputStream = openConnectionInputStream(connection); passed = inputStream.readInt(); //更新进度条 updateProgress(); } catch (IOException ioe) { } finally { closeConnection(connection, outputStream, inputStream); } if (passed == 1) return true; else return false; } private HttpConnection openConnection() throws IOException { try { HttpConnection connection = (HttpConnection) Connector .open(serviceURL); connection.setRequestProperty("User-Agent", "BANKBILL1.0/MIDP-2.0 Configuration/CLDC-1.1"); connection.setRequestProperty("Content-Type", "application/octet-stream"); connection.setRequestMethod(HttpConnection.POST); offline = false; return connection; } catch (IOException ioe) { offline = true; throw ioe; } } private DataOutputStream openConnectionOutputStream( HttpConnection connection) throws IOException { try { return connection.openDataOutputStream(); } catch (IOException ioe) { offline = true; throw ioe; } } private DataInputStream openConnectionInputStream(HttpConnection connection) throws IOException { try { int responseCode = connection.getResponseCode(); if (responseCode == HttpConnection.HTTP_OK) { DataInputStream inputStream = connection.openDataInputStream(); return inputStream; } } catch (IOException ioe) { offline = true; } return null; } void closeConnection(HttpConnection connection, DataOutputStream outputStream, DataInputStream inputStream) { if (outputStream != null) { try { outputStream.close(); } catch (IOException ioe) { } } if (inputStream != null) { try { inputStream.close(); } catch (IOException ioe) { } } if (connection != null) { try { connection.close(); } catch (IOException ioe) { } } return; } protected void updateProgress() { if (progressObserverUI != null) { if (!progressObserverUI.isStopped()) { progressObserverUI.updateProgress(); return; } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -