?
的错误,后来查了下讲大概意思是android中解析的时候xml文件格式问题。
?
在文件的最前端 即<?xml version="1.0" encoding="UTF-8"?>前面不能有空格。
?
修改即好。
?
xml
<?xml version="1.0" encoding="UTF-8" ?> - <msg> <name>nico</name> <des>male</des> </msg>
?
ParserUtil
package com.nico; import java.io.InputStream; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import android.util.Xml; public class ParserUtil { private String myDes = " "; public String getDes(InputStream is){ XmlPullParser parser = Xml.newPullParser(); try { parser.setInput(is, "utf-8"); int event = parser.getEventType(); while(event!=XmlPullParser.END_DOCUMENT) { switch(event) { case XmlPullParser.START_TAG: String name = parser.getName(); System.out.println("--------456---------->"+name); if("des".equals(name)) { myDes = parser.nextText(); return myDes; } break; } event = parser.next(); } } catch (Exception e) { e.printStackTrace(); } return myDes; } }
?
TestActivity
package com.nico; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import android.app.Activity; import android.os.Bundle; import android.os.Handler; public class Test extends Activity { Handler mhandler = new Handler(); /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mhandler.post(new Runnable() { @Override public void run() { URL url; try { url = new URL("http://10.0.0.130:8080/msg.xml"); HttpURLConnection conn = (HttpURLConnection) url .openConnection(); InputStream is = conn.getInputStream(); ParserUtil util = new ParserUtil(); String ss = util.getDes(is); System.out.println("-------123---------->" + ss); } catch (Exception e) { e.printStackTrace(); } } }); } }
?