使用android中的jsoup库从html页面中提取图像类型

时间:2017-03-24 11:37:10

标签: php android html html5 jsoup

<div class="_jjzlb" style="padding-bottom: 55.2778%;"><img alt="AT Dam party.. #nashik  #big #dam" class="_icyx7" id="pImage_11" src="https://instagram.fbom1-2.fna.fbcdn.net/t51.2885-15/e35/17438694_254543168340407_6435023364997251072_n.jpg" style=""></div>

从上面的代码我必须提取此链接 - https://instagram.fbom1-2.fna.fbcdn.net/t51.2885-15/e35/17438694_254543168340407_6435023364997251072_n.jpg

我应该如何使用android中的jsoup库提取链接?

我已经实现了如下代码。

class RetrieveFeedTask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... strings) {

        Document doc = null;
        try {
            doc = Jsoup.connect("https://www.instagram.com/p/BR72iDGg8bAcA73KgP0ZqCopjKfvn5wJy1OQE40/").get();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //Get all elements with img tag ,
        Elements img = doc.getElementsByTag("img");

        String imgSrc = img.attr("src");

        System.out.println("Img source: " + imgSrc);

        return img.toString();
    }

    protected void onPostExecute(String feed) {

        Log.e("MyTag", "Final links ==" + feed);


    }
}

但是这段代码给出了错误 - java.lang.NullPointerException:尝试在一个上调用虚方法'org.jsoup.select.Elements org.jsoup.nodes.Document.select(java.lang.String)' null对象引用

如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您无需登录。

class RetrieveFeedTask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... strings) {

        Document doc = null;
        try {
            doc = Jsoup.connect("https://www.instagram.com/p/BR72iDGg8bAcA73KgP0ZqCopjKfvn5wJy1OQE40/").get();
        } catch (IOException e) {
            e.printStackTrace();
        }


        Elements img = doc.select("img");

        String imgSrc = img.attr("src");

        System.out.println("Img source: " + imgSrc);

        return img.toString();
    }

    protected void onPostExecute(String feed) {

        Log.e("MyTag", "Final links ==" + feed);


    }
}