In the case of weak network transmission, how to save traffic (interview small problem, Android article)

Source: Internet
Author: User

Immediately graduated, on the occasion of graduation, I quit the previous internship work, mainly work content is not very interested. Recently looking for a job, mainly looking for Java and Android work. I think I learned well, but the interview was frustrating. Let me mention some of the questions you asked.

The first: In the case of weak network transmission, how to save traffic?

Due to the fact that there is not much contact with Android, the problem is naturally not very pleasant to answer.

I answer this: compress some data, such as passing a string, first converting the string to a character array, and then compressing the character array. Then there are a lot of transmission objects (usually json,xml this way too cumbersome), extract the public parts, and then only transfer the different parts of the past (I think, how to know is the same ...) )

I think the answer is not very good, the interviewer did not say much.

Finish the test, search the information on the Internet, add their own understanding, finishing the next.

Let's start with how Android interacts with the server.

1. Socket (TCP/UDP)

2, NiO, and the derived NIO framework such as Netty

3, WebService (specifically, the SOAP protocol to transmit data using XML)

4, JSON (special HTTP protocol based on the rest-style transfer mode)

Here, when it comes to data compression, I think it means to compress and transfer the JSON first.

The most intuitive way to solve the problem of large data is to compress the data. The server will need to pass the data first compressed, and then sent to the Android client, the Android client receives the compressed data, unzip it, get the data before compression.

The following figure should illustrate the situation.


Reference http://www.cnblogs.com/lykbk/archive/2013/08/15/3259045.html


Then there is a problem with some compression algorithms. How do you choose the compression algorithm? Here is a compression algorithm.

Reference http://www.cnblogs.com/answer1991/archive/2012/05/07/2487052.html

packagecom.chenjun.utils.compress; Importjava.io.bytearrayinputstream;importjava.io.bytearrayoutputstream;importjava.io.inputstream; Importjava.io.outputstream;importjava.util.zip.gzipinputstream;importjava.util.zip.gzipoutputstream;          Publicclass Compress {private static final int buffer_length = 400;          The minimum length of the compressed byte, the byte array smaller than this length is not suitable for compression, and will be larger after compression public static final int byte_min_length = 50;    Whether the byte array compresses the flag bit public static final byteflag_gbk_string_uncompressed_bytearray = 0;    public static Final byteflag_gbk_string_compressed_bytearray = 1;    public static Final byteflag_utf8_string_compressed_bytearray = 2;       public static Final byteflag_no_update_info = 3; /** * Data Compression * * @param is * @param os * @throws Exception */public static void compress            (InputStream is,outputstream os) throws Exception {Gzipoutputstream Gos = newgzipoutputstream (OS);          int count; byte data[] = Newbyte[buffer_length];         while ((count = Is.read (data, 0,buffer_length))! =-1) {gos.write (data, 0, count);           } gos.finish ();          Gos.flush ();      Gos.close ();  /** * Data Uncompressed * * @param is * @param os * @throws Exception */public static void Decompress (Inputstreamis, OutputStream os) throws Exception {Gzipinputstream GIS = Newgzipin           Putstream (IS);          int count;          byte data[] = Newbyte[buffer_length];          while ((count = Gis.read (data, 0,buffer_length))! =-1) {os.write (data, 0, count);      } gis.close (); }/** * Data compression * * * * * * * * * * * * * * * @return * @throws Exception */public static byte[] Byte         Compress (byte[]data) throws Exception {Bytearrayinputstream Bais = newbytearrayinputstream (data);          Bytearrayoutputstream BAOs = Newbytearrayoutputstream (); Compression compress (Bais, BAOs);          byte[] Output =baos.tobytearray ();         Baos.flush ();          Baos.close ();          Bais.close ();     return output; /** * Data Uncompressed * * * * * * * * * * * * * * * * * @return * @throws Exception */public static byte[]         Bytedecompress (byte[]data) throws Exception {Bytearrayinputstream Bais = newbytearrayinputstream (data);          Bytearrayoutputstream BAOs = Newbytearrayoutputstream ();          Decompression decompress (Bais, BAOs);          data = Baos.tobytearray ();         Baos.flush ();          Baos.close ();          Bais.close ();     return data; } }
I am currently looking for a job. email [email protected]. This advertisement long-term effective ..... A joke.

Related Article

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.