android读取网络xml乱码解决

packagecom.lolaage.tool;

importjava.io.FileInputStream;

importjava.io.InputStream;

importjava.net.HttpURLConnection;

importjava.net.URL;

importjava.text.ParseException;

importjava.text.SimpleDateFormat;

importjava.util.Date;

importjava.util.zip.GZIPInputStream;

importjavax.xml.parsers.DocumentBuilder;

importjavax.xml.parsers.DocumentBuilderFactory;

importorg.apache.http.Header;

importorg.apache.http.HttpEntity;

importorg.apache.http.HttpResponse;

importorg.apache.http.client.methods.HttpGet;

importorg.apache.http.client.methods.HttpUriRequest;

importorg.apache.http.impl.client.DefaultHttpClient;

importorg.w3c.dom.Document;

importorg.w3c.dom.NodeList;

importorg.xml.sax.InputSource;

importandroid.graphics.Bitmap;

importandroid.graphics.BitmapFactory;

importandroid.provider.ContactsContract.Data;

importandroid.text.format.DateFormat;

importandroid.text.format.DateUtils;

importandroid.util.Log;

importcom.lolaage.entity.WeatherInfo;

importcom.lolaage.entity.WeatherNextInfo;

publicclassWeatherUtil{

privatestaticStringUSER_AGENT="Mozilla/5.0(X11;U;Linuxx86_64;en-US;rv:1.9.2.11)Gecko/20101031GentooFirefox/3.6.11";

/**

*取得天气信息

*@paramurl

*/

publicstaticWeatherInfogetWeatherMes(Stringurl){

WeatherInfoweatherInfo=newWeatherInfo();

DefaultHttpClientclient=newDefaultHttpClient();

HttpUriRequestrequest=newHttpGet(url);

//设置请求头(用来解决网络获取数据乱码问题)

request.setHeader("User-Agent",USER_AGENT);

request.setHeader("Accept-Encoding","gzip,deflate");

HttpResponseresponse=null;

InputStreaminputStream=null;

try{

response=client.execute(request);

HttpEntityentity=response.getEntity();

inputStream=entity.getContent();

//判断返回的请求头(解决乱码)

Headerheader=response.getFirstHeader("Content-Encoding");

if(header!=null&&header.getValue().toLowerCase().indexOf("gzip")>-1){

inputStream=newGZIPInputStream(inputStream);

}

//解析

DocumentBuilderbuilder=DocumentBuilderFactory.newInstance().newDocumentBuilder();

Documentdocument=builder.parse(newInputSource(inputStream));

NodeListnodeList=document.getElementsByTagName("forecast_information");

//取得当天日期

Stringdate=nodeList.item(0).getChildNodes().item(4).getAttributes().item(0).getNodeValue();

NodeListnodeList2=document.getElementsByTagName("current_conditions");

//取得当天天气、湿度、图片、风速

Stringcondition=nodeList2.item(0).getChildNodes().item(0).getAttributes().item(0).getNodeValue();

Stringhumidity=nodeList2.item(0).getChildNodes().item(3).getAttributes().item(0).getNodeValue();

Stringicon=nodeList2.item(0).getChildNodes().item(4).getAttributes().item(0).getNodeValue();

Stringwind=nodeList2.item(0).getChildNodes().item(5).getAttributes().item(0).getNodeValue();

NodeListnodeList3=document.getElementsByTagName("forecast_conditions");

//取得当天的星期、温度

Stringweek=nodeList3.item(0).getChildNodes().item(0).getAttributes().item(0).getNodeValue();

Stringlow=nodeList3.item(0).getChildNodes().item(1).getAttributes().item(0).getNodeValue();

Stringhigh=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;

WeatherNextInfonextInfo;

//取得今后3天的天气情况

for(inti=1;i<nodeList3.getLength();i++){

nextInfo=newWeatherNextInfo();

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(Exceptione){

e.printStackTrace();

}finally{

try{

if(inputStream!=null){

inputStream.close();

}

}catch(Exceptione2){

e2.printStackTrace();

}

}

returnweatherInfo;

}

/**

*日期格式化

*@paramstr

*@return

*@throwsParseException

*/

publicstaticStringdateFormat(Stringstr){

Datedate=null;

SimpleDateFormatdf=newSimpleDateFormat("yyyy-mm-dd");

if(str!=null&&!"".equals(str)){

try{

date=df.parse(str);

}catch(ParseExceptione){

e.printStackTrace();

}

}

SimpleDateFormatsdf=newSimpleDateFormat("yyyy年mm月dd日");

returnsdf.format(date);

}

/**

*

*@paramstr

*@return

*/

publicstaticStringweekFormat(Stringstr){

Stringweek="";

if(str!=null&&!"".equals(str)){

if(str.startsWith("周")){

week=str.replaceFirst("周","星期");

}

}

returnweek;

}

/**

*返回天气图标

*@paramiconUrl

*@return

*/

publicstaticBitmapreturnIcon(StringiconUrl){

URLimgUrl=null;

Bitmapbp=null;

HttpURLConnectionconnection=null;

InputStreaminputStream=null;

try{

imgUrl=newURL(iconUrl);

connection=(HttpURLConnection)imgUrl.openConnection();

connection.setDoInput(true);

connection.connect();

inputStream=connection.getInputStream();

bp=BitmapFactory.decodeStream(inputStream);

}catch(Exceptione){

e.printStackTrace();

}finally{

try{

if(inputStream!=null||connection!=null){

inputStream.close();

connection.disconnect();

}

}catch(Exceptione2){

e2.printStackTrace();

}

}

returnbp;

}

}

相关推荐