android读取网络xml乱码解决

package com.lolaage.tool;

import java.io.FileInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.zip.GZIPInputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.ContactsContract.Data;
import android.text.format.DateFormat;
import android.text.format.DateUtils;
import android.util.Log;

import com.lolaage.entity.WeatherInfo;
import com.lolaage.entity.WeatherNextInfo;

public class WeatherUtil {

private static String USER_AGENT = "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.11) Gecko/20101031 Gentoo Firefox/3.6.11";
/**
* 取得天气信息
* @param url
*/
public static WeatherInfo getWeatherMes(String url){
WeatherInfo weatherInfo = new WeatherInfo();
DefaultHttpClient client = new DefaultHttpClient();
HttpUriRequest request = new HttpGet(url);
//设置请求头(用来解决网络获取数据乱码问题)
request.setHeader("User-Agent",USER_AGENT);
request.setHeader("Accept-Encoding", "gzip,deflate");

HttpResponse response = null;
InputStream inputStream = null;
try {
response = client.execute(request);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();

//判断返回的请求头(解决乱码)
Header header = response.getFirstHeader("Content-Encoding");
if(header != null && header.getValue().toLowerCase().indexOf("gzip") > -1){
inputStream = new GZIPInputStream(inputStream);
}
//解析
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = builder.parse(new InputSource(inputStream));
NodeList nodeList = document.getElementsByTagName("forecast_information");
//取得当天日期
String date = nodeList.item(0).getChildNodes().item(4).getAttributes().item(0).getNodeValue();
NodeList nodeList2 = document.getElementsByTagName("current_conditions");
//取得当天天气、湿度、图片、风速
String condition = nodeList2.item(0).getChildNodes().item(0).getAttributes().item(0).getNodeValue();
String humidity = nodeList2.item(0).getChildNodes().item(3).getAttributes().item(0).getNodeValue();
String icon = nodeList2.item(0).getChildNodes().item(4).getAttributes().item(0).getNodeValue();
String wind = nodeList2.item(0).getChildNodes().item(5).getAttributes().item(0).getNodeValue();

NodeList nodeList3 = document.getElementsByTagName("forecast_conditions");
//取得当天的星期、温度
String week = nodeList3.item(0).getChildNodes().item(0).getAttributes().item(0).getNodeValue();
String low = nodeList3.item(0).getChildNodes().item(1).getAttributes().item(0).getNodeValue();
String high = nodeList3.item(0).getChildNodes().item(2).getAttributes().item(0).getNodeValue();
weatherInfo.date = date;
weatherInfo.condition = condition;
weatherInfo.humidity = humidity;
weatherInfo.icon = icon;
weatherInfo.wind = wind;
weatherInfo.week = week;
weatherInfo.highTemp = high;
weatherInfo.lowTemp = low;

WeatherNextInfo nextInfo;
//取得今后3天的天气情况
for (int i = 1; i < nodeList3.getLength(); i++) {
nextInfo = new WeatherNextInfo();
nextInfo.week = nodeList3.item(i).getChildNodes().item(0).getAttributes().item(0).getNodeValue();
nextInfo.low = nodeList3.item(i).getChildNodes().item(1).getAttributes().item(0).getNodeValue();
nextInfo.high = nodeList3.item(i).getChildNodes().item(2).getAttributes().item(0).getNodeValue();
nextInfo.icon = nodeList3.item(i).getChildNodes().item(3).getAttributes().item(0).getNodeValue();
nextInfo.condition = nodeList3.item(i).getChildNodes().item(4).getAttributes().item(0).getNodeValue();
weatherInfo.nextWeatherList.add(nextInfo);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(inputStream != null){
inputStream.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return weatherInfo;
}

/**
* 日期格式化
* @param str
* @return
* @throws ParseException
*/
public static String dateFormat(String str){
Date date = null;
SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");
if(str != null && !"".equals(str)){
try {
date = df.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年mm月dd日");
return sdf.format(date);
}
/**
*
* @param str
* @return
*/
public static String weekFormat(String str){
String week = "";
if(str != null && !"".equals(str)){
if(str.startsWith("周")){
week = str.replaceFirst("周", "星期");
}
}
return week;
}
/**
* 返回天气图标
* @param iconUrl
* @return
*/
public static Bitmap returnIcon(String iconUrl){
URL imgUrl = null;
Bitmap bp = null;
HttpURLConnection connection = null;
InputStream inputStream = null;
try {
imgUrl = new URL(iconUrl);
connection = (HttpURLConnection) imgUrl.openConnection();
connection.setDoInput(true);
connection.connect();
inputStream = connection.getInputStream();
bp = BitmapFactory.decodeStream(inputStream);
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(inputStream != null || connection != null){
inputStream.close();
connection.disconnect();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return bp;
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值