Java ME URL Encoder

Di java SE kita bisa meng-encode url, misalnya dengan:

URLEncoder.encode("http://blahblahblahSembarangNamaURL.com/abc def/ghi jkl.php","UTF-8");

di Java ME kelas UrlEncoder, tidak ada, tapi kita bisa membuatnya juga, seperti ini (default character encodingnya UTF-8):

public class URLEncoder{
    public static String encode(String s){
        StringBuffer sbuf = new StringBuffer();
        int ch;
        for (int i = 0; i < s.length(); i++)
        {
            ch = s.charAt(i);
            switch(ch)
            {
               case ' ': { sbuf.append("+"); break;}
               case '!': { sbuf.append("%21"); break;}
               case '*': { sbuf.append("%2A"); break;}
               case '\'': { sbuf.append("%27"); break;}
               case '(': { sbuf.append("%28"); break;}
               case ')': { sbuf.append("%29"); break;}
               case ';': { sbuf.append("%3B"); break;}
               case ':': { sbuf.append("%3A"); break;}
               case '@': { sbuf.append("%40"); break;}
               case '&': { sbuf.append("%26"); break;}
               case '=': { sbuf.append("%3D"); break;}
               case '+': { sbuf.append("%2B"); break;}
               case '$': { sbuf.append("%24"); break;}
               case ',': { sbuf.append("%2C"); break;}
               case '/': { sbuf.append("%2F"); break;}
               case '?': { sbuf.append("%3F"); break;}
               case '%': { sbuf.append("%25"); break;}
               case '#': { sbuf.append("%23"); break;}
               case '[': { sbuf.append("%5B"); break;}
               case ']': { sbuf.append("%5D"); break;}
               default: sbuf.append((char)ch);
            }
        }
        return sbuf.toString();
    }
}

2 thoughts on “Java ME URL Encoder

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s