【问题标题】:How to resolve Jsoup Array Index Out Of Bounds Exception?如何解决 Jsoup 数组索引越界异常?
【发布时间】:2016-08-13 14:04:31
【问题描述】:

试图通过 HTML 解析从网站获取数据。

java.lang.ArrayIndexOutOfBoundsException: 长度=0;索引=0

为什么数组是空的?

我只包含了包含 Jsoup 的类,因为我确定错误就在其中。我还包括了网站的 HTML 部分,以防你们想看看它。

Jsoup 类

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;


public class contentExcluding {



    public String[] createData(final String [] text, final String [] pictures)
    {

        Runnable rMain = new Runnable() {
            @Override
            public void run() {
                try {
                    Document doc = Jsoup.connect("http://egyptianstreets.com/").get();
                    Element content = doc.getElementById("featured-multi-main-img");
                    Elements headlines = content.getElementsByTag("img");
                    Elements img = content.getElementsByAttribute("src");
                    String x = img.toString();
                    pictures[0] = x;
                    text[0] = x;

                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        };
        Thread t = new Thread(rMain);
        t.start();

        return pictures;
    }

}

HTML

<div id="head-wrap" class="left relative">
        <div class="head-wrap-out">
            <div class="head-wrap-in">
                                <div id="featured-multi-wrap" class="left relative">
                                    <div id="featured-multi-main" class="left relative">
                        <a href="http://egyptianstreets.com/2016/08/06/egyptian-rowers-nadia-negm-and-abdel-khalek-elbana-reach-quarter-finals-at-olympics/" rel="bookmark">
                        <div id="featured-multi-main-img" class="left relative">
                                                            <img width="1000" height="512" src="http://egyptianstreets.com/wp-content/uploads/2016/08/pablo-2-1000x512.png" class="unlazy reg-img wp-post-image" alt="pablo (2)" />                             <img width="400" height="240" src="http://egyptianstreets.com/wp-content/uploads/2016/08/pablo-2-400x240.png" class="unlazy mob-img wp-post-image" alt="pablo (2)" />   

【问题讨论】:

    标签: android html arrays jsoup


    【解决方案1】:

    It looks like pictures or text are 0 length arrays.

    检查您是否已将它们初始化(在调用createData 方法之前),其大小为 0。

    例子:

    String[] pictures = contentExcluding.createData(new String[1], new String[1]);
    

    【讨论】:

    • 我理解错误,但问题是 Jsoup 类将 null 返回到数组,因此它为空。
    • IndexOutOfBoundsException 与内容是否为空无关。 @Davide 是对的,你需要正确初始化你的数组。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-23
    • 2016-08-03
    • 2016-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多