JAVA中的http請(qǐng)求接收數(shù)據(jù)處理編碼問題URLEncode編碼和URLDecoder解碼運(yùn)用方法
近期在搞直接調(diào)用接口,碰到一個(gè)URLEncode編碼問題,Json中含有URLEncode編碼過的數(shù)據(jù),
為防止HTML標(biāo)簽亂碼,在解析的時(shí)候需要進(jìn)行解碼
注:面向?qū)ο缶幊滩恍枰朔绞睫D(zhuǎn)換
URLEncode主要是把一些特殊字符轉(zhuǎn)換成轉(zhuǎn)移字符,比如:&要轉(zhuǎn)換成&這樣的。
URLEncode編碼轉(zhuǎn)換方式:
public static String toURLEncoded(String paramString) { if (paramString == null || paramString.equals("")) { LogD("toURLEncoded error:" paramString); return ""; } try { String str = new String(paramString.getBytes(), "UTF-8"); str = URLEncoder.encode(str, "UTF-8"); return str; } catch (Exception localException) { LogE("toURLEncoded error:" paramString, localException); } return ""; }
URLDecoder.decode解碼返回的參數(shù)的轉(zhuǎn)換:
public static String toURLDecoded(String paramString) { if (paramString == null || paramString.equals("")) { LogD("toURLDecoded error:" paramString); return ""; } try { String str = new String(paramString.getBytes(), "UTF-8"); str = URLDecoder.decode(str, "UTF-8"); return str; } catch (Exception localException) { LogE("toURLDecoded error:" paramString, localException); } return ""; }
原文鏈接:JAVA中的http請(qǐng)求處理編碼URLEncode