Troubleshooting guide

118
BlackBerry Java Development Environment Development Guide
Using the wireless service providers Internet gateway
Java™ applications for BlackBerry® devices can connect to the Internet using the Internet gateway that the
wireless service provider provides. Most wireless service providers provide their own Internet gateway that offers
direct TCP/IP connectivity to the Internet. Some operators also provide a WAP gateway that lets HTTP
connections occur over the WAP protocol. Java applications for BlackBerry devices can use either of these
gateways to establish connections to the Internet. If you write your application for BlackBerry device users who
are on a specific wireless network, this approach can often yield good results. However, if you write your
application for BlackBerry device users on a variety of wireless networks, testing your application against the
different Internet gateways and achieving a consistent experience can be challenging. In these scenarios, you
might find it useful to use the BlackBerry Internet Service, and use the wireless service provider’s Internet
gateway as a default connection type if the BlackBerry Internet Service is not available.
In the Technical Knowledge Center on the BlackBerry Developer Zone, see the Managing Wireless Data Transport
in the BlackBerry Solution v4.0
Part 1: Understanding TCP and HTTP transport options for Java applications for
BlackBerry
white paper for more information about managing wireless connectivity and how to effectively use
each of the gateways.
Use HTTP connections
Code sample: Using an HTTP connection to retrieve data
The HTTPFetch.java code sample requires that you create resource files in the application project and define the
required resource keys.
See “Localizing applications” on page 235 for more information about creating resource
files.
Task Steps
Before opening an HTTP connection,
verify that the BlackBerry® device is in a
wireless coverage area.
>Use the CoverageInfo class and CoverageStatusListener interface of the
net.rim.device.api.system package to make sure that the BlackBerry device is in a
wireless coverage area.
Open an HTTP connection. 1. Invoke Connector.open(), specifying HTTP as the protocol.
2. Cast the returned object as an HttpConnection or a StreamConnection object.
HttpConnection conn = null;
String URL = "http://www.myServer.com/myContent";
conn = (HttpConnection)Connector.open(URL);
Set the HTTP request method (GET or
POST).
>Invoke HttpConnection.setRequestMethod().
conn.setRequestMethod(HttpConnection.POST);
Set header fields. >Invoke setRequestProperty() on the HttpConnection.
conn.setRequestProperty("User-Agent", "BlackBerry/3.2.1");
Retrieve header fields. >Invoke getRequestProperty() on the HttpConnection.
String lang = conn.getRequestProperty("Content-Language");
Send and receive data. >Invoke openInputStream() and openOutputStream() on the HTTPConnection.
InputStream in = conn.openInputStream();
OutputStream out = conn.openOutputStream();