When using WebView, if the parameters in the URL are in Chinese, the intercepted string will resemble this: http://api.letstar.cn/zq/news.html?id=20&cupName=%E6%B5%8B%E8%AF% 957, what if I want to use Cupname?
public static string Escape (string src) {int I;int J; StringBuffer tmp = new StringBuffer () tmp.ensurecapacity (Src.length () * 6); for (i = 0; i < src.length (); i++) {j = src. CharAt (i); if (J < n) {tmp.append ("%"), if (J < tmp.append) ("0"), Tmp.append (Integer.tostring (J, 16));} else {tmp.append ("%u"); Tmp.append (Integer.tostring (J, 16));}} return tmp.tostring ();}
String str= "k.a%c3%a4%c2%b8%c2%b4%c3%a6%c2%97%c2%b6%c3%a4%c2%be%c2%9b%c3%a5%c2%ba%c2%94%c3%a5%c2%95%c2%86";// Str=escape (Java.net.URLDecoder.decode (str, "Utf-8"))//system.out.println (Java.net.URLDecoder.decode (str, "Utf-8 ")); str= (Java.net.URLDecoder.decode (str," utf-8 ")); SYSTEM.OUT.PRINTLN (utf8togb2312 (str));
Analytical:
private static string utf8togb2312 (String str) {StringBuffer SB = new Stringbuff ER (); for (int i=0; i<str.length (); i++) {char c = str.charat (i); Switch (c) {case ' + ': sb.append ('); Break Case '% ': try {sb.append ((char) integer.parseint (str.substring ( I+1,I+3), 16)); } catch (NumberFormatException e) {throw new illegalargumentexception (); } i + = 2; Break Default:sb.append (c); Break }}//Undo conversion to external encoding String result = sb.tostring (); String Res=null; try{byte[] inputbytes = result.getbytes ("8859_1"); res= New String (inputbytes, "UTF-8"); } catch (Exception e) {} return res;}
JAVA JS escape function, parsing with JS encodeURI encoded string, UTF8 to gb2312 function