android.os.NetworkOnMainThreadException清单
在下面的代码中,我运行RssReader的Android项目时出现错误。
我的主要文件:
public class Earthquake extends Activity {
ListView earthquakeListView;
ArrayAdapter < Quake > aa;
ArrayList < Quake > earthquakes = new ArrayList < Quake > ()
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
earthquakeListView = (ListView) this.findViewById(R.id.earthquakeListView);
int layoutID = android.R.layout.simple_list_item_1;
aa = new ArrayAdapter < Quake > (this, layoutID, earthquakes);
earthquakeListView.setAdapter(aa);
refreshEarthquakes();
}
private void refreshEarthquakes() {
//get the XML
URL url;
try {
String quakeFeed = getString(R.string.quake_feed);
url = new URL(quakeFeed);
URLConnection connection;
connection = url.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection) connection;
int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream in = httpConnection.getInputStream();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
//Parse the earthquake feed
Document dom = db.parse( in );
Element docEle = dom.getDocumentElement();
//Clear the old earthquakes
earthquakes.clear();
//Get a list of each earthquake entry
NodeList nl = docEle.getElementsByTagName("entry");
if (nl != null && nl.getLength() > 0) {
for (int i = 0; i < nl.getLength(); i++) {
Element entry = (Element) nl.item(i);
Element title = (Element) entry.getElementsByTagName("title").item(0);
Element g = (Element) entry.getElementsByTagName("georss:point").item(0);
Element when = (Element) entry.getElementsByTagName("updated").item(0);
Element link = (Element) entry.getElementsByTagName("link").item(0);
String details = title.getFirstChild().getNodeValue();
String hostname = "http://earthquake.usgs.gov";
String linkString = hostname + link.getAttribute("href");
String point = g.getFirstChild().getNodeValue();
String dt = when.getFirstChild().getNodeValue();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'");
Date qdate = new GregorianCalendar(0, 0, 0).getTime();
try {
qdate = sdf.parse(dt);
} catch (ParseException e) {
e.printStackTrace();
}
String[] location = point.split(" ");
Location l = new Location("dummyGPS");
l.setLatitude(Double.parseDouble(location[0]));
l.setLongitude(Double.parseDouble(location[1]));
String magnitudeString = details.split(" ")[1];
int end = magnitudeString.length() - 1;
double magnitude = Double.parseDouble(magnitudeString.substring(0, end));
details = details.split(",")[1].trim();
Quake quake = new Quake(qdate, details, l, magnitude, linkString);
//Process a newly found earthquake
addNewQuake(quake);
}
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} finally {}
}
private void addNewQuake(Quake _quake) {
//Add the new quake to our list of earthquakes
earthquakes.add(_quake);
//Notify the array adapter of a change
aa.notifyDataSetChanged();
}
}
第二个文件:
public class Quake {
private Date date;
private String details;
private Location location;
private double magnitude;
private String link;
public Date getDate() {
return date;
}
public String getDetails() {
return details;
}
public Location getLocation() {
return location;
}
public double getMagnitude() {
return magnitude;
}
public String getLink() {
return link;
}
public Quake(Date _d, String _det, Location _loc, double _mag, String _link) {
date = _d;
details = _det;
location = _loc;
magnitude = _mag;
link = _link;
}
@Override
public String toString() {
SimpleDateFormat sdf = new SimpleDateFormat("HH.mm");
String dateString = sdf.format(date);
return dateString + ": " + magnitude + " " + details;
}
}
我已阅读了许多文章,并试图添加AsyncTask
但它不起作用。 我也试着添加这个:
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
但是这个错误出现了:
android.os.NetworkOnMainThreadException
我该如何解决这个问题?
当应用程序尝试在其主线程上执行联网操作时,会引发此异常。 在AsyncTask
运行你的代码。
请参阅:http://www.androiddesignpatterns.com/2012/06/app-force-close-honeycomb-ics.html
链接地址: http://www.djcxy.com/p/5027.html