A time ago wrote a from Shun Fung to obtain the information of the program, through the httpurlconnection to get data garbled, clearly intercepted data write utf-8 or garbled, and finally found that the reason is no data decompression, blocking:
Finally, the problem is found, the data transfer appears to be compressed first, it is estimated that the transfer efficiency bar.
Then unzip it first:
1 /**2 * This is a static method, the Data tool method, incoming input stream in, and the data length, you can get the extracted result byte array3 * @paramin an input stream, which can be obtained by getInputStream () of the HttpURLConnection object4 * @paramlength data is mainly used to create a just byte array to hold the resulting data, can be HttpURLConnection object Getheaderfield ("Content-length") to obtain5 */6 7 Public Static byte[] GetInt (InputStream in,intlength) {8 byte[] by =New byte[length];9 Ten Try { OneGzipinputstream Giz =NewGzipinputstream (in); AGiz.read (by, 0, length); -}Catch(IOException e) { - //TODO auto-generated Catch block the e.printstacktrace (); - } - returnby ; -}
After extracting the byte[] array data, directly through the
string result = new String (by, "Utf-8");
We can get results.
which
By: Get the extracted array
Utf-8: This is the type of encoding you can see from the Intercept.
"Note--java" about httpurlconnection getting data Chinese garbled problem