Questions about Java.net.URLEncoder.encode () and Java.net.URLEncoder.encode () in the JDK in Android.jar

Source: Internet
Author: User

Problem: Recently done in the project, because to use the socket, so it is inevitable that the code to "GBK", but found in the use of Android.jar in the Java.net.URLEncoder.encode ("Kanji", "GBK") When coding, it is found to lose half of the last character, such as "Min" code became "%C3" instead of the correct "%c3%d6", and then in another Java project demo, found Java.net.URLEncoder.encode ("Kanji", "GBK") Without this problem, it began to be depressed ...  try: Try 1: Find the JDK in the Java.net.URLEncoder class implementation and Android.jar is completely different, guess whether the underlying implementation is not the same cause the above problem, Then I try to copy the Java.net.URLEncoder class from the JDK into my project and find that the problem is still, and this idea fails!  try 2: by discovering that it may be related to different platforms, my project uses the Android network box, 4.4.2, Third-party manufacturer OEM system, that other Android platform can run correctly?          start trying, Using another small Android project for Java.net.URLEncoder.encode to do a small demo apk, run on another Android phone, found to be able to run correctly, can get the correct results. Good platform-agnostic? The original is really related to the platform!!   Problem reason: by discovering the GetBytes () that seems to be a string, the System.arrayscopy () is used in the native method to intercept the byte of a char (specifically, not too deep, and so on, the project is urgent).   Solution: Fortunately, the transmission of data in this project only urlencode the Chinese characters, so, some tools will need to transfer the data in the Chinese characters through the regular pick out, and then the Chinese characters in the use of the string after the Chinese character only a string, such as: "1", and then removed as long as the Chinese part of the code (this code is also a tool method, just converts a byte array into a 16-character string, and then the encoding of each Chinese characters separated by a space, and then take the process of using string.spit () the encoding of each Chinese character string[]), Then the original string of the Chinese character and the encoding after the character string to replace, this Shang method to complete the .  code:
1  2 Static FinalString regEx = "[\\u4e00-\\u9fa5]+";3  4 /**urlencoding GBK encoding of the Chinese characters in values in the JSON string with data content as string*/5  Public Staticstring Encodingsocketdata (String socketdata) {6 7    ///Judging If there is no Chinese8     if((! Textutils.isempty (Socketdata)) && (Socketdata.getbytes (). length! =socketdata.length ())) {9arraylist<string> list =NewArraylist<string>();TenPattern p =Pattern.compile (regEx); OneMatcher m =P.matcher (socketdata); A        while(M.find ()) { -List.add (M.group (0)); -       } the      for(intI=0;i<list.size (); i++){ -String Oldstr =List.get (i); -String Newstr =encoding (OLDSTR); -Socketdata =Socketdata.replacefirst (Oldstr, newstr); +     } -     } + returnSocketdata; A  at } - /**The main processing of characters in the string encoding, data must be a Chinese character or a number of Chinese characters connected to the string form*/ - Staticstring encoding (String data) { -     if(Textutils.isempty (data)) { -       returndata; -}Else{ inKTLOG.E ("data>>> before >>" +data);//Test -     intSize =data.length (); toString temp =NULL; +     byte[] Arrays =NULL; -     Try { thearrays = (data+ "1"). GetBytes (CCSParams.SocketParams.SOCKET_DATA_PARSE_FORMAT); *string[] Strarrays = socketutils.bytearray2hexstr (Arrays,true). Split (""); $StringBuffer STRB =NewStringBuffer ();Panax Notoginseng      for(inti=0;i<size*2;i+=2){ -Strb.append ('% '). Append (Strarrays[i]). Append ('% '). Append (strarrays[i+1]); the     } +temp =strb.tostring (); A}Catch(unsupportedencodingexception e) { theKTLOG.E ("Encoding Execution (" +data+ ") exception", e); +     } -     returntemp; $     } $ } -   -  Public StaticString Bytearray2hexstr (byte[] Barray,Booleanformat) { theStringBuffer STRB =NULL; -     if(NULL!=Barray) {WuyiSTRB =NewStringBuffer (barray.length); the      for(inti = 0; i < barray.length; i++) { -String str = integer.tohexstring (0xFF &barray[i]). Trim (); Wu       if(Str.length () < 2) { -Strb.append ("0"); About       } $       strb.append (str); -       if(format) { -Strb.append (""); -      } A     } +}Else { the return NULL; -     } $     returnstrb.tostring (). Trim (); the}

Questions about Java.net.URLEncoder.encode () in Android.jar and Java.net.URLEncoder.encode () in the JDK

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.