XML解析实例--------获得天气预报数据

[color=olive][size=xx-large]XML解析实例--------获得天气预报数据[/size][/color]

运行效果图:
[img]
[img]http://dl.iteye.com/upload/attachment/0066/5172/e6852e38-d003-3710-a23c-89f3fdb987d8.jpg[/img]
[/img]

工程结构图:
[img]
[img]http://dl.iteye.com/upload/attachment/0066/5174/5502f506-c33d-3191-b713-59f5c8178d9c.jpg[/img]
[/img]

一、MainActivity:
package com.amaker.flipper;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.http.HttpEntity;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*setContentView(R.layout.main);
String str = getWeatherAsString();
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG).show();*/
setListAdapter(new MyAdpter(MainActivity.this));
}

private static class MyAdpter extends BaseAdapter {
List list;
LayoutInflater inflater;
public MyAdpter(Context context) {
list = readXml();
inflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
return list.size();
}

@Override
public Object getItem(int arg0) {
return null;
}

@Override
public long getItemId(int arg0) {
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
InputStream in = null;
Weather w = list.get(position);
String icon = w.getIcon();
String iconUrl = "http://www.google.com"+icon;
try {
URL url = new URL(iconUrl);
try {
in = url.openStream();
} catch (IOException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.icon = (ImageView) convertView
.findViewById(R.id.icon_imageView1);
holder.week_tv = (TextView) convertView
.findViewById(R.id.week_textView1);
holder.condition_tv = (TextView) convertView
.findViewById(R.id.condition_textView2);
holder.low_tv = (TextView) convertView
.findViewById(R.id.low_textView3);
holder.high_tv = (TextView) convertView
.findViewById(R.id.high_textView4);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.icon.setImageBitmap(BitmapFactory.decodeStream(in));
holder.week_tv.setText(w.getWeek());
holder.condition_tv.setText(w.getCondition());
holder.low_tv.setText(w.getLow()+"");
holder.high_tv.setText(w.getHight()+"");
return convertView;

}

static class ViewHolder {
ImageView icon;
TextView week_tv;
TextView condition_tv;
TextView low_tv;
TextView high_tv;
}

}

static List readXml() {
SAXParserFactory factory = SAXParserFactory.newInstance();
try {
SAXParser parser = factory.newSAXParser();
InputSource is = null;
is = new InputSource(new StringReader(getWeatherAsString()));
MyHandler dh = new MyHandler();
try {
parser.parse(is, dh);
return dh.list();
} catch (IOException e) {
e.printStackTrace();
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
return null;
}

// 得到天气情况的xml
public static String getWeatherAsString() {
String strUrl = "http://www.google.com/ig/api?hl=zh-cn&weather=Beijing";
HttpGet getRequest = new HttpGet(strUrl);
DefaultHttpClient client = new DefaultHttpClient();
try {
HttpResponse response = client.execute(getRequest);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}



二、MyHandler
package com.amaker.flipper;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class MyHandler extends DefaultHandler {
List list;
Weather currentWeather;

public List list(){
return list;
}
@Override
public void startDocument() throws SAXException {
super.startDocument();
list = new ArrayList();
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, attributes);
if(qName!=null&&qName.equals("forecast_conditions")){
currentWeather = new Weather();
}
if(qName!=null&&qName.equals("day_of_week")){
String week = attributes.getValue("data");
if(currentWeather!=null){
currentWeather.setWeek(week);
}

}
if(qName!=null&&qName.equals("low")){
int low = Integer.parseInt(attributes.getValue("data"));
if(currentWeather!=null){
currentWeather.setLow(low);
}

}
if(qName!=null&&qName.equals("high")){
int hight = Integer.parseInt(attributes.getValue("data"));
if(currentWeather!=null){
currentWeather.setHight(hight);
}

}
if(qName!=null&&qName.equals("icon")){
String icon = attributes.getValue("data");
if(currentWeather!=null){
currentWeather.setIcon(icon);
}

}
if(qName!=null&&qName.equals("condition")){
String condition = attributes.getValue("data");
if(currentWeather!=null){
currentWeather.setCondition(condition);
}


}

}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
super.characters(ch, start, length);
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
super.endElement(uri, localName, qName);
if(qName!=null&&qName.equals("forecast_conditions")){
list.add(currentWeather);
}
}
@Override
public void endDocument() throws SAXException {
super.endDocument();
}
}



三、Weather:
package com.amaker.flipper;

public class Weather {
private String week;
private int low;
private int hight;
private String icon;
private String condition;



@Override
public String toString() {
return "Weather [condition=" + condition + ", hight=" + hight
+ ", icon=" + icon + ", low=" + low + ", week=" + week + "]";
}
public String getWeek() {
return week;
}
public void setWeek(String week) {
this.week = week;
}
public int getLow() {
return low;
}
public void setLow(int low) {
this.low = low;
}
public int getHight() {
return hight;
}
public void setHight(int hight) {
this.hight = hight;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public String getCondition() {
return condition;
}
public void setCondition(String condition) {
this.condition = condition;
}


}


四、list_item:

xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:layout_height="wrap_content"
android:id="@+id/icon_imageView1"
android:src="@drawable/icon"
android:layout_width="wrap_content" />
android:text=""
android:id="@+id/week_textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:text=""
android:id="@+id/condition_textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:text=""
android:id="@+id/low_textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:text=""
android:id="@+id/high_textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />



记得添加权限:

你可能感兴趣的:(android)