This week's giveaway is in the Programmer Certification (OCPJP) forum.
We're giving away four copies of Java Mock Exams (software) and have David Mayer on-line!
See this thread for details.
Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting Live Data Problem Problem

waqas shahwar
Posts: 19
  • Mark post as helpful
  • send pies
  • 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.*;

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
Posts: 1427
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to run the network code in a separate thread. Otherwise, it blocks the UI.
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!