如何在j2me中加载我的地​​图?

时间:2011-02-11 06:35:07

标签: java google-maps java-me mobile httpconnection

我的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();
        }
    }
}

我无法在手机中加载图像,使用模拟器可以正常工作。 例外情况说“HTTP连接错误”.. 它出现在loadImage()方法中..

如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

确保您的应用程序有权进行http连接,并且您已与手机建立了工作连接