×

Discussion Board

Results 1 to 7 of 7
  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    6

    Problem with httpConnection

    Hey guys (and girls)!

    Currently I'm using the Google Static Maps API, this all works fine until I start using the "|" character in my URL. When using this character it throws an exception on the last line of code (Error in HTTP operation). Anybody any ideas how to work around this issue?


    Code:
     
     HttpConnection hc = null;
     DataInputStream in = null;
    
     String url = "http://maps.google.com/staticmap?path=37.40139,-122.05708|37.40139,-122.09639&zoom=13&size=245x245";
                       
     hc = (HttpConnection)Connector.open(url);
     in = new DataInputStream(hc.openInputStream());
    Thanks

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Problem with httpConnection

    "|" is not a valid character in a URL. Replace it with "%7C".

    Graham.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    18

    Re: Problem with httpConnection

    You can use this code to escape urls:

    Code:
      private static char[] ESCAPED = {
            ' ', '<', '>', '#', '%', '{', '}', '|', '\\', '^',
            '~', '[', ']', '`', ';', '?', ':', '@', '=', '&', '$'
        };
    
        public static String escapeChar(
                char c) {
            for (int j = 0; j <
                    ESCAPED.length; j++) {
                if (c == ESCAPED[j]) {
                    return '%' + Integer.toHexString(c);
                }
    
            }
            return String.valueOf(c);
        }
    
        public static String escapeURL(
                String s) {
            if (s == null || s.equals("")) {
                return "";
            }
    
            StringBuffer escaped = new StringBuffer();
    
            for (int i = 0; i <
                    s.length(); i++) {
                char c = s.charAt(i);
    
                escaped.append(escapeChar(c));
            }
    
            return escaped.toString();
        }

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    6

    Re: Problem with httpConnection

    Thanks!

    The html representation of the pipe character works fine!

  5. #5
    Registered User
    Join Date
    Sep 2007
    Location
    Bangalore
    Posts
    868

    Re: Problem with httpConnection

    Hi Thomassius,
    The ideal way of doing this is if there is any charecter rather than 1-9 , a-z and A-Z then put the Equivalent hex value
    StringBuffer sb = new StringBuffer("");
    char c;
    for (int i = 0; i < s.length(); i++) {
    c = s.charAt(i);
    if ( (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) {
    sb.append(c);
    continue;
    }

    if (c > 15) {
    sb.append("%" + Integer.toHexString( (int) c));
    }else {
    sb.append("%0" + Integer.toHexString( (int) c)) ;
    }
    }

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    18

    Re: Problem with httpConnection

    bhanuchandar.k:

    Nice, better idea

    Edit:

    No, I'm afraid its not a good idea after all - what about '?' and '&'?

  7. #7
    Registered User
    Join Date
    Sep 2007
    Location
    Bangalore
    Posts
    868

    Re: Problem with httpConnection

    Hi psorobka,
    You have to encode only the data part not entire url
    i.e if your request is like this
    http://www.xxx.com/AppName/yyy.jsp?A...=value&c=value

    You have to do the encoding of values only not the entire url

Similar Threads

  1. Problem MMC+USB Connection
    By Fede81 in forum Symbian
    Replies: 1
    Last Post: 2007-06-14, 06:13
  2. Problem MMC+USB Connection
    By Fede81 in forum General Development Questions
    Replies: 3
    Last Post: 2006-07-19, 15:21
  3. Very Challenging HTTPConnection Problem
    By MananW in forum Mobile Java Networking & Messaging & Security
    Replies: 4
    Last Post: 2006-07-10, 06:34
  4. Problem with HttpConnection (URGENT)
    By bhandari in forum Mobile Java Networking & Messaging & Security
    Replies: 1
    Last Post: 2004-01-08, 23:39
  5. 7210 HttpConnection problem
    By joey829 in forum Mobile Java General
    Replies: 3
    Last Post: 1970-01-01, 02:00

Posting Permissions

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