如何解决Jsoup数组索引超出界限异常?

时间:2016-08-06 21:19:38

标签: android html arrays jsoup

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

  

java.lang.ArrayIndexOutOfBoundsException:length = 0;索引= 0

为什么数组是空的?

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

Jsoup Class

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)" />   

1 个答案:

答案 0 :(得分:2)

It looks like pictures or text are 0 length arrays.

检查您是否使用更大的0来初始化它们(在调用createData方法之前)。

示例:

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