Troubleshooting guide
121
7: Creating connections
InputStream input = s.openInputStream();
// Extract data in 256 byte chunks.
byte[] data = new byte[256];
int len = 0;
StringBuffer raw = new StringBuffer();
while ( -1 != (len = input.read(data)) ) {
raw.append(new String(data, 0, len));
}
String text = raw.toString();
updateContent(text);
input.close();
s.close();
} catch (IOException e) {
System.err.println(e.toString());
// Display the text on the screen.
updateContent(e.toString());
}
// Reset the start state.
_start = false;
}
}
}
}
private final class HTTPMainScreen extends MainScreen
{
// Close the connection thread when the user closes the application.
public void close() {
_connectionThread.stop();
super.close();
}
}
// Constructor.
public HTTPFetch() {
_mainScreen = new HTTPMainScreen();
_mainScreen.setTitle(new LabelField(
_resources.getString(APPLICATION_TITLE), LabelField.ELLIPSIS
| LabelField.USE_ALL_WIDTH));
_mainScreen.add(new SeparatorField());
_content = new RichTextField(
_resources.getString(HTTPDEMO_CONTENT_DEFAULT));
_mainScreen.add(_content);
// Start the helper thread.
_connectionThread.start();
pushScreen(_mainScreen);
fetchPage(SAMPLE_PAGE);
}
// Retrieve web content.
private void fetchPage(String url) {
// Perform basic validation (set characters to lowercase and add http:// or https:/
/).
String lcase = url.toLowerCase();
boolean validHeader = false;