我想解析html网站并获取字符串值。但是在解析div类时我收到错误。
<div class="content clear">
我写了上面的代码,但收到了错误。
try {
doc = Jsoup.connect("http://tvrehberi.hurriyet.com.tr/program-detay/308271/deli-deli-olma").get();
List<String> saatItem = new ArrayList<String>();
for (Element iterable : doc.getElementsByClass("content&clear")) {
saatItem.add(iterable.text());
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
此错误导致类具有空白字符。如果类值没有空白字符,则代码运行完美。 我该如何解决这个问题?
错误日志:
02-06 00:18:53.770: E/AndroidRuntime(28775): FATAL EXCEPTION: main
02-06 00:18:53.770: E/AndroidRuntime(28775): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.htmlparsingtutorial/com.example.htmlparsingtutorial.MainActivity}: android.os.NetworkOnMainThreadException
02-06 00:18:53.770: E/AndroidRuntime(28775): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
02-06 00:18:53.770: E/AndroidRuntime(28775): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
02-06 00:18:53.770: E/AndroidRuntime(28775): at android.app.ActivityThread.access$600(ActivityThread.java:140)
02-06 00:18:53.770: E/AndroidRuntime(28775): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
02-06 00:18:53.770: E/AndroidRuntime(28775): at android.os.Handler.dispatchMessage(Handler.java:99)
02-06 00:18:53.770: E/AndroidRuntime(28775): at android.os.Looper.loop(Looper.java:137)
02-06 00:18:53.770: E/AndroidRuntime(28775): at android.app.ActivityThread.main(ActivityThread.java:4898)
02-06 00:18:53.770: E/AndroidRuntime(28775): at java.lang.reflect.Method.invokeNative(Native Method)
02-06 00:18:53.770: E/AndroidRuntime(28775): at java.lang.reflect.Method.invoke(Method.java:511)
02-06 00:18:53.770: E/AndroidRuntime(28775): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
02-06 00:18:53.770: E/AndroidRuntime(28775): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
02-06 00:18:53.770: E/AndroidRuntime(28775): at dalvik.system.NativeStart.main(Native Method)
02-06 00:18:53.770: E/AndroidRuntime(28775): Caused by: android.os.NetworkOnMainThreadException
02-06 00:18:53.770: E/AndroidRuntime(28775): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
02-06 00:18:53.770: E/AndroidRuntime(28775): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
02-06 00:18:53.770: E/AndroidRuntime(28775): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
02-06 00:18:53.770: E/AndroidRuntime(28775): at java.net.InetAddress.getAllByName(InetAddress.java:214)
02-06 00:18:53.770: E/AndroidRuntime(28775): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
02-06 00:18:53.770: E/AndroidRuntime(28775): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
02-06 00:18:53.770: E/AndroidRuntime(28775): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
02-06 00:18:53.770: E/AndroidRuntime(28775): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
02-06 00:18:53.770: E/AndroidRuntime(28775): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
02-06 00:18:53.770: E/AndroidRuntime(28775): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
02-06 00:18:53.770: E/AndroidRuntime(28775): at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
02-06 00:18:53.770: E/AndroidRuntime(28775): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
02-06 00:18:53.770: E/AndroidRuntime(28775): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
02-06 00:18:53.770: E/AndroidRuntime(28775): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
02-06 00:18:53.770: E/AndroidRuntime(28775): at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:408)
02-06 00:18:53.770: E/AndroidRuntime(28775): at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:393)
02-06 00:18:53.770: E/AndroidRuntime(28775): at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:159)
02-06 00:18:53.770: E/AndroidRuntime(28775): at org.jsoup.helper.HttpConnection.get(HttpConnection.java:148)
02-06 00:18:53.770: E/AndroidRuntime(28775): at com.example.htmlparsingtutorial.MainActivity.onCreate(MainActivity.java:90)
02-06 00:18:53.770: E/AndroidRuntime(28775): at android.app.Activity.performCreate(Activity.java:5206)
02-06 00:18:53.770: E/AndroidRuntime(28775): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
02-06 00:18:53.770: E/AndroidRuntime(28775): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
02-06 00:18:53.770: E/AndroidRuntime(28775): ... 11 more
答案 0 :(得分:0)
是的,如果在主线程中运行网络代码,则抛出此异常。 解决方案是将您的任务作为AsyncTask运行。
顺便说一句。请测试此代码,在PC上运行:
Document doc = Jsoup.connect("http://tvrehberi.hurriyet.com.tr/program-detay/308271/deli-deli-olma").get();
List<String> saatItem = new ArrayList<String>();
for( Element element : doc.select("div[class=content clear]"))
{
saatItem.add(element.text());
}