protected Void doInBackground(Void... params) {
try {
// Connect to the web site
Document document = Jsoup.connect(url).get();
// Using Elements to get the Meta data
// Locate the content attribute
Elements h3Tag = document.select("h3");
desc1 = Integer.toString(h3Tag.size());
if(Integer.parseInt(desc1)==1)
{
Element description = document.getElementsByTag("table").get(3);
}
在上面的代码中,每当我添加行
document.getElementByTag("table").get(3));
该程序崩溃..但是,如果我删除h3tag选择它将工作正常..其中只有一个将工作,而不是两个..帮助?
日志
01-12 16:33:26.314: E/AndroidRuntime(966): at android.os.AsyncTask$3.done(AsyncTask.java:299)
01-12 16:33:26.314: E/AndroidRuntime(966): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
01-12 16:33:26.314: E/AndroidRuntime(966): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
01-12 16:33:26.314: E/AndroidRuntime(966): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
01-12 16:33:26.314: E/AndroidRuntime(966): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-12 16:33:26.314: E/AndroidRuntime(966): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
01-12 16:33:26.314: E/AndroidRuntime(966): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
01-12 16:33:26.314: E/AndroidRuntime(966): at java.lang.Thread.run(Thread.java:856)
01-12 16:33:26.314: E/AndroidRuntime(966): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 3, size is 3
01-12 16:33:26.314: E/AndroidRuntime(966): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
01-12 16:33:26.314: E/AndroidRuntime(966): at java.util.ArrayList.get(ArrayList.java:304)
01-12 16:33:26.314: E/AndroidRuntime(966): at org.jsoup.select.Elements.get(Elements.java:501)
01-12 16:33:26.314: E/AndroidRuntime(966): at com.example.scrapping.MainActivity$Description.doInBackground(MainActivity.java:132)
01-12 16:33:26.314: E/AndroidRuntime(966): at com.example.scrapping.MainActivity$Description.doInBackground(MainActivity.java:1)
01-12 16:33:26.314: E/AndroidRuntime(966): at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-12 16:33:26.314: E/AndroidRuntime(966): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
01-12 16:33:26.314: E/AndroidRuntime(966): ... 4 more
01-12 16:33:29.355: E/WindowManager(966): Activity com.example.scrapping.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40d42af0 V.E..... R......D 0,0-772,216} that was originally added here
01-12 16:33:29.355: E/WindowManager(966): android.view.WindowLeaked: Activity com.example.scrapping.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40d42af0 V.E..... R......D 0,0-772,216} that was originally added here
01-12 16:33:29.355: E/WindowManager(966): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:354)
01-12 16:33:29.355: E/WindowManager(966): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:216)
01-12 16:33:29.355: E/WindowManager(966): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
01-12 16:33:29.355: E/WindowManager(966): at android.app.Dialog.show(Dialog.java:281)
01-12 16:33:29.355: E/WindowManager(966): at com.example.scrapping.MainActivity$Description.onPreExecute(MainActivity.java:116)
01-12 16:33:29.355: E/WindowManager(966): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
01-12 16:33:29.355: E/WindowManager(966): at android.os.AsyncTask.execute(AsyncTask.java:534)
01-12 16:33:29.355: E/WindowManager(966): at com.example.scrapping.MainActivity$2.onClick(MainActivity.java:55)
01-12 16:33:29.355: E/WindowManager(966): at android.view.View.performClick(View.java:4204)
01-12 16:33:29.355: E/WindowManager(966): at android.view.View$PerformClick.run(View.java:17355)
01-12 16:33:29.355: E/WindowManager(966): at android.os.Handler.handleCallback(Handler.java:725)
01-12 16:33:29.355: E/WindowManager(966): at android.os.Handler.dispatchMessage(Handler.java:92)
01-12 16:33:29.355: E/WindowManager(966): at android.os.Looper.loop(Looper.java:137)
01-12 16:33:29.355: E/WindowManager(966): at android.app.ActivityThread.main(ActivityThread.java:5041)
01-12 16:33:29.355: E/WindowManager(966): at java.lang.reflect.Method.invokeNative(Native Method)
01-12 16:33:29.355: E/WindowManager(966): at java.lang.reflect.Method.invoke(Method.java:511)
01-12 16:33:29.355: E/WindowManager(966): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-12 16:33:29.355: E/WindowManager(966): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-12 16:33:29.355: E/WindowManager(966): at dalvik.system.NativeStart.main(Native Method)
01-12 16:33:31.285: I/Process(966): Sending signal. PID: 966 SIG: 9
答案 0 :(得分:2)
我认为你现在已经知道了这一点,但我会发布一个解释以供将来参考:
错误是IndexOutOfBoundsException由以下行引起的(特别是get(3)
部分):
Element description = document.getElementsByTag("table").get(3);
根据日志输出,description
的大小为3.也就是说,您正在解析的HTML文档中只存在3个<table>
元素。请记住索引编号从零开始,3是无效索引。也就是说,只有0,1和2是大小为3的ArrayList的有效索引。
删除Elements h3Tag = document.select("h3");
行时代码工作的原因是因为没有这个,if语句的条件不满足,因此它不会运行代码。
答案 1 :(得分:1)
您正在使用错误索引..尝试1或2,因为您有此异常:
01-12 16:33:26.314: E/AndroidRuntime(966): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 3, size is 3