Java Reference
In-Depth Information
finally {
try {
if (in != null) in.close();
if (hc != null) hc.close();
}
catch (IOException ioe) {}
}
}
}
The
run()
method contains all of the networking code. It's fairly simple; we pass the URL
of an image (retrieved as an application property) to
Connector
's
open()
method and cast the
result to
HttpConnection
. Then we retrieve the length of the image file, using the
getLength()
method. Given the length, we create a byte array and read data into it. Finally, having read the
entire image file into a byte array, we can create an
Image
from the raw data.
If the content length is not specified, the image data is read in chunks, the size of which
can be implementation dependent.
You'll need to specify the MIDlet property
ImageLoader-URL
in order for this example to
work correctly. Note that you need to specify the URL of a PNG image, not of a JPEG or GIF. The
URL
http://65.215.221.148:8080/wj2/res/java2d_sm_ad.png
produces the results shown in
Figure 10-3.
Figure 10-3.
The ImageLoader example
Posting a Form with HTTP POST
Posting a form is a little more complicated on the MIDlet side. In particular, there are request
headers that need to be set in
HttpConnection
before the server is contacted. The process works
like this:
Search WWH ::
Custom Search