我想解析一个网站以获取新闻信息。它适用于以前版本的 Android,但不适用于 ICS。这是之前工作的一段代码:
public NewsParsing(){
try {
url = new URL("http://www.curtin.edu.my/campusnews/index.htm");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
doc = Jsoup.parse(url, 3000);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (doc != null)
{
newsArray = new ArrayList<NewsStructure>();
Element latestNews = doc.select("div[id=post-]").first();
Iterator<Element> newsTable = latestNews.select("div[id=post-6400]").iterator();
while (newsTable.hasNext())
{
newsContent = new NewsStructure();
Element currIte = newsTable.next();
Element hElement = currIte.getElementsByTag("h2").first();
String newsTitle = hElement.select("a").first().text().toString();
String newsLink = hElement.select("a").first().attr("href").toString();
Element sElement = currIte.getElementsByTag("div").first();
String newsSnippet = sElement.select("p").first().text().toString();
Element dElement = currIte.getElementsByTag("p").last();
String newsDate = dElement.text().toString();
newsContent.setTitle(newsTitle);
newsContent.setLink(newsLink);
newsContent.setSnippet(newsSnippet);
newsContent.setDate(newsDate);
newsArray.add(newsContent);
}
}
}
日志猫:
07-13 03:18:07.830: W/dalvikvm(1595): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
07-13 03:18:07.920: E/AndroidRuntime(1595): FATAL EXCEPTION: main
07-13 03:18:07.920: E/AndroidRuntime(1595): java.lang.RuntimeException: Unable to start activity ComponentInfo{my.csmict.csm/my.csmict.csm.CurtinNewsMain}: android.os.NetworkOnMainThreadException
07-13 03:18:07.920: E/AndroidRuntime(1595): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
07-13 03:18:07.920: E/AndroidRuntime(1595): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-13 03:18:07.920: E/AndroidRuntime(1595): at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-13 03:18:07.920: E/AndroidRuntime(1595): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-13 03:18:07.920: E/AndroidRuntime(1595): at android.os.Handler.dispatchMessage(Handler.java:99)
07-13 03:18:07.920: E/AndroidRuntime(1595): at android.os.Looper.loop(Looper.java:137)
07-13 03:18:07.920: E/AndroidRuntime(1595): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-13 03:18:07.920: E/AndroidRuntime(1595): at java.lang.reflect.Method.invokeNative(Native Method)
07-13 03:18:07.920: E/AndroidRuntime(1595): at java.lang.reflect.Method.invoke(Method.java:511)
07-13 03:18:07.920: E/AndroidRuntime(1595): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-13 03:18:07.920: E/AndroidRuntime(1595): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-13 03:18:07.920: E/AndroidRuntime(1595): at dalvik.system.NativeStart.main(Native Method)
07-13 03:18:07.920: E/AndroidRuntime(1595): Caused by: android.os.NetworkOnMainThreadException
07-13 03:18:07.920: E/AndroidRuntime(1595): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
07-13 03:18:07.920: E/AndroidRuntime(1595): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
07-13 03:18:07.920: E/AndroidRuntime(1595): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
07-13 03:18:07.920: E/AndroidRuntime(1595): at java.net.InetAddress.getAllByName(InetAddress.java:220)
07-13 03:18:07.920: E/AndroidRuntime(1595): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
07-13 03:18:07.920: E/AndroidRuntime(1595): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
07-13 03:18:07.920: E/AndroidRuntime(1595): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
07-13 03:18:07.920: E/AndroidRuntime(1595): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
07-13 03:18:07.920: E/AndroidRuntime(1595): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
07-13 03:18:07.920: E/AndroidRuntime(1595): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
07-13 03:18:07.920: E/AndroidRuntime(1595): at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
07-13 03:18:07.920: E/AndroidRuntime(1595): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
07-13 03:18:07.920: E/AndroidRuntime(1595): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
07-13 03:18:07.920: E/AndroidRuntime(1595): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
07-13 03:18:07.920: E/AndroidRuntime(1595): at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:404)
07-13 03:18:07.920: E/AndroidRuntime(1595): at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:391)
07-13 03:18:07.920: E/AndroidRuntime(1595): at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:157)
07-13 03:18:07.920: E/AndroidRuntime(1595): at org.jsoup.helper.HttpConnection.get(HttpConnection.java:146)
07-13 03:18:07.920: E/AndroidRuntime(1595): at my.csmict.csm.NewsParsing.<init>(NewsParsing.java:22)
07-13 03:18:07.920: E/AndroidRuntime(1595): at my.csmict.csm.CurtinNewsMain.onCreate(CurtinNewsMain.java:33)
07-13 03:18:07.920: E/AndroidRuntime(1595): at android.app.Activity.performCreate(Activity.java:4465)
07-13 03:18:07.920: E/AndroidRuntime(1595): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-13 03:18:07.920: E/AndroidRuntime(1595): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
请指教。我正在使用 Jsoup 1.6.3。