danielagaba 0 Junior Poster in Training

hi

i'm using the following code to enable a midlet to pull information from a php file. my problem is when i run the midlet in the wireless toolkit emulator, instead of showing the information, it displays "Error ...". I am using wamp to host the php page

J2ME code:

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


public class Test extends MIDlet implements CommandListener {
	static final int DefaultTimeout = 2000;
    Display display;
	String url = "http://localhost/mobile/testGET.php";
    /** user interface alert component. */
    Alert splashScreenAlert;
    Image splashScreen;
	Form mainForm;
	static final Command exitCommand = new Command("Exit", Command.STOP, 1);
	static final Command chkCommand = new Command("Check", Command.SCREEN, 2);
    boolean imageLoaded;
	private boolean firstTime;
	ChoiceGroup CoursePOP;
	

    public WhatsHot() {
        display = Display.getDisplay(this);
		mainForm = new Form("Whats Hot");
		mainForm.addCommand(exitCommand);
		mainForm.addCommand(chkCommand);
		mainForm.setCommandListener(this);
		firstTime = true;
		CoursePOP = new ChoiceGroup ("Whats Hot Where ... ", Choice.POPUP,new String[] {"Item1", "Item2", 
			"Item3"}, null);
		mainForm.append(CoursePOP);
        
        
        try {
            splashScreen = Image.createImage("/sample/whatshot/screen.png");
            imageLoaded = true;
        } catch (java.io.IOException ex) {
        }

        splashScreenAlert = new Alert("Whats Hot", "", splashScreen, AlertType.INFO);
        splashScreenAlert.setTimeout(DefaultTimeout);     
    }
	
	 public void commandAction(Command c, Displayable s) {
		String label = c.getLabel();
		if (label.equals("Exit")) {
			destroyApp(true);
		}else if (label.equals("Check")){
	
			Thread t = new Thread(){
				public void run(){
					try {
				testGET(url);
				} catch (IOException e) {
				System.out.println("IOException " + e);
				e.printStackTrace();
				}
				}
			};
		t.start();
		}
    }

    public void startApp() {
		if (firstTime) {
            display.setCurrent(splashScreenAlert, mainForm);
            firstTime = false;
        }	    
    }

    public void destroyApp(boolean unconditional) {
    }

    public void pauseApp() {
    }
	
	void testGET(String url) throws IOException {
        HttpConnection connection = null;
        InputStream is = null;
        OutputStream os = null;
        StringBuffer stringBuffer = new StringBuffer();
        TextBox textBox = null;

        try {
          connection = (HttpConnection)Connector.open(url);
          connection.setRequestMethod(HttpConnection.GET);
          connection.setRequestProperty("IF-Modified-Since","20 Jan 2001 16:19:14 GMT");
          connection.setRequestProperty("User-Agent","Profile/MIDP-2.0 Confirguration/CLDC-1.0");
          connection.setRequestProperty("Content-Language", "en-CA");
          connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
          os = connection.openOutputStream();
          is = connection.openDataInputStream();
          int ch;
          while ((ch = is.read()) != -1) {
            stringBuffer.append((char) ch);
          }
          textBox = new TextBox("Simple GET Test", stringBuffer.toString(), 1024, 0);
        } finally {
           if(is!= null) {
              is.close();
           }
           if(os != null) {
              os.close();
           }
           if(connection != null) {
              connection.close();
           }
        }
        display.setCurrent(textBox);
    }
	

    
}

PHP code:

<?php
$response = "Hello";

if (isset($_GET)) {
  switch ($_GET["type"]) {
    case 1: $response = "Good Morning"; break;
    case 2: $response = "Good Afternoon"; break;
    case 3: $response = "Good Evening"; break;
    default: $response = "Hello"; break;
  }  
}
echo $response;
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.