1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| public void startApp() {
try {
// establish a socket connection with remote server
//streamConnection =(StreamConnection) Connector.open(connectString);
//etablir une connexion HTTP en J2ME
c = (HttpConnection)Connector.open("http://www.java-samples.com",Connector.READ_WRITE, true);
//c.setRequestMethod(HttpConnection.GET); //default
c.setRequestProperty("Connection","close");
// create DataOuputStream on top of the socket connection
outputStream = c.openOutputStream();
dataOutputStream = new DataOutputStream(outputStream);
// send the HTTP request
//dataOutputStream.writeChars("GET /index.htm HTTP/1.0 \n");
dataOutputStream.writeChars(stringToReverse);
dataOutputStream.flush();
// create DataInputStream on top of the socket connection
inputStream = c.openInputStream();
dataInputStream = new DataInputStream(inputStream);
// retrieve the contents of the requested page from Web server
int inputChar;
while ( (inputChar = dataInputStream.read()) != -1) {
results.append((char) inputChar);
}
// display the page contents on the phone screen
resultField = new StringItem(null, results.toString());
resultScreen.append(resultField);
myDisplay.setCurrent(resultScreen);
} catch (IOException e) {
System.err.println("Exception caught:" + e);
} finally {
// free up I/O streams and close the socket connection
try {
if (dataInputStream != null)
dataInputStream.close();
} catch (Exception ignored) {}
try {
if (dataOutputStream != null)
dataOutputStream.close();
} catch (Exception ignored) {}
try {
if (outputStream != null)
outputStream.close();
} catch (Exception ignored) {}
try {
if (inputStream != null)
inputStream.close();
} catch (Exception ignored) {}
try {
if (c != null)
c.close();
} catch (Exception ignored) {}
}
} |
Partager