我正在使用此代码解析一个 xml 文件,我有 url 地址,但它在这句话中给出了错误 android os network on main thread exception URLConnection httpConnection = url.openConnection(); inputStream = httpConnection.getInputStream(); 这是我的代码:
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);
}
}