Hi,
I have been trying to build a location based application. For tht i hav used network parameters and gt the location frm a stored database of n/w parameters and their corresponding positions. Sometimes the application runs correctly bt sometimes i get an unhandled exception.
I am attaching the code.
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.ContentConnection;
import javax.microedition.io.StreamConnection;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class HelloWorld extends MIDlet implements CommandListener{
private Form form;
private Display display;
private Command exit = new Command("Exit", Command.EXIT, 1);
String url = "";
private String S60_cell_id;
private String Signal;
private Form form1;
private Command cmExit;
private Command cmView;
private Form fmViewPng;
String loc = null;
public HelloWorld(){
super();
}
public void startApp(){
form = new Form("Hello World");
String msg = "Hello World!!!!!!!";
form.append(msg);
form.addCommand(exit);
cmView = new Command("View", Command.SCREEN, 2);
form.addCommand(cmView);
display = Display.getDisplay(this);
display.setCurrent(form);
S60_cell_id = System.getProperty("com.nokia.mid.cellid");
Signal = System.getProperty("com.nokia.mid.networksignal");
// S60_cell_id="1351";
// Signal="78";
url = "http://locateme.co.in/welcome.php?cellid="+S60_cell_id+"&signal="+Signal;
form.setCommandListener(this);
connection(url);
}
public void pauseApp(){}
public void exit(){
destroyApp(false);
notifyDestroyed();
}
public void destroyApp(boolean unconditional){
notifyDestroyed();
}
private void connection(String url) {
StreamConnection sc = null;
InputStream is = null;
StringBuffer buffer = new StringBuffer();
try {
sc = (StreamConnection) Connector.open(url);
is = sc.openInputStream();
int chars;
while ((chars = is.read()) != -1) {
if ((chars != ' ')&&(chars!='\n')) {
buffer.append((char) chars);
}
}
System.out.println(buffer.toString());
loc = buffer.toString();
form.append(loc);
} catch (IOException ex1) {
ex1.printStackTrace();
}
finally{
if(is != null){
try {
is.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
if(sc != null){
try {
sc.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
public void commandAction(Command c, Displayable d) {
if (c == exit) {
exit();
}
if (c == cmExit)
{
destroyApp(false);
notifyDestroyed();
}
else if (c == cmView)
{
int width = form.getWidth();
int height = form.getHeight();
url ="http://maps.google.com/maps/api/staticmap?center="+loc+"&format=png32&zoom=14&size="+width+"x"+height+"&markers=color:blue|label:S|"+loc+"&sensor=true&key=ABQIAAAAe-XrGezTSfvomiYHOAdjexSrqYa9FVv0ZzmTKrWlYncjiNW5DhSw7OuYiFstDeXcRsMloWpJpsiXrQ";
// url = "http://maps.google.com/staticmap?center=" + loc + "&format=png32&zoom=15&size="+width+"x"+height + "&key=ABQIAAAAiyHApDe39izCxX3VkLY8jhSrqYa9FVv0ZzmTKrWlYncjiNW5DhSt_6ACgssm3qmm3HiM6dN0jctQhQ";
cmExit = new Command("Exit", Command.EXIT, 1);
// Create the form that will hold the png image
fmViewPng = new Form("");
display.setCurrent(fmViewPng);
fmViewPng.addCommand(cmExit);
// Set up a listener for form
fmViewPng.setCommandListener(this);
// Download image and place on the form
try
{
Image im;
if ((im = getImage(url)) != null)
{
ImageItem ii = new ImageItem(null, im, ImageItem.LAYOUT_DEFAULT, null);
// If there is already an image, set (replace) it
if (fmViewPng.size() != 0)
fmViewPng.set(0, ii);
else // Append the image to the empty form
fmViewPng.append(ii);
}
else
fmViewPng.append("Unsuccessful download.");
// Display the form with the image
display.setCurrent(fmViewPng);
}
catch (Exception e)
{
System.err.println("Msg: " + e.toString());
}
}
}
private Image getImage(String url) throws IOException
{
ContentConnection connection = (ContentConnection) Connector.open(url);
// * There is a bug in MIDP 1.0.3 in which read() sometimes returns
// an invalid length. To work around this, I have changed the
// stream to DataInputStream and called readFully() instead of read()
// InputStream iStrm = connection.openInputStream();
DataInputStream iStrm = connection.openDataInputStream();
ByteArrayOutputStream bStrm = null;
Image im = null;
try
{
// ContentConnection includes a length method
byte imageData[];
int length = (int) connection.getLength();
if (length != -1)
{
imageData = new byte[length];
// Read the png into an array
// iStrm.read(imageData);
iStrm.readFully(imageData);
}
else // Length not available...
{
bStrm = new ByteArrayOutputStream();
int ch;
while ((ch = iStrm.read()) != -1)
bStrm.write(ch);
imageData = bStrm.toByteArray();
bStrm.close();
}
// Create the image from the byte array
im = Image.createImage(imageData, 0, imageData.length);
}
finally
{
// Clean up
if (iStrm != null)
iStrm.close();
if (connection != null)
connection.close();
if (bStrm != null)
bStrm.close();
}
return (im == null ? null : im);
}
}

Reply With Quote

