Why the length of image array differs url is static string or returned from a funct?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • phpuser123
    New Member
    • Dec 2009
    • 108

    Why the length of image array differs url is static string or returned from a funct?

    Code:
    public void run() {
            try {
                System.out.println("Sending message...");
                stringItem.setText(stringItem.getText() + "1");
                sendMessage("Query,map,$,start,211,Arsenal,!");
               stringItem.setText(stringItem.getText() + "2");
                String unformattedurl = receiveMessage();
                stringItem.setText(stringItem.getText() + "3");
                stringItem.setText(unformattedurl);
                 //System.out.println("Receiving message..."+unformattedurl);
                String url = getURL(unformattedurl);
               // System.out.println("formatted url is.."+url);
                //connection.close();
                // url = "http://maps.google.com/maps/api/staticmap?center=100,20&zoom=14&size=500x400&format=JPEG&maptype=roadmap&sensor=false";
                URL = getURL1(unformattedurl);
                URL = url;
                
                stringItem.setText(stringItem.getText() + 6);
                //Imageloader imageloader=new Imageloader(stringItem,item,url);
                System.out.println("Loading image...");
               
                        try {System.out.println("Thread1...");
                           // stringItem.setText(URL);
                        stringItem.setText(stringItem.getText() + 7);
                            System.out.println("Thread2...");
                           // stringItem.setText(stringItem.getText() + URL);
                             image .setImage( loadImage(URL) );
                             stringItem.setText(stringItem.getText() + 8);
                             System.out.println("Thread3...");
                            //item.setImage(image);
                            //mItem.setLabel(mItem.toString());
                            //stringItem.setText(stringItem.getText() + ioe.getMessage());
                        } catch (Exception ex) {
                           stringItem.setText(stringItem.getText() + "Err1"+ex.getMessage());
    
                            System.out.println("ThreadError..."+ex.getMessage());
                        }
                    
                
               
            } catch (Exception ex) {
                stringItem.setText(stringItem.getText()+"Error2 " + ex.getMessage());
            }
    
        }
    
    
    
    
    public Image loadImage(String url) throws IOException {
        stringItem.setText(stringItem.getText() + url);
        HttpConnection hpc = null;
        DataInputStream dis = null;
        try {
          hpc = (HttpConnection) Connector.open(URL);
          //hpc.getResponseMessage();
    
          int length = (int) hpc.getLength();
          System.out.println("Length is "+length);
          stringItem.setText(stringItem.getText() + "len" +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();
        }
      }
    
    
     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";
     }

    When I run the above code with a static url, that is url stored in a string it works fine but as I take the same url and return it from a function I get an null exception and size of image as -1..
Working...