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)換


發(fā)送給服務(wù)端的請(qǐng)求中的參數(shù)值,如果含有特殊符號(hào),需要是做URLEncode,服務(wù)端才可以正常解析,否則可能會(huì)出錯(cuò)。
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