如何加载图像?

时间:2011-02-26 18:19:51

标签: google-maps http url java-me

以下代码向服务器发送请求,服务器发回响应。然后,客户端格式化请求并使其成为在imageitem上加载的URL。这可以使用模拟器正常工作,但是当我将它部署在我的mob上时,它能够从我的服务器检索字符串,但是当它尝试加载图像时,它会显示NegativeArrayException。

注:1。我已经激活了我的暴徒网络      2.当我使用静态网址时,它工作正常,而不是传递格式化网址,我传递一个静态网址。

如何加载图像?

 public void run() {
        try {
            sendMessage("Query,map,$,start,211,Arsenal,!");
            String unformattedurl = receiveMessage();

            String url = "http://maps.google.com/maps/api/staticmap?center=100,20&zoom=14&size=500x400&format=JPEG&maptype=roadmap&sensor=false";
           URL = getURL(unformattedurl);


                        stringItem.setText(URL);
                        Image image = loadImage(URL);
                        item.setImage(image);

        } catch (Exception ex) {
            stringItem.setText(stringItem.getText() + "Err2"+ex.getMessage());
        }

    }



 private String getURL1(String message){
     return "http://maps.google.com/maps/api/staticmap?center=Mauritius&zoom=10&size=200x200&maptype=roadmap&markers=color:Blue|label:U|22.0,22.0&markers=color:Green|label:B|21.0,21.0&sensor=false";
 }



    private String getURL(String message) throws Exception{

        String[] messagearr=client.split(message,",");

        String color,object;
        String url="http://maps.google.com/maps/api/staticmap?center=Mauritius&zoom=10&size="+200+"x"+200+"&maptype=roadmap";

        String lat,longitude;

        if (messagearr[0].equals("query")){

            if (messagearr[1].equals("Error")){
                String error=messagearr[2];

            }
            else {

                int startindex=5;
                String distance,time;
                distance=messagearr[2];
                time=messagearr[3];
                String name=messagearr[4];
                String 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);

                }
                url+="&sensor=false";


                return url;
            }
        }

        throw new Exception("Url is wrong");
    }

     private void sendMessage(String message) throws IOException{

            DataOutputStream dos = connection.openDataOutputStream();

            dos.writeUTF(message);

    }

 private String receiveMessage() throws IOException{
       //stringItem.setText(stringItem.getText()+5);
      DataInputStream dis = connection.openDataInputStream();
      //stringItem.setText(stringItem.getText()+6);
      return dis.readUTF();
  }
public Image loadImage(String url) throws IOException {
    HttpConnection hpc = null;
    DataInputStream dis = null;
    try {
      hpc = (HttpConnection) Connector.open(url);

      int length = (int) hpc.getLength();
      System.out.println("Length is "+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();
    }
  }
}

这和我的另一个问题是相同的,除了我有更详细的定义问题。 我还将URL存储在stringItem中,当我手动输入到我的broser中时,我加载了图像?

当我尝试hpc.getResponseMessage()时,我抛出了一个IOException ..

0 个答案:

没有答案
相关问题