Methods Summary |
---|
public void | destroyApp(boolean unconditional)Destroy must cleanup everything. The thread is signaled
to stop and no result is produced.
|
void | getViaContentConnection(java.lang.String url)Simple read of a url using ContentConnection.
No HTTP specific behavior is needed or used.
Connector.open is used to open url and a ContentConnection is returned.
The ContentConnection may be able to provide the length.
If the length is available, it is used to read the data in bulk.
From the StreamConnection the InputStream is opened.
It is used to read every character until end of file (-1).
If an exception is thrown the connection and stream is closed.
ContentConnection c = null;
InputStream is = null;
try {
c = (ContentConnection)Connector.open(url);
is = c.openInputStream();
buffer.setLength(0);
int len = (int)c.getLength();
if (len > 0) {
byte[] data = new byte[len];
int actual = is.read(data);
process(data);
} else {
int ch;
while ((ch = is.read()) != -1) {
process((byte)ch);
}
}
} finally {
if (is != null)
is.close();
if (c != null)
c.close();
}
|
void | getViaHttpConnection(java.lang.String url)Read the HTTP headers and the data using HttpConnection.
Check the response code to insure successful retrieval.
Connector.open is used to open url and a HttpConnection is returned.
The HTTP headers are read and processed.
If the length is available, it is used to read the data in bulk.
From the HttpConnection the InputStream is opened.
It is used to read every character until end of file (-1).
If an exception is thrown the connection and stream is closed.
HttpConnection c = null;
InputStream is = null;
OutputStream os = null;
try {
int status = -1;
// Open the connection and check for re-directs
while (true) {
c = (HttpConnection)Connector.open(url);
setRequestHeaders(c);
// Get the status code, causing the connection to be made
status = c.getResponseCode();
if (status == HttpConnection.HTTP_TEMP_REDIRECT ||
status == HttpConnection.HTTP_MOVED_TEMP ||
status == HttpConnection.HTTP_MOVED_PERM) {
// Get the new location and close the connection
url = c.getHeaderField("location");
c.close();
System.out.println("Redirecting to " + url);
} else {
break;
}
}
// Any 500 status number (500, 501) means there was a server error
if ((status == HttpConnection.HTTP_NOT_IMPLEMENTED) ||
(status == HttpConnection.HTTP_VERSION) ||
(status == HttpConnection.HTTP_INTERNAL_ERROR) ||
(status == HttpConnection.HTTP_GATEWAY_TIMEOUT) ||
(status == HttpConnection.HTTP_BAD_GATEWAY)) {
System.err.print("WARNING: Server error status ["+status+"] ");
System.err.println("returned for url ["+url+"]");
if (is != null)
is.close();
if (os != null)
os.close();
if (c != null)
c.close();
return;
}
// Only HTTP_OK (200) means the content is returned.
if (status != HttpConnection.HTTP_OK) {
throw new IOException("Response status not OK ["+status+"]");
}
// Get the ContentType
String type = c.getType();
processType(type);
// open the InputStream
is = c.openInputStream();
buffer.setLength(0);
// Get the length and process the data
int len = (int)c.getLength();
if (len > 0) {
byte[] data = new byte[len];
int actual = is.read(data);
process(data);
} else {
int ch;
while ((ch = is.read()) != -1) {
process((byte)ch);
}
}
} finally {
if (is != null)
is.close();
if (c != null)
c.close();
}
|
void | getViaStreamConnection(java.lang.String url)Simple read of a url using StreamConnection.
No HTTP specific behavior is needed or used.
Connector.open is used to open url and a StreamConnection is returned.
From the StreamConnection the InputStream is opened.
It is used to read every character until end of file (-1).
If an exception is thrown the connection and stream is closed.
StreamConnection c = null;
InputStream s = null;
try {
c = (StreamConnection)Connector.open(url);
s = c.openInputStream();
buffer.setLength(0);
int ch;
while ((ch = s.read()) != -1) {
process((byte)ch);
}
} finally {
if (s != null)
s.close();
if (c != null)
c.close();
}
|
public void | pauseApp()Pause, discontinue with the http tests
|
void | postViaHttpConnection(java.lang.String url)Post a request with some headers and content to the server and
process the headers and content.
Connector.open is used to open url and a HttpConnection is returned.
The request method is set to POST and request headers set.
A simple command is written and flushed.
The HTTP headers are read and processed.
If the length is available, it is used to read the data in bulk.
From the StreamConnection the InputStream is opened.
It is used to read every character until end of file (-1).
If an exception is thrown the connection and stream is closed.
int status = 0;
HttpConnection c = null;
InputStream is = null;
OutputStream os = null;
try {
c = (HttpConnection)Connector.open(url);
// Set the request method and headers
c.setRequestMethod(HttpConnection.POST);
c.setRequestProperty("If-Modified-Since",
"29 Oct 1999 19:43:31 GMT");
c.setRequestProperty("User-Agent",
"Profile/MIDP-1.0 Configuration/CLDC-1.0");
c.setRequestProperty("Content-Language", "en-US");
// Getting the output stream may flush the headers
os = c.openOutputStream();
os.write("LIST games\n".getBytes());
os.flush(); // Optional, openInputStream will flush
// Get the status code, causing the connection to be made
status = c.getResponseCode();
// Any 500 status number (500, 501) means there was a server error
if ((status == HttpConnection.HTTP_NOT_IMPLEMENTED) ||
(status == HttpConnection.HTTP_VERSION) ||
(status == HttpConnection.HTTP_INTERNAL_ERROR) ||
(status == HttpConnection.HTTP_GATEWAY_TIMEOUT) ||
(status == HttpConnection.HTTP_BAD_GATEWAY)) {
System.err.print("WARNING: Server error status ["+status+"] ");
System.err.println("returned for url ["+url+"]");
if (is != null)
is.close();
if (os != null)
os.close();
if (c != null)
c.close();
return;
}
// Only HTTP_OK (200) means the content is returned.
if (status != HttpConnection.HTTP_OK) {
throw new IOException("Response status not OK ["+status+"]");
}
// Open the InputStream and get the ContentType
is = c.openInputStream();
String type = c.getType();
processType(type);
// Get the length and process the data
int len = (int)c.getLength();
if (len > 0) {
byte[] data = new byte[len];
int actual = is.read(data);
process(data);
} else {
int ch;
while ((ch = is.read()) != -1) {
process((byte)ch);
}
}
} finally {
if (is != null)
is.close();
if (os != null)
os.close();
if (c != null)
c.close();
}
|
void | process(byte b)Process the data one character at a time.
buffer.append((char)b);
|
void | process(byte[] b)Process the data from the array.
for (int i = 0; i < b.length; i++) {
process(b[i]);
}
|
void | processType(java.lang.String type)Process the type.
|
public void | run()Run the examples.;
try {
gauge.setLabel("Get using ContentConnection");
gauge.setValue(2);
getViaContentConnection(url);
gauge.setLabel("Get using StreamConnection");
gauge.setValue(4);
getViaStreamConnection(url);
gauge.setLabel("Get using HttpConnection");
gauge.setValue(6);
getViaHttpConnection(url);
gauge.setLabel("Post using HttpConnection");
gauge.setValue(8);
postViaHttpConnection(url);
} catch (Exception e) {
System.out.println(e);
e.printStackTrace();
}
gauge.setValue(10);
notifyDestroyed();
|
void | setRequestHeaders(javax.microedition.io.HttpConnection c)Add request properties for the configuration, profiles,
and locale of this system.
String conf = System.getProperty("microedition.configuration");
String prof = System.getProperty("microedition.profiles");
int space = prof.indexOf(' ");
if (space != -1) {
prof = prof.substring(0, space -1);
}
String locale = System.getProperty("microedition.locale");
String ua = "Profile/" + prof +
" Configuration/" + conf;
c.setRequestProperty("User-Agent", ua);
if (locale != null) {
c.setRequestProperty("Content-Language", locale);
}
|
public void | startApp()Start a thread to run the examples.
new Thread(this).start();
|