How do I get my map to load in j2me?

369 Views Asked by At

My midlet

public void commandAction(Command command, Displayable displayable) {
        // write pre-action user code here
        if (displayable == form) {
            if (command == exitCommand) {
                // write pre-action user code here
                exitMIDlet();
                // write post-action user code here
            } else if (command == okCommand) {
                try {
                    // write pre-action user code here

                  connection = startConnection(5000, myip);
                  sendMessage("Query,map,$,start,211,Arsenal,!");
                  String unformattedurl=        receiveMessage();
                  stringItem.setText(stringItem.getText()+"  "+unformattedurl);
                  String url=getURL(unformattedurl,200,200);
                  Imageloader imageloader=new Imageloader(stringItem,item,url);
                  Thread thread=new Thread(imageloader);
                  thread.start();
                }
                catch(Exception e){
                        stringItem.setText(stringItem.getText()+"  "+e.getMessage());
                }
            }
        }
        // write post-action user code here
    }


private void sendMessage(String message) throws IOException{
        stringItem.setText(stringItem.getText()+2);
            DataOutputStream dos = connection.openDataOutputStream();
            System.out.println("Sending...."+message);
            dos.writeUTF(message);
             stringItem.setText(stringItem.getText()+3);
    }


     public SocketConnection startConnection(int port,String ip) throws IOException{
        return (SocketConnection) Connector.open("socket://"+ip+":"+port);
    }



     public static String[] split(String str,String delim) throws Exception{
        String copy=str;
        int len=0;
        int index = copy.indexOf(delim)+1;
                if (index==0){
                        throw new Exception("Data format not supported");

                }
                else{

                        int prev_index=0;
                        Vector array=new Vector();
                        //System.out.println(index);
                        array.addElement(copy.substring(0,index-1));

                        while (index !=0){
                                len++;
                                copy=copy.substring(index);
                                index = copy.indexOf(delim)+1;
                                //System.out.println(copy+"   "+index);
                                if (copy.indexOf(delim)==-1){
                                        array.addElement(copy);
                                }
                                else{
                                        //System.out.println(0+"     "+copy.indexOf(delim));
                                        array.addElement(copy.substring(0,copy.indexOf(delim)));
                                }
                        }
                        String[] array2=new String [array.size()];
                        for (int i=0;i<array.size();i++){
                                array2[i]=(String)array.elementAt(i);
                                //System.out.println(array2[i]);
                        }

                        return array2;
                }
    }

  private String receiveMessage() throws IOException{
       stringItem.setText(stringItem.getText()+5);
      DataInputStream dis = connection.openDataInputStream();
      stringItem.setText(stringItem.getText()+6);
      return dis.readUTF();
  }




    private String getURL(String message,int width,int height) throws Exception{
        item.setLabel(item.getLabel()+6);
        System.out.println("TEst1");
        //System.out.println("Formatted message is "+query);
        //System.out.println("Getting height......"+label.getMaximumSize().height);
        String[] messagearr=split(message,",");
        System.out.println("TEst2");
        String color,object;
        String url="http://maps.google.com/maps/api/staticmap?center=Mauritius&zoom=10&size="+width+"x"+height+"&maptype=roadmap";
        //&markers=color:green|label:Bus21|40.702147,-74.015794&markers=color:blue|label:User|40.711614,-74.012318&sensor=false";
        String lat,longitude;

         System.out.println("TEst3");
        if (messagearr[0].equals("query")){
             System.out.println("TEst4");
            if (messagearr[1].equals("Error")){
                String error=messagearr[2];
                 System.out.println("TEst5"+error);
                 item.setAltText(error);
                 item.setLabel("Test"+error);
            }
            else {
                System.out.println("Wrong number");
                int startindex=5;
                String distance,time;
                distance=messagearr[2];
                time=messagearr[3];
                String name=messagearr[4];
                imageLabel="The bus is at :"+time+"min and "+distance +"km from user";


                for (int i=startindex;i<messagearr.length;i+=2){
                     System.out.println("TEst8");
                     lat=messagearr[i];
                     longitude=messagearr[i+1];

                    if (i==startindex){
                        object="U";
                        color="Blue";
                    }

                    else{
                        object="B";
                        color="Green";
                    }


                     url=url+"&markers=color:"+color+"|label:"+object+"|"+lat+","+longitude;
                     item.setLabel(7+url);
                     System.out.println("TEst10"+" name "+name+" long "+longitude+" lat "+lat+" url "+url );
                }
                url+="&sensor=false";

                System.out.println("Image loaded ....  "+url);
                return url;
            }
        }
         item.setLabel(item.getLabel()+8);
        throw new Exception("Url is wrong");
    }
}





public class Imageloader implements Runnable{

  StringItem stringitem=null;
  ImageItem imageitem=null;
  String url=null;
Imageloader(StringItem stringitem,ImageItem imageitem,String url){
    this.stringitem=stringitem;
    this.imageitem=imageitem;
    this.url=url;
    stringitem.setText(stringitem.getText()+1);
}

private void loadImage() throws IOException{
    stringitem.setText(stringitem.getText()+2);
    imageitem.setImage(getImage(url));
}

public Image getImage(String url) throws IOException {


stringitem.setText(stringitem.getText()+3);
    HttpConnection hpc = null;
    DataInputStream dis = null;
    try {
      hpc = (HttpConnection) Connector.open(url);
      System.out.println(hpc.getResponseMessage());
      //hpc.getResponseMessage()
      stringitem.setText(stringitem.getText()+" ... "+hpc.getResponseMessage());
      int length = (int) hpc.getLength();
      System.out.println("Length is "+length);
      stringitem.setText(stringitem.getText()+" ... "+length);
      byte[] data = new byte[length];
      dis = new DataInputStream(hpc.openInputStream());
      dis.readFully(data);
      return Image.createImage(data, 0, data.length);
    } finally {
      if (hpc != null)
        hpc.close();
      if (dis != null)
        dis.close();
    }
  }

    public void run() {
        try {
            stringitem.setText(stringitem.getText()+5);
            loadImage();
        } catch (IOException ex) {
            imageitem.setLabel(ex.getMessage()+5);
            ex.printStackTrace();
        }
    }
}

I am unable to load an image in my mobile phone howerver it works fine using an emulator. The exception says "Error in HTTP connection".. and it occurs in the loadImage() method ..

How do I sort this out?

1

There are 1 best solutions below

1
On BEST ANSWER

make sure your application has access to make http connection and you have got working connection with your phone