JAVA中的http請求接收數(shù)據(jù)處理編碼問題URLEncode編碼和URLDecoder解碼運用方法
近期在搞直接調(diào)用接口,碰到一個URLEncode編碼問題,Json中含有URLEncode編碼過的數(shù)據(jù),
為防止HTML標簽亂碼,在解析的時候需要進行解碼
注:面向對象編程不需要此方式轉換
URLEncode主要是把一些特殊字符轉換成轉移字符,比如:&要轉換成&這樣的。
URLEncode編碼轉換方式:
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ù)的轉換:
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請求處理編碼URLEncode