Troubleshooting guide

119
7: Creating connections
Example: HTTPFetch.java
/**
* HTTPFetch.java
* Copyright (C) 2001-2005 Research In Motion Limited. All rights reserved.
*/
package com.rim.samples.docs.httpfetch;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.i18n.*;
import net.rim.device.api.system.*;
import javax.microedition.io.*;
import java.io.*;
import com.rim.samples.docs.resource.*;
public class HTTPFetch extends UiApplication implements HTTPFetchResource
{
// Constants.
private static final String SAMPLE_PAGE = “http://localhost/testpage/sample.txt”;
private static final String[] HTTP_PROTOCOL = {“http://”, “http:\\”};
// Members.
private MainScreen _mainScreen;
private RichTextField _content;
/**
* Send and receive data over the network on a
* separate thread from the main thread of your application.
*/
ConnectionThread _connectionThread = new ConnectionThread();
//statics
private static ResourceBundle _resources = ResourceBundle.getBundle(
HTTPFetchResource.BUNDLE_ID, HTTPFetchResource.BUNDLE_NAME);
public static void main(String[] args) {
HTTPFetch theApp = new HTTPFetch();
theApp.enterEventDispatcher();
}
/**
* The ConnectionThread class manages the HTTP connection.
* Fetch operations are not queued, but if a second fetch request
* is made while a previous request is still active,
* the second request stalls until the previous request completes.
*/
private class ConnectionThread extends Thread {
private static final int TIMEOUT = 500; //ms
private String _theUrl;
/* The volatile keyword indicates that because the data is shared,
* the value of each variable must always be read and written from memory,
* instead of cached by the VM. This technique is equivalent to wrapping