Hi,
I have just made a simple program to download some text. I can only use any Internet access point. If I use WAP access, it does not work. I would really appreciate if anybody knows how to use WAP access point in MIDLET.
The source code is given below:
Code:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
public class NetDemo extends MIDlet
{
private Display display;
private String url = "http://www.javacourses.com/hello.txt";
public NetDemo()
{
display = Display.getDisplay(this);
}
/**
* This will be invoked when we activate the MIDlet.
*/
public void startApp()
{
// Use the specified URL is overriden in the descriptor
try
{
downloadPage(url);
}
catch(IOException e)
{
// handle the exception
}
}
private void downloadPage(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);
c.setRequestProperty( "User-Agent","Profile/MIDP-1.0 Configuration/CLDC-1.0" );
is = c.openInputStream();
len =c.getLength() ;
if ( len != -1)
{
// Read exactly Content-Length bytes
for (int i =0 ; i < len ; i++ )
{
if ((ch = is.read()) != -1)
{
b.append((char) ch);
}
}
}
else
{
// Read till the connection is closed.
while ((ch = is.read()) != -1)
{
len = is.available() ;
b.append((char)ch);
}
}
t = new TextBox("hello again....", b.toString(), 1024, 0);
}
finally
{
is.close();
c.close();
}
display.setCurrent(t);
}
/**
* Pause, discontinue....
*/
public void pauseApp()
{
}
/**
* Destroy must cleanup everything.
*/
public void destroyApp(boolean unconditional)
{
}
}