wood burning stoves 2.0*
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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "Getting Live Data Problem Problem" Watch "Getting Live Data Problem Problem" New topic
Author

Getting Live Data Problem Problem

waqas shahwar
Greenhorn

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.*;
import java.io.*;
import javax.microedition.io.*;

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);
locationScreen.append(cityField);
stateField = new TextField("State", "", 2, TextField.ANY);
locationScreen.append(stateField);

// Set the Exit and Go commands for the location screen
locationScreen.addCommand(exitCommand);
locationScreen.addCommand(goCommand);
locationScreen.setCommandListener(this);

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

// Set the Back command for the conditions screen
conditionsScreen.addCommand(backCommand);
conditionsScreen.setCommandListener(this);
}

public void startApp() throws MIDletStateChangeException {
// Set the current display to the location screen
display.setCurrent(locationScreen);
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

public void commandAction(Command c, Displayable s) {
if (c == exitCommand) {
destroyApp(false);
notifyDestroyed();
}
else if (c == goCommand) {
// Get the conditions for the city and state
getConditions(cityField.getString().toUpperCase(),
stateField.getString().toLowerCase());
}
else if (c == backCommand) {
// Clear the location fields
cityField.setString("");
stateField.setString("");

// Set the current display back to the location screen
display.setCurrent(locationScreen);
}
}

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

try {
// Open the HTTP connection
conn = (StreamConnection)Connector.open("http://iwin.nws.noaa.gov/iwin/" +
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 = in.read()) != '\003') && (ch != -1) && !done) {
if (ch != '\n') {
// Read the line a character at a time
data.append((char)ch);
}
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
display.setCurrent(conditionsScreen);

// 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
author
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: http://www.amazon.com/exec/obidos/ASIN/0137129394/mobileenterpr-20/
Ringful: http://www.ringful.com/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting Live Data Problem Problem