0

**接下来的代码应该将数据从 webService 解析到 textView;

问题:IOEXception:我怎样才能解决异常并让我的程序正常工作??**所以任何人都可以帮助我,因为我尝试了很多来自网络的示例代码,但对我没有任何帮助

 @Override
 public class NewParsActivity extends Activity {   

public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
         Button b;

        /** Called when the activity is first created. */
        static final String baseUrl="http://www.androidpeople.com/wp-content/uploads/2010/06/example.xml";

        TextView tv;
        EditText ed,ed2;


            tv=(TextView)findViewById(R.id.text);
            ed=(EditText)findViewById(R.id.edt);
            b=(Button) findViewById(R.id.button1);
            ed2=(EditText) findViewById(R.id.editText1);

            try{



                SAXParserFactory spf=SAXParserFactory.newInstance();
                SAXParser sp=spf.newSAXParser();
                XMLReader xr=sp.getXMLReader();
                URL link=new URL(baseUrl);
                handlXml doingWork=new handlXml();
                xr.setContentHandler(doingWork);
                xr.parse(new InputSource(link.openStream()));
                String information=doingWork.getInformation();
                tv.setText(information);
            }catch(Exception e){
             tv.setText("Error");
            }



    //
    public class handlXml  extends DefaultHandler {
       One  item=new One();

       public String getInformation(){
           return item.DataToString();

       }
    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        // TODO Auto-generated method stub
        if(localName.equals("website")){
            String uniN=attributes.getValue("category");
            item.setName(uniN);
        }
    }

}

    //

public class One {

    String UName;
    public void setName(String n){
        UName=n;    
    }


public String DataToString()
{
    return "In"+UName;
}
}
4

1 回答 1

0

你确定你已经正确地实现了处理程序吗?你仍然缺乏characters()endElement()

startElement()仅用于识别您正在阅读的标签。使用临时变量将内容存储在 中characters(),并将数据保存在endElement().

也许这就是问题所在。我不确定。

于 2012-06-26T12:03:08.760 回答