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.