Micro-Credit Public account development tutorial (vi) Content length limitations of text messages uncover

Source: Internet
Author: User
Tags stringbuffer

I believe many friends have encountered such a problem: when the text message content is too long, the micro-letter will not do any response. So what is the maximum length of the text message allowed by the micro-letter? How do we calculate the length of the text? Why do some people react to micro-letters as if the maximum length of text messages supported is more than 1300? This article will completely remove everyone's doubts.

The message length is limited to 2048 in the interface document

As you can see, the interface document is written very clearly: the message content of the reply is not longer than 2048 bytes. So why do a lot of people test that the response message content is over 1300 bytes long, and the micro-letter doesn't respond? I think that's the problem in this part of the people who don't figure out exactly how to calculate the number of bytes of text.

How to correctly calculate the number of bytes of text

Calculating the number of bytes in a literal (string), the first thing you should think of is the GetBytes () method of the String class, which returns a byte array of strings, and then computes the length of the array to get the number of bytes in the string. For example:

public static void Main (String []args)  {  
    //Run Result: 4  
    System.out.println ("Liu Feng". GetBytes (). length);  

In the above example, the number of bytes in two Chinese was calculated as 4, which means that one Chinese character occupies 2 bytes. Is that really the case? In fact, we have overlooked a problem: for different encoding methods, Chinese accounted for the number of bytes is not the same! What the hell is going on? In the above example, we do not specify the encoding method, then the operating system will use the default encoding. Let's look at the three conclusions I have drawn:

1 If the above example runs on the operating system platform with the default encoding of iso8859-1, the result is 2;

2 If the above example runs on the operating system platform with the default encoding of gb2312 or GBK, the result is 4;

3 If the above example runs on the operating system platform with the default encoding of Utf-8, the result is 6;

If so, does it mean that the String.getbytes () method defaults to gb2312 or GBK encoding on our system platform? Let's look at one more example:

Public 

static void main (String []args) throws Unsupportedencodingexception  {  
    //Run Result: 2  
    System.out.println ("Liu Feng". GetBytes ("iso8859-1"). length);  
    Run Result: 4  
    System.out.println ("Liu Feng". GetBytes ("GB2312"). length);  
    Run Result: 4  
    System.out.println ("Liu Feng". GetBytes ("GBK"). length);  
    Run Result: 6  
    System.out.println ("Liu Feng". GetBytes ("UTF-8"). length);  

Is this example a good proof of the three conclusions I have given? In other words, when the iso8859-1 encoding is used, a medium/English is only one byte; when the GB2312 or GBK encoding is used, one Chinese occupies two bytes, while the UTF-8 encoding is used, one Chinese occupies three bytes.

The encoding method used in micro-platform and the calculation of the number of bytes in the string

So what is the encoding for returning messages to a micro-trust server? Of course is UTF-8, because we have adopted the following code in the Dopost method to avoid Chinese garbled:

The request, the response encoding are set to UTF-8 (prevent Chinese garbled)  
request.setcharacterencoding ("UTF-8");  
Response.setcharacterencoding ("UTF-8");

To verify what I said, I wrote an example to test:

private static String getmsgcontent () {StringBuffer buffer = new StringBuffer ();

70 Chinese characters per line, a total of 682 Chinese characters plus 1 exclamation points in English buffer.append ("Don't know when to start liking here every night will come here to see you, you look so beautiful you tell me I can't see you *  
    Can't see you I'm lost I want to hold your hand through the ups and downs of any difficulties I will accompany you ");  
    Buffer.append ("Don't know when to begin to like here every night will come here to see you how beautiful you look so I can not see you can not see you I lost their good want to hold your hand through the ups and downs of any difficulties I will accompany you");  
    Buffer.append ("Don't know when to begin to like here every night will come here to see you how beautiful you look so I can not see you can not see you I lost their good want to hold your hand through the ups and downs of any difficulties I will accompany you");  
    Buffer.append ("Don't know when to begin to like here every night will come here to see you how beautiful you look so I can not see you can not see you I lost their good want to hold your hand through the ups and downs of any difficulties I will accompany you");  
    Buffer.append ("Don't know when to begin to like here every night will come here to see you how beautiful you look so I can not see you can not see you I lost their good want to hold your hand through the ups and downs of any difficulties I will accompany you");  
    Buffer.append ("Don't know when to begin to like here every night will come here to see you how beautiful you look so I can not see you can not see you I lost their good want to hold your hand through the ups and downs of any difficulties I will accompany you");  
    Buffer.append ("Don't know when to begin to like here every night will come here to see you how beautiful you look so I can not see you can not see you I lost their good want to hold your hand through the ups and downs of any difficulties I will accompany you");  
    Buffer.append ("Don't know when to begin to like here every night will come here to see you how beautiful you look so I can not see you can not see you I lost their good want to hold your hand through the ups and downs of any difficulties I will accompany you");  
  Buffer.append ("Don't know when to begin to like here every night will come here to see you how beautiful you look so I can not see you can not see you I lost their good want to hold your hand through the ups and downs of any difficulties I will accompany you");  Buffer.append ("Don't know when to begin to like here every night will come here to see you how beautiful you look so I can not see you can not see you I have lost their good want to lead!");  
return buffer.tostring (); The public static void main (String []args) throws Exception {//is 1365 bytes when used gb2312 encoding System.out.prin  
    TLN (Getmsgcontent (). GetBytes ("gb2312"). length);  
The UTF-8 encoding is used to account for 2047 bytes System.out.println (getmsgcontent (). GetBytes ("Utf-8"). length); }

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.