当前位置: 代码迷 >> Android >> android天气预报 sax解析报错,该如何处理
  详细解决方案

android天气预报 sax解析报错,该如何处理

热度:73   发布时间:2016-05-01 13:06:36.0
android天气预报 sax解析报错

天气预报API地址:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName?theCityName=beijing

<?xml version="1.0" encoding="utf-8" ?> - <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://WebXml.com.cn/">
<string>直辖市</string> 

<string>北京</string> 

<string>54511</string> 

。。。。。

</ArrayOfString>

整个天气预报的XML只有一个<string>元素,代码看了很久感觉没什么问题,onclick后报异常。后来单步调试运行发现 执行完第一次startElement后立马就报错了。DEBUG视图 弹出一个ExpatParser.startElement(。。。)的标签页 内部标注了一行文本“Source not found.”
我这个代码应该怎么改~~真心不知道怎么办了



WeatherReport.java
=================
public class WeatherReport extends Activity implements OnClickListener {
private static final String WEATHER_API_URL = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName?theCityName=";
private static final String NETWORK_ERROR = "网络异常";
private EditText weather_city_edit;
private Button goQuery;
private TextView city;
private TextView weather;
private TextView wendu;
private ImageView imgweather;

private String info="info";
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  city=(TextView) findViewById(R.id.city);
  weather=(TextView) findViewById(R.id.weather);
  wendu=(TextView) findViewById(R.id.wendu);
  imgweather=(ImageView) findViewById(R.id.imgweather);
  weather_city_edit = (EditText) findViewById(R.id.weather_city_edit);
  goQuery = (Button) findViewById(R.id.goQuery);
  goQuery.setOnClickListener(this);
  
}
public void onClick(View v) {
  // 获得用户输入的城市名称
  Log.i(info,"onClick");
  String cityname = weather_city_edit.getText().toString().trim();
  String resultStr=WEATHER_API_URL + cityname;
  
  try {
  // 构建SAXParser
   
  URL url = new URL(resultStr);
  javaBean javabean=new javaBean();
  System.out.println(resultStr);
  SAXParserFactory factory = SAXParserFactory.newInstance(); //创建一个SAX工厂
  SAXParser parser = factory.newSAXParser();
  XMLReader xmlreader = parser.getXMLReader(); //创建一个SAX解析器
  SaxParseXml saxparsexml = new SaxParseXml();
  xmlreader.setContentHandler(saxparsexml); //设置内容处理器,根据参数来调用特定类来处理各阶段的解析事件
  xmlreader.parse(new InputSource(url.openStream()));//执行解析器解析
   
  city.setText(javabean.getCity().toString());
  weather.setText(javabean.getWeather().toString());
  wendu.setText(javabean.getWendu().toString());

} catch (ParserConfigurationException e) {
  e.printStackTrace();
  } catch (SAXException e) {
  e.printStackTrace();
  } catch (IOException e) {
  e.printStackTrace();
  }
}



SaxParseXml.java
=======================
package com.skyfors;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import android.util.Log;
public class SaxParseXml extends DefaultHandler {
// 存放遍历集合
private List<javaBean> list;
// 构建 对象
private javaBean javaBean;
// 用来存放每次遍历后的元素名称(节点名称)
private String tagName;
private int i;
private String info="info";
public List<javaBean> getList() {
  return list;
}
public void setList(List<javaBean> list) {
  相关解决方案