Because the character encoding issues, causing me to toss two hours. Here is the code, we look to understand.
String url = "http://www.google.com/ig/api?weather=,,,31174165,121433841";
DefaultHttpClient client = new DefaultHttpClient();
HttpUriRequest req = new HttpGet(url);
HttpResponse resp = client.execute(req);
HttpEntity ent = resp.getEntity();
int status = resp.getStatusLine().getStatusCode();
if(L.Debug) {
L.d("" + status);
}
// 若状态码为 200 ,则表示OK
if (status == HttpStatus.SC_OK) {
//String result = EntityUtils.toString(ent);
if(L.Debug) {
//L.d(result);
}
InputStream stream = ent.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(stream, "utf-8"));
DocumentBuilder b = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
String xmlString = "";
for(String temp = br.readLine(); temp != null;xmlString += temp ,temp = br.readLine());
// 去除字符串中的换行符,制表符,回车符。
xmlString = xmlString.replaceAll("/n|/t|/r", "");
InputStream stream2 = new ByteArrayInputStream(xmlString.getBytes());
Document d = b.parse(new InputSource(stream2));
NodeList n = d.getElementsByTagName("forecast_conditions");
String tips = "";
tips += n.item(0).getChildNodes().item(0).getAttributes()
.item(0).getNodeValue();
if(L.Debug) {
L.d("" + tips );
L.d("" + n.item(0).getChildNodes().getLength());
//L.Toast(tips, this);
}
br.close();
stream.close();
stream2.close();
}
The key is this line of code
BufferedReader br = new BufferedReader(new InputStreamReader(stream, "utf-8"));
The received stream into the buffer, the encoding process into a utf-8 (if or distortion, or that Instead gb2312 GBK)
Method Two
String url = "http://www.google.com/ig/api?weather=,,,31174165,121433841";
DefaultHttpClient client = new DefaultHttpClient();
HttpUriRequest req = new HttpGet(url);
HttpResponse resp = client.execute(req);
HttpEntity ent = resp.getEntity();
int status = resp.getStatusLine().getStatusCode();
// If the status is equal to 200 ,that is OK
if (status == HttpStatus.SC_OK) {
String result = EntityUtils.toString(ent);
if(L.Debug) {
L.testCharset(result);
}
// Encode utf-8 to iso-8859-1
result = new String(result.getBytes("ISO-8859-1"), "UTF-8");
DocumentBuilder b = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
String xmlString = result;
// Remove the newline,tabs and return characters from string
xmlString = xmlString.replaceAll("/n|/t|/r", "");
InputStream stream = new ByteArrayInputStream(xmlString.getBytes());
Document d = b.parse(new InputSource(stream));
getWeatherBean(d);
stream.close();
}
// 以下是测试字符编码的
public static void testCharset(String datastr){
try {
String temp = new String(datastr.getBytes(), "GBK");
Log.v("TestCharset","****** getBytes() -> GBK ******/n"+temp);
temp = new String(datastr.getBytes("GBK"), "UTF-8");
Log.v("TestCharset","****** GBK -> UTF-8 *******/n"+temp);
temp = new String(datastr.getBytes("GBK"), "ISO-8859-1");
Log.v("TestCharset","****** GBK -> ISO-8859-1 *******/n"+temp);
temp = new String(datastr.getBytes("ISO-8859-1"), "UTF-8");
Log.v("TestCharset","****** ISO-8859-1 -> UTF-8 *******/n"+temp);
temp = new String(datastr.getBytes("ISO-8859-1"), "GBK");
Log.v("TestCharset","****** ISO-8859-1 -> GBK *******/n"+temp);
temp = new String(datastr.getBytes("UTF-8"), "GBK");
Log.v("TestCharset","****** UTF-8 -> GBK *******/n"+temp);
temp = new String(datastr.getBytes("UTF-8"), "ISO-8859-1");
Log.v("TestCharset","****** UTF-8 -> ISO-8859-1 *******/n"+temp);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
TestCharset first with this method, test coding which no distortion occurs, and then modify result = new String (result.getBytes ( "ISO-8859-1"), "UTF-8"); inside the corresponding coding.
Reproduced in: https: //my.oschina.net/lendylongli/blog/226779