Java Reference
In-Depth Information
public ClientConnection(NetworkDemoMIDlet midlet)
{
this.midlet = midlet;
}
public void sendMessage(String url)
{
this.url = url;
start();
}
public void run()
{
try
{
HttpConnection conn = (HttpConnection)Connector.open(url);
int responseCode = conn.getResponseCode();
midlet.append("Response code","" + responseCode);
int i = 0;
String headerKey = null;
while((headerKey = conn.getHeaderFieldKey(i++)) != null)
{
midlet.append(headerKey,conn.getHeaderField(headerKey));
}
InputStream in = conn.openInputStream();
StringBuffer buffer = new StringBuffer(TEXT_FIELD_SIZE);
int ch;
int read = 0;
while ( (ch = in.read()) != -1 && read < TEXT_FIELD_SIZE)
{
buffer.append((char)ch);
read++;
}
midlet.append("HTML Response" ,buffer.toString());
conn.close();
conn = null;
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
The
url
parameter of the
sendMessage()
method has the following
form:
http://www.symbian.com:80
The
sendMessage()
method creates a request and then starts a
new
Thread
to create the connection, send the request and read the
response. Let us look at the contents of the thread's
run()
method in
more detail:
HttpConnection conn = (HttpConnection)Connector.open(url);
int responseCode = conn.getResponseCode();
midlet.append("Response code","" + responseCode);
int i = 0;