• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Getting Live Data Problem Problem

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.");
}
}
}
 
author
Posts: 1436
6
Python TypeScript Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to run the network code in a separate thread. Otherwise, it blocks the UI.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic