public static String encodeString(String uri)
  {
    CharBuffer cb = CharBuffer.allocate();

    for (int i = 0; i < uri.length(); i++) {
      char ch = uri.charAt(i);

      switch (ch) {
      case '<':
        cb.append("&lt;");
        break;
      case '>':
        cb.append("&gt;");
        break;
      case '&':
        cb.append("&amp;");
        break;
      default:
        cb.append(ch);
      }
    }

    return cb.close();
  }
public static String encodeString ( String uri ) { CharBuffer cb = CharBuffer . allocate ( ) ; for ( int i = 0 ; i < uri . length ( ) ; i ++ ) { char ch = uri . charAt ( i ) ; switch ( ch ) { case '<' : cb . append ( "&lt;" ) ; break ; case '>' : cb . append ( "&gt;" ) ; break ; case '&' : cb . append ( "&amp;" ) ; break ; default : cb . append ( ch ) ; } } return cb . close ( ) ; }
Escape special characters.