The MIDP Connection Framework BLACKBERRY

The BlackBerry uses the same connection framework as defined in the MIDP standard, with some extra functionality specific to the BlackBerry platform.

All connections are initiated using the class. We briefly used this class in Chapter 6 to open a connection to the file system.The same class is used for HTTP, HTTPS, socket, and many other connection types. For example, to open an HTTP connection to retrieve a web page, use the following:

HttpConnection connection = (HttpConnection);

Note that this just retrieves the primary content of the page. To get images and other resources, you’d have to request them specifically.

All the methods return a subclass of the interface. The specific type depends on the scheme of the URL passed in (the http:// portion). There’s a fairly hefty hierarchy of connection types, but for most purposes you’ll directly use only a few, as shown in Table.

Common Network Connection Types, Connection Interface that Connector Returns, and the URL Schcme Indicating Each Type

Common Network Connection Types, Connection Interface that Connector Returns, and the URL Schcme Indicating Each Type

All of the previous network connection types allow a similar format for the URL For example:

HttpConnection connection =
SocketConnection socket = SocketConnection);

We used FileConnection in the previous chapter, which, as you saw, uses the file:// scheme.

HTTP Basics
Because we’ll explore HTTP networking first, we’ve included a quick review of the basics of the protocol. If you’re familiar with HTTP, you can probably safely skip this section, but we want to briefly cover the basics of how the protocol works before we continue to ensure all the terms we use are fresh in your mind. HTTP is the fundamental protocol of the World Wide Web. It’s a connectionless requestresponse protocol, meaning there is no concept of a persistent connection between a series of requests.

Request and Response

An HTTP request is a message sent from the client (in this case, the BlackBerry device) to the server. The server sends back a response. The request and response might contain some content called the body.In addition, the response always contains a numeric response code, which lets us know if the request was successful, if it failed, or if more action is needed. It gives more detailed information about what exactly happened (e.g. the cause of failure).


HTTP supports several request methods, which help the server know how to handle the request. The most important HTTP methods for our purposes are:

  • ET: Used to retrieve a resource (such as a web page or image) from a URL
  • OST: Used to send data to a server (such as the content f a form) based on a URL

In fact, the BlackBerry supports only the GET, POST, and HEAD methods; it doesn’t support custom methods. For most applications, this is sufficient, but it is something to keep in mind.


Finally, in addition to the main content of the request and response (the body), HTTP allows additional data to be sent in the form of headers. They can be sent with the request to the server and the response from the server, and they can contain arbitrary text data. There are many standard headers, and the connection API contains methods for easily accessing some of the most common ones.

The Test Web Application
I created a simple web application to let you easily explore performing HTTP POSTs and GETs from the BlackBerry (See Figure). You can access this application using your browser:

The Test Web Application

The Test Web Application

It consists of a single HTML page containing a PNG image and a text box. When you enter text into the box and click Go! it displays the words you typed in reverse order,one per line with an HTML line break tag between them.

Typing text into the web application

Typing text into the web application

The resulting output from the previous figure

The resulting output from the previous figure

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status