android用SAX解析webService返回的数据出错

懒睡的猫 2014-04-21 02:47:17
这2天在调试android的sax解析自己用axis2做的一个webService查询sql返回的数据,但一直都出错进入不了解析的startElement


下面是代码(就一个按钮显示解析的数据):
testButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

try {
SoapObject request1 = new SoapObject(space, "queryStudents");
//request1.addProperty("name", "小明");
HttpTransportSE ht1 = new HttpTransportSE(
"http://192.168.1.3:8080/axis2/services/MyService");
ht1.debug = true;
SoapSerializationEnvelope envelope1 = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope1.bodyOut = request1;
envelope1.dotNet = true;
envelope1.setOutputSoapObject(request1);
//envelope1.encodingStyle = "UTF-8";

ht1.call(space_name, envelope1);

//把返回值(xml格式)强制转化为Object类型
//Object object1 = (Object) envelope1.getResponse();
SoapObject object1 = (SoapObject)envelope1.bodyIn;
String info2 = object1.getProperty(0).toString();
System.out.println("info2--->" + info2);
//对于返回值有多个内容是,可以考虑解析xml
String info1 = envelope1.bodyIn.toString();
System.out.println("result--->" + info1);
try{
//创建一个SAXParserFactory(写法已规定好)
SAXParserFactory factory = SAXParserFactory.newInstance();
//创建XML的一个对象(写法已规定好)作用:逐行扫描xml文件
XMLReader reader = factory.newSAXParser().getXMLReader();
//为XMLreader设置内容处理器,利用继承来实现DefaultHandler接口里面的一些方法
reader.setContentHandler(new MycontenHander());
//开始解析XML文件
reader.parse(new InputSource(new StringReader(info1)));
//reader.parse(inputSource);
}
catch(Exception e){
e.printStackTrace();
}

} catch (HttpResponseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
});




下面是webService返回的数据:
<ns:queryStudentsResponse xmlns:ns="http://service" xmlns:ax21="http://entity/xsd">
<ns:return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ax21:Student">
<ax21:address>南京</ax21:address>
<ax21:id>1</ax21:id>
<ax21:name>小明</ax21:name>
<ax21:number>1</ax21:number>
<ax21:tel>119</ax21:tel>
</ns:return>
<ns:return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ax21:Student">
<ax21:address>西藏</ax21:address>
<ax21:id>2</ax21:id>
<ax21:name>小郭</ax21:name>
<ax21:number>2</ax21:number>
<ax21:tel>110</ax21:tel>
</ns:return>
<ns:return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ax21:Student">
<ax21:address>朝鲜</ax21:address>
<ax21:id>3</ax21:id>
<ax21:name>坤哥</ax21:name>
<ax21:number>3</ax21:number>
<ax21:tel>117</ax21:tel>
</ns:return>
<ns:return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ax21:Student">
<ax21:address>加拿大</ax21:address>
<ax21:id>4</ax21:id>
<ax21:name>张三</ax21:name>
<ax21:number>0</ax21:number>
<ax21:tel>120</ax21:tel>
</ns:return>
</ns:queryStudentsResponse>
复制代码


显示的错误说是在:
04-21 05:11:35.767: WARN/System.err(1401): at com.example.webtestactivity.MainActivity$2.onClick(MainActivity.java:96)
也就是这句:reader.parse(new InputSource(new StringReader(info1)));
这个错在info1,但我也不知道是什么问题
还有个:
04-21 05:11:35.716: WARN/System.err(1401): org.apache.harmony.xml.ExpatParser$ParseException: At line 1, column 21: not well-formed (invalid token)
这个是在说我的那个webService返回的数据格式有问题吗??还是??





这是打印的信息:
04-21 05:11:35.695: INFO/System.out(1401): info2--->Student{address=南京; id=1; name=小明; number=1; tel=119; }


04-21 05:11:35.695: INFO/System.out(1401): result--->queryStudentsResponse{return=Student{address=南京; id=1; name=小明; number=1; tel=119; }; return=Student{address=西藏; id=2; name=小郭; number=2; tel=110; }; return=Student{address=朝鲜; id=3; name=坤哥; number=3; tel=117; }; return=Student{address=加拿大; id=4; name=张三; number=0; tel=120; }; }

04-21 05:11:35.716: INFO/System.out(1401): ------begin-----


然后就进不了那个startElement了

这数据有木有问题啊??求指导

startElement的函数:
       @Override
public void startElement(String uri, String localName, String qName,
Attributes atts) throws SAXException {
// TODO Auto-generated method stub
System.out.println("Qname------>" + qName);
System.out.println("Localname------>" + localName);
tagName = localName;
if (localName.equals("return")) {
for (int i = 0; i < atts.getLength(); i++) {
System.out.println(atts.getLocalName(i) + atts.getValue(i));
}
}

}
复制代码


速度来人啊!!!!
...全文
64 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

24,923

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Apache
社区管理员
  • Apache
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧