携帯電話用Java の雰囲気と IM
001:import java.io.*;
002:import javax.microedition.io.*;
003:import javax.microedition.midlet.*;
004:import javax.microedition.lcdui.*;
005:
006:/**
007: * サンプルプログラムの画面を提供します。
008: *
009: * @author Itaru Ogawa
010: */
011:public class DBTestForm extends Form implements CommandListener {
012: /** 名前入力用のテキスト */
013: private TextField nameField;
014:
015: /** 状態入力用のテキストBOX */
016: private TextField stateField;
017:
018: /** 「状態」のラベルとテキスト */
019: private StringItem state;
020:
021: /** 「設定」コマンド */
022: private Command setCommand;
023:
024: /** 「取得」コマンド */
025: private Command getCommand;
026:
027: /** サーブレットのURL */
028: private static final String URL = "http://localhost:8080/servlet/DBTestServlet";
029:
030: /**
031: * コンストラクタ。画面の準備をします。
032: */
033: public DBTestForm() {
034: super("DBTest");
035: /* コマンドの作成とリスナーの登録 */
036: setCommand = new Command("設定", Command.SCREEN, 1);
037: getCommand = new Command("取得", Command.SCREEN, 1);
038:
039: addCommand(setCommand);
040: addCommand(getCommand);
041:
042: setCommandListener(this);
043:
044: /* コンポーネントの初期化 */
045: nameField = new TextField("名前", "", 6, TextField.ANY);
046: stateField = new TextField("状態", "", 6, TextField.ANY);
047:
048: state = new StringItem("状態リスト\n", "");
049:
050: append(nameField);
051: append(stateField);
052: append(state);
053: }
054:
055: /**
056: * URLエンコードします。エンコード時にShift-JISを用います。
057: *
058: * @param in エンコードして欲しい文字列
059: * @return エンコードされた文字列
060: */
061: public static String encode(String in) {
062: StringBuffer inBuf = new StringBuffer(in);;
063: StringBuffer outBuf = new StringBuffer();
064: for (int i=0;i<inBuf.length(); i++) {
065: char temp = inBuf.charAt(i);
066: if (('a'<=temp && temp <='z')
067: || ('A'<=temp && temp <='Z')
068: || ('0'<=temp && temp <='9')
069: || temp == '.' || temp == '-' || temp == '*' || temp == '_') {
070: outBuf.append(temp);
071: } else if (temp == ' ') {
072: outBuf.append('+');
073: } else {
074: byte[] bytes;
075: try {
076: bytes = new String(new char[]{temp}).getBytes("SJIS");
077: for(int j=0;j<bytes.length;j++) {
078: int high = (bytes[j]>>>4)&0x0F;
079: int low = (bytes[j]&0x0F);
080: outBuf.append('%');
081: outBuf.append(Integer.toString(high, 16).toUpperCase());
082: outBuf.append(Integer.toString(low , 16).toUpperCase());
083: }
084: } catch (Exception e) {
085: }
086: }
087: }
088:
089: return outBuf.toString();
090: }
091:
092: /**
093: * コマンドハンドラ。各コマンドが発行された時の処理を行います。
094: * @param c コマンド
095: * @param d ディスプレイ
096: */
097: public void commandAction(Command c, Displayable d) {
098: HttpConnection http = null;
099: InputStream input = null;
100:
101: if (c==setCommand) {
102: /* 「設定」が押された時の処理 */
103: String name = nameField.getString();
104: String state = stateField.getString();
105: try {
106: /* httpで情報を設定します */
107: http = (HttpConnection)Connector.open(
108: URL + "/set" + "?name=" + encode(name) + "&state=" + encode(state)
109: );
110: http.setRequestMethod(HttpConnection.GET);
111: input = http.openInputStream();
112: } catch (IOException ioe) {
113: ioe.printStackTrace();
114: }
115:
116: try {
117: if (input != null) {
118: input.close();
119: }
120: if (http != null) {
121: http.close();
122: }
123: } catch (IOException ioe) {
124: ioe.printStackTrace();
125: }
126:
127: } else if (c==getCommand) {
128: /* 「取得」が押された時の処理 */
129: String list = "";
130: try {
131: /* httpで情報を取得します */
132: http = (HttpConnection)Connector.open(URL + "/get");
133: http.setRequestMethod(HttpConnection.GET);
134: input = http.openInputStream();
135: byte[] data = new byte[(int)http.getLength()];
136:
137: input.read(data, 0, data.length);
138: list = new String(data, "SJIS");
139: } catch (IOException ioe) {
140: ioe.printStackTrace();
141: }
142:
143: try {
144: if (input != null) {
145: input.close();
146: }
147: if (http != null) {
148: http.close();
149: }
150: } catch (IOException ioe) {
151: ioe.printStackTrace();
152: }
153:
154: /* 表示情報更新 */
155: state.setText(list);
156: }
157: }
158:}