×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    3

    [Solved] Error opening google maps url (static api)

    Hi everybody.
    In my application I need to download a map using google maps static apis.
    I build the url using coordinates acquired from GPS.

    The problem is that if I try to open this url, an exception is thrown and I cannot download the map.
    I tried with a simpler url ( http://imgtops.sourceforge.net/bakeoff/o-png24.png ) and it worked properly: a popup asking to establish a connection appeared and everything was fine.
    When using google maps url instead, I don't even get the popup message asking for a connection.

    The url I'm trying to open is something similar to: http://maps.google.com/maps/api/stat...8749,10.418579

    I'm using a Nokia E51.
    Everything works fine on WTK 2.5.2 emulator.

    Here's the code:
    Code:
    public byte[] getPic() throws IOException {
    		HttpConnection cc = null;
    		DataInputStream dis = null;
    		ByteArrayOutputStream baos = null;
    		byte pic[] = null;
    		
    		try {
    			cc = (HttpConnection) Connector.open(jj.getUrl());
    			cc.setRequestMethod(HttpConnection.GET);
    			dis = cc.openDataInputStream();
    			int ll = (int) cc.getLength();
    			
    			if (ll != -1) {
    				pic = new byte[ll];
    				dis.readFully(pic);
    			}
    			else {
    				baos = new ByteArrayOutputStream();
    				int ch;
    				while ((ch = dis.read()) != -1) {
    					baos.write(ch);
    				}
    				pic = baos.toByteArray();
    				baos.close();
    			}
    		}
    		finally {
    			if (cc != null)
    				cc.close();
    			if (dis != null)
    				dis.close();
    			if (baos != null)
    				baos.close();
    		}
    		return pic;
    	}
    jj.getUrl() provides an url string similar to the one above.

    many Thanks.
    Last edited by puller86; 2010-06-27 at 12:25. Reason: Solved

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    Noida
    Posts
    941

    Re: Error opening google maps url (static api)

    Quote Originally Posted by puller86 View Post
    Hi everybody.
    In my application I need to download a map using google maps static apis.
    I build the url using coordinates acquired from GPS.

    The problem is that if I try to open this url, an exception is thrown and I cannot download the map.
    I tried with a simpler url ( http://imgtops.sourceforge.net/bakeoff/o-png24.png ) and it worked properly: a popup asking to establish a connection appeared and everything was fine.
    When using google maps url instead, I don't even get the popup message asking for a connection.

    The url I'm trying to open is something similar to: http://maps.google.com/maps/api/stat...8749,10.418579

    I'm using a Nokia E51.
    Everything works fine on WTK 2.5.2 emulator.

    jj.getUrl() provides an url string similar to the one above.

    many Thanks.
    Check the URL encoding of your URL its should properly escaped , you must be getting URL encoding exception.

    See the FN Wiki code example on google map - http://wiki.forum.nokia.com/index.ph...API_in_Java_ME
    see - private static String urlEncode(String str) method.
    Thanks,
    Ekta

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    3

    Re: Error opening google maps url (static api)

    Thank you very much!
    It was only necessary to encode "|" and "," characters with %7C and %2C respectively, no other illegal chars were present in my url.
    I encoded while building the url in a previous step.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Location
    Noida
    Posts
    941

    Re: Error opening google maps url (static api)

    Quote Originally Posted by puller86 View Post
    Thank you very much!
    It was only necessary to encode "|" and "," characters with %7C and %2C respectively, no other illegal chars were present in my url.
    I encoded while building the url in a previous step.
    I can even see some : characters in your URL which also need encoding.

    Code:
    //Only these are the only un reserved vhar's for URL all others need encoding ...
    private static final String URL_UNRESERVED =  
            "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +  
            "abcdefghijklmnopqrstuvwxyz" + 
            "0123456789-_.~"; 
    
    
     private static String urlEncode(String str) { 
            StringBuffer buf = new StringBuffer(); 
            byte[] bytes = null; 
            try { 
                ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
                DataOutputStream dos = new DataOutputStream(bos); 
                dos.writeUTF(str); 
                bytes = bos.toByteArray(); 
            } catch (IOException e) { 
                // ignore 
            } 
            for (int i = 2; i < bytes.length; i++) { 
                byte b = bytes[i]; 
                if (URL_UNRESERVED.indexOf(b) >= 0) { 
                    buf.append((char) b); 
                } else { 
                    buf.append('%').append(HEX[(b >> 4) & 0x0f]).append(HEX[b & 0x0f]); 
                } 
            } 
            return buf.toString(); 
        }
    Thanks,
    Ekta

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    3

    Re: Error opening google maps url (static api)

    Quote Originally Posted by ektasrv View Post
    I can even see some : characters in your URL which also need encoding.

    Code:
    //Only these are the only un reserved vhar's for URL all others need encoding ...
    private static final String URL_UNRESERVED =  
            "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +  
            "abcdefghijklmnopqrstuvwxyz" + 
            "0123456789-_.~"; 
    
    
     private static String urlEncode(String str) { 
            StringBuffer buf = new StringBuffer(); 
            byte[] bytes = null; 
            try { 
                ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
                DataOutputStream dos = new DataOutputStream(bos); 
                dos.writeUTF(str); 
                bytes = bos.toByteArray(); 
            } catch (IOException e) { 
                // ignore 
            } 
            for (int i = 2; i < bytes.length; i++) { 
                byte b = bytes[i]; 
                if (URL_UNRESERVED.indexOf(b) >= 0) { 
                    buf.append((char) b); 
                } else { 
                    buf.append('%').append(HEX[(b >> 4) & 0x0f]).append(HEX[b & 0x0f]); 
                } 
            } 
            return buf.toString(); 
        }
    Yes, you're right, ':' must be encoded when not used in protocol portion of the url .
    Many thanks again.

Similar Threads

  1. J2ME Google Maps API java.lang.SecurityException error
    By elaltaico in forum Mobile Java General
    Replies: 7
    Last Post: 2009-11-09, 06:00
  2. Google Maps Api problem
    By ingsaurabh in forum Mobile Java General
    Replies: 7
    Last Post: 2009-07-29, 16:38
  3. About Google Maps API
    By lysander in forum Mobile Java Networking & Messaging & Security
    Replies: 1
    Last Post: 2009-01-23, 13:52
  4. Google Maps using Location Api
    By Burim in forum Symbian
    Replies: 1
    Last Post: 2008-08-10, 18:32
  5. Google Maps API to Symbian
    By Olaris in forum Symbian
    Replies: 1
    Last Post: 2007-06-18, 18:30

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •