This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Java Micro Edition and the fly likes Getting Live Data Problem Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "Getting Live Data Problem Problem" Watch "Getting Live Data Problem Problem" New topic

Getting Live Data Problem Problem

waqas shahwar

Joined: Sep 17, 2003
Posts: 19
Hello world i have a problem with following weather MIDlet when i sonnect net and launch emulator to execute weather midlet then emulator hangs and cann't move to select lists how can i run this midlet to get live weather data
message is this
weather wants to send information this will require the use of AIRTIME which may cost u money is this ok (null)

here is complete code

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;

public class Weather extends MIDlet implements CommandListener {
private Command exitCommand, goCommand, backCommand;
private Display display;
private Form locationScreen;
private TextField cityField, stateField;
private Form conditionsScreen;
private StringItem locationItem, conditionsItem, temperatureItem,
humidityItem, windItem;

public Weather() {
// Get the Display object for the MIDlet
display = Display.getDisplay(this);

// Create the Exit, Go, and Back commands
exitCommand = new Command("Exit", Command.EXIT, 2);
goCommand = new Command("Go", Command.OK, 2);
backCommand = new Command("Back", Command.BACK, 2);

// Create the location screen form
locationScreen = new Form("Enter Location");
cityField = new TextField("City", "", 25, TextField.ANY);
stateField = new TextField("State", "", 2, TextField.ANY);

// Set the Exit and Go commands for the location screen

// Create the conditions screen form
conditionsScreen = new Form("Current Conditions");
locationItem = new StringItem("", "");
conditionsItem = new StringItem("", "");
temperatureItem = new StringItem("", "");
humidityItem = new StringItem("", "");
windItem = new StringItem("", "");

// Set the Back command for the conditions screen

public void startApp() throws MIDletStateChangeException {
// Set the current display to the location screen

public void pauseApp() {

public void destroyApp(boolean unconditional) {

public void commandAction(Command c, Displayable s) {
if (c == exitCommand) {
else if (c == goCommand) {
// Get the conditions for the city and state
else if (c == backCommand) {
// Clear the location fields

// Set the current display back to the location screen

private void getConditions(String city, String state) {
StreamConnection conn = null;
InputStream in = null;
StringBuffer data = new StringBuffer();

try {
// Open the HTTP connection
conn = (StreamConnection)"" +
state + "/hourly.html");

// Obtain an input stream for the connection
in = conn.openInputStream();

// Read a line at a time from the input stream
int ch;
boolean done = false;
while (((char)(ch = != '\003') && (ch != -1) && !done) {
if (ch != '\n') {
// Read the line a character at a time
else {
// Make sure the line is long enough
if (data.length() >= city.length()) {
// See if the line starts with the city name
if ((city.length() > 0) &&
(data.toString().substring(0, city.length()).compareTo(city) == 0)) {
// Fill in the conditions string items
locationItem.setText(city + ", " + state.toUpperCase());
conditionsItem.setText("Cond.: " +
data.toString().substring(15, 22));
temperatureItem.setText("Temperature: " +
data.toString().substring(25, 27) + '\260');
humidityItem.setText("Rel. Humidity: " +
data.toString().substring(33, 35) + "%");
windItem.setText("Wind: " +
data.toString().substring(36, 44) + " mph");

// Set the current display to the conditions screen

// We're done, so bail out of the outer while loop
done = true;
// Clear the string for the next line
data = new StringBuffer();

// The done flag tells us if there was a problem
if (!done)
display.setCurrent(new Alert("Weather",
"The location is invalid. Please try another.", null, AlertType.ERROR));
catch (IOException e) {
System.err.println("The connection could not be established.");
Michael Yuan
Ranch Hand

Joined: Mar 07, 2002
Posts: 1427
You need to run the network code in a separate thread. Otherwise, it blocks the UI.

Seam Framework:
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: Getting Live Data Problem Problem
It's not a secret anymore!