0
am使用此代碼來解析xml文件我有url地址,但它給了我的錯誤android os網絡主線程異常在這句話 URLConnection httpConnection = url.openConnection( );inputStream = httpConnection.getInputStream(); 這是我的代碼:Android的XML解析代碼使用kxml.zip
package com.example.mainp;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Vector;
import org.apache.http.HttpConnection;
import org.kxml.Xml;
import org.kxml.parser.XmlParser;
import org.kxml.parser.ParseEvent;
import org.kxml.parser.XmlParser;
import android.app.Activity;
import android.widget.Toast;
public class parse {
public static Vector places;
Activity ac;
Place place;
private ParseEvent event = null;
public void init(Activity activity) {
this.ac=activity;
XmlParser parser = null;
InputStreamReader isR = null;
HttpConnection connection = null;
try {
InputStream inputStream = null;
System.out.println("0");
Toast.makeText(ac, "24", Toast.LENGTH_LONG).show();
//URL url = new URL("http://217.52.114.3/Mobinil%20Islamic%20WebService/MobinilIslamicService.asmx/GetIslamicDirectory");
URL url= new URL("http://217.52.114.3/CinemaGuid/CinemaGuid.asmx/getTVSchedule");
System.err.println("2");
URLConnection httpConnection = url.openConnection();
inputStream = httpConnection.getInputStream();
// inputStream = connection.openInputStream();
Toast.makeText(ac, "4", Toast.LENGTH_LONG).show();
isR = new InputStreamReader(inputStream, "UTF-8");
System.out.println("3");
parser = new XmlParser(isR);
System.out.println("4");
Toast.makeText(activity, "5", Toast.LENGTH_LONG).show();
places = new Vector<Place>();
traverse(parser);
isR.close();
inputStream.close();
inputStream = null;
isR = null;
// System.gc();
} catch (Exception ex) {
Toast.makeText(activity, ex.toString(), Toast.LENGTH_LONG).show();
}
}
private void traverse(XmlParser parser) {
// TODO Auto-generated method stub
boolean leave = false;
do {
try {
event = parser.read();
ParseEvent pe;
switch (event.getType()) {
case Xml.START_TAG:
// see API doc of StartTag for more access methods
// Pick up Title for display
if("places".equalsIgnoreCase(event.getName())){
places = new Vector();
//Toast.makeText(ac, "6", Toast.LENGTH_LONG).show();
}
if("places".equalsIgnoreCase(event.getName())){
place = new Place();
place.setName(event.getValue("name"));
place.seturl(event.getValue("url"));
place.setaddress(event.getValue("adress"));
//Toast.makeText(MainPActivity.this, "7", Toast.LENGTH_LONG).show();
}
if("description".equalsIgnoreCase(event.getName())){
pe = parser.read();
if(pe.getText()!=null){
place.setdescription(pe.getText().toString());
}
}
traverse(parser); // recursion call for each <tag></tag>
break;
case Xml.END_TAG:
if ("Place".equalsIgnoreCase(event.getName())) {
places.addElement(place);
// Toast.makeText(MainPActivity.this, "8", Toast.LENGTH_LONG).show();
}
leave = true;
break;
case Xml.END_DOCUMENT:
leave = true;
break;
case Xml.TEXT:
break;
case Xml.WHITESPACE:
break;
default:
}
} catch (Exception ex) {
Toast.makeText(ac, ex.getMessage(), Toast.LENGTH_LONG).show();
}
} while (!leave);
}
}
什麼是例外呢?請編輯你的答案。 –