Android-network interaction: client requests server resource follow-up

Source: Internet
Author: User

Yesterday, I tried to download server resources to the client through a network connection. However, I also found some problems, such as successfully downloading MP3, txt, and PNG files, but compressing files (RAR) but it cannot be decompressed. It was originally thought that this would happen for files not supported by the Android platform, but Android cannot open images in the same GIF format, however, it can be opened successfully after being downloaded. At the moment, no relevant information can be found, and I hope that Daniel can explain it.

When searching for relevant android network connection materials, we found many examples: some are stored as files, and some are directly read and displayed. If you are not familiar with it, it is really difficult, and it is easy to confuse and you don't know which one is better. After clarifying the basic concepts, I also tried to parse and display some text files (txt and XML) directly)

In the previous article, we talked about the link from the network data stream to a specific file (fileoutputstream). When we compare the file with the string, we will use another object bufferedreader (reading text from the character input stream, buffer each character to enable efficient reading of characters, arrays, and rows ).

Code

 Inputstream is = connect. getinputstream ();
Inputstreamreader ISR = new inputstreamreader (is, "GBK ");
Bufferedreader BR = new bufferedreader (ISR); // variable string
Stringbuffer sb = new stringbuffer ();
// Store one row of data (Br. Readline reads and writes one row at a time)
String Readline = NULL;
While (Readline = Br. Readline ())! = NULL ){
Log. V ("tag", "------------------" + sb. tostring ());
SB. append (Readline). append ("\ n ");
}
BR. Close ();
Is. Close ();
Connect. Disconnect ();
Showtext = new string (sb. tostring (). getbytes ("GBK"), "GBK ");

Here, we should pay attention to the encoding problem, which is why Chinese Characters in Android are garbled. I understand it as follows: Windows and Linux (or Java Virtual Machine) the Chinese character encoding is different. When you use GBK-Encoded chinese characters in Windows to another platform (another default Chinese character encoding method), garbled characters will appear, therefore, the encoding and resolution must be unified.

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.