본문 바로가기

Android

URLEncoder, Decoder

url로 직접 접속하다 보면 특정 브라우저 등에서 파라미터에 붙은 특수문자, 공백 등을 자동으로 변환해주지 않아 접속이 불가할 때가 있다.

아래 URLEncoder와 URLDecoder를 사용하여 인코딩과 디코딩하면 된다.

안드로이드-서버간에 특수문자와 공백은 잘 인코딩/디코딩했으나 한글은 euc-kr과 utf-8 두 방법 모두 깨졌다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public String getURLEncode(String content){
        try {
            return URLEncoder.encode(content, "utf-8");   // UTF-8
           // return URLEncoder.encode(content, "euc-kr");  // EUC-KR
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
 
    public String getURLDecode(String content){
        try {
            return URLDecoder.decode(content, "utf-8");   // UTF-8
            //return URLDecoder.decode(content, "euc-kr");  // EUC-KR
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
cs


한글은 다음 참조: http://kutar37.tistory.com/81