Send and receive data using a network connection
The following task
shows you how to send and receive data using an HTTP connection to a web site.
The data that is returned from the web site is displayed on the screen. You can
use a similar process to send and receive data using other network protocols.
To make this task as general as possible, the following code sends an HTTP GET
command manually to a server. Normally, you would use an
HttpConnection interface, which constructs the HTTP
command string according to the options that you configure.
CAUTION:
Network input and output operations are not
thread-safe. Make sure that you create a separate thread when you use a
Connection object.
-
Import the required classes and interfaces.
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.UiApplication;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.microedition.io.*;
-
Create a class that extends the
MainScreen class.
public class HTTPOutputScreen extends MainScreen
{
-
Create an instance variable for a
RichTextField object to display the results.
RichTextField _rtfOutput = new RichTextField();
-
Create a constructor that accepts a
Connection object as an argument.
public HTTPOutputScreen(Connection conn)
{
-
Add the
RichTextField to your screen, and start a thread to
access the network connection.
_rtfOutput.setText("Retrieving data. Please wait...");
add(_rtfOutput);
ContentReaderThread t = new ContentReaderThread(conn);
t.start();
-
Create a method to populate your
RichTextField with the data that is returned from
the web site, after
ContentReaderThread completes. You invoke this
method in step 20.
public void showContents(final String result)
{
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
_rtfOutput.setText(result);
}
});
}
-
In an inner class, create a thread to communicate with the web
server. Accept a
ConnectionDescriptor object as an argument.
private final class ContentReaderThread extends Thread
{
private Connection _connection;
ContentReaderThread(Connection conn)
{
_connection = conn;
}
public void run()
{
-
Initialize an
OutputStream object and an
InputStream object to exchange data with the web
site.
OutputStream os = null;
InputStream is = null;
-
Initialize a
String object to store the response from the web
site.
String result = "";
-
Create an
OutputConnection object to send data over your
connection.
try
{
OutputConnection outputConn = (OutputConnection) connection;
-
Retrieve an
OutputStream from your
OutputConnection.
os = outputConn.openOutputStream();
String getCommand = "GET " + "/" + " HTTP/1.0\r\n\r\n";
os.write(getCommand.getBytes());
os.flush();
-
Send an HTTP GET command to the web server over your
OutputStream. Convert the GET command into a
byte array.
String getCommand = "GET " + "/" + " HTTP/1.0\r\n\r\n";
os.write(getCommand.getBytes());
os.flush();
-
Create an
InputConnection object to receive data from your
connection.
InputConnection inputConn = (InputConnection) connection;
-
Retrieve an
InputStream from your
InputConnection.
is = inputConn.openInputStream();
-
Retrieve the stream data and store it in a
byte array.
byte[] data = net.rim.device.api.io.IOUtilities.streamToBytes(is);
result = new String(data);
-
Catch any errors that might be generated by your procedure.
catch(Exception e)
{
result = "ERROR fetching content: " + e.toString();
}
-
Close your
OutputStream.
if(os != null)
{
try
{
os.close();
}
catch(IOException e)
{
// process the error condition
}
}
-
Close your
InputStream.
if(is != null)
{
try
{
is.close();
}
catch(IOException e)
{
}
}
-
Close your
Connection.
{
connection.close();
}
catch(IOException ioe)
{
}
}
-
Call
showContents() that you created in step 6.
showContents(result);
}
}
Was this information helpful? Send us your comments.