Android httpclient接收xml乱码问题解决方法

因为字符编码的问题,害我折腾了两个小时。以下是代码,大家看一下就明白了。

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();
		}
 

关键是这一行代码

BufferedReader br =  new BufferedReader(new InputStreamReader(stream, "utf-8"));
 

将接收的流放入buffer里,处理成utf-8的编码(如果还是乱码,那请改为gb2312或者gbk)

方法二

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这个方法,测试哪一种编码没有出现乱码,再修改result = new String(result.getBytes("ISO-8859-1"), "UTF-8");里面对应的编码。

转载于:https://my.oschina.net/lendylongli/blog/226779

猜你喜欢

转载自blog.csdn.net/weixin_33937499/article/details/92576537
今日推荐