Skip to main content
GameDev.net gamedev.net
🔒 Locked

HTML connection[j2me]

Started by walle Nov 18, 2005 at 2:58 AM 7 replies 1.4k views
Original Post
walle
walle
Hello. I'm trying to make a html connection using MIDP. When my first try didn't work I tried making the same connection from a new midlet, in the midlet itself. And it worked. So now I wanna use this working connection in my gamescreen class(extends canvas) were I have the key input functions. But I can't get it to work. Any ideas? Thanks.
shmoove
shmoove
More information is needed.

What do you mean by "not working"? What errors did you get?
Post some relevant code.

shmoove
walle
walle
This is the code that works:
[source lang=java]import java.io.*;import javax.microedition.midlet.*;import javax.microedition.io.*;import javax.microedition.lcdui.*;/** * An example MIDlet to fetch a page    using an HttpConnection.*/public class SecondExample extends MIDlet {  private Display display;  private String url = 	"http://www.smet.walrus.talkhost.info/index.php?name1=wakka&score1=5440";    public SecondExample() {      display = Display.getDisplay(this);    }  /**   * startApp is invoked when    * the MIDlet is activated.  */  public void startApp() {    //The specified URL is overridden     //in the descriptor    try {      send(url);    } catch(IOException e) {      // handle the exception    }  }  private void send(String url) 	throws IOException {    StringBuffer b = new StringBuffer();    InputStream is = null;    HttpConnection c = null;    TextBox t = null;    try     {      long len = 0 ;      int ch = 0;      c = (HttpConnection)Connector.open(url);      is = c.openInputStream();    } finally {      is.close();      c.close();    }    display.setCurrent(t);  }  /**   * Pause, discontinue....  */  public void pauseApp() { }  /**   * Destroy must cleanup everything.  */  public void destroyApp(boolean unconditional) { }}


This is the function that sends the data in the one that don't work:
[source lang=java]private void send() throws IOException    {        String url;        url = "www.smet.walrus.talkhost.info/index.php?";        for(int i = 0; i < names.length; i++)        {             url += names + "&" + scores + "&";        }                InputStream is = null;        HttpConnection c = null;        try         {            c = (HttpConnection)Connector.open(url);            is = c.openInputStream();        }         finally         {            is.close();            c.close();        }   }

And this is the code in the gamescreen class:
[source lang=java]protected void keyPressed( int keyCode )	{		//get the game action from the passed keyCode		int gameAction = getGameAction( keyCode );		switch( gameAction )		{			case FIRE:			try { highscore.send(); } catch(Exception  e) {}                        colour = 0xFFFFFF;			break;		}	}


This is supposed to send the highscore data when the fire button is pressed, and change color so you can see that the data have been sent.

The error I get is: java.lang.NullPointerException and then the screen locks up...it never changes color.
Edit: changed it a little, so now it don't lock up, but I dont get any html data to the page. And the green network light don't light up, as it does in the one that work.

I think it has something doing with that I don't get any question if its ok to send html, because I get that with the first midlet.

Plese say if I missed some important information.


Thanks.

[Edited by - walle on November 18, 2005 5:36:44 AM]
guyaton
guyaton
I would also suggest getting the URL from the jad file, for it is easier to modify later on. you can do it like this
//this is valid where "URL" is in the midlet like this://URL: http://google.com String url = this.getAppProperty("URL");


doing alot of web programming on cell phones I can tell you this can be very useful especially when urls change unexpectedly [grin]

~guyaton
~guyaton
Blew
Blew
Quote:
Original post by guyaton
I would also suggest getting the URL from the jad file, for it is easier to modify later on. you can do it like this
*** Source Snippet Removed ***

doing alot of web programming on cell phones I can tell you this can be very useful especially when urls change unexpectedly [grin]

~guyaton

Unfortunatelly, that won't work in the cases there is no .jad file involved, like when you install the app via bluetooth (only the jar file is sent).
blew
PMSchuss
PMSchuss
I've only done Bluetooth deployment via the 60 series Nokia phones, but those seem to support JAD files just fine. You just have to send both the JAD and the JAR to the phone and click on the JAD in your inbox. Are there any phones that require Bluetooth deployment that don't support this method?
Blew
Blew
Heh, i never did it that way. I only send the jar to the phone and install it.

Do you know if using both files, as you do, you can access the properties in the jad file after installing it via bluetooth?
blew
trojanman
trojanman
Quote:
Original post by Blew
Heh, i never did it that way. I only send the jar to the phone and install it.

Do you know if using both files, as you do, you can access the properties in the jad file after installing it via bluetooth?


Yes. If you use both files you can access properties in the jad file. This is how locales are often interpreted on devices that support multiple languages.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.