Android HTML Jsoup [英] Android HTML Jsoup

查看:140
本文介绍了Android HTML Jsoup的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从我的拼贴新闻网站获取图像的绝对URL,但到目前为止还没有成功。我在这个网站上工作 http://www.dcu.ie/news/index.shtml。从源头可以看出,第一个图像具有绝对URL,但其余部分只有相对URL。我已经尝试过Jsoups文档中的示例,但无法使其工作。这将显示第一张图像,然后显示其余的空白框。我很感激任何可能的帮助。谢谢

Im trying to get the absolute URLs for images from my collages news website but have so far been unsuccessful. I am working from this site http://www.dcu.ie/news/index.shtml. As you can see from the source the first image has the absolute URL but the remainder only have the relative URLs. I have tried examples from Jsoups documentation but cant get it to work. This displays the first image and then empty boxes for the rest. I'd appreciate any help possible.Thanks

public class NewsActivity extends Activity {
    WebView mWebView;
    String test2 = "<html><body>";
    Document docs;
    public void main(String... args) 
        {
        try 
        {
        docs = Jsoup.connect("http://www.dcu.ie/news/index.shtml").get();
    } 
        catch (IOException e) 
        {
        e.printStackTrace();
    }
        Elements imgs = docs.select("img[src$=.jpg]");
        for (Element img : imgs) 
        {
            String url = img.toString();
            test2 = test2 + " " + url + " ";
        }
        public void onCreate(Bundle savedInstanceState) {
        main();

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.setWebViewClient(new NewsClient());
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setDomStorageEnabled(true);
        mWebView.loadData(test2, "text/html", "utf-8");
    }
}


推荐答案

你需要 元素# absUrl() 提取绝对URL而不是 Element#toString()以获取整个HTML元素的文本表示。

You need Element#absUrl() to extract the absolute URL instead of Element#toString() to get the text representation of the whole HTML element.

Elements imgs = docs.select("img[src$=.jpg]");
for (Element img : imgs) {
    String url = img.absUrl("src");
    String newImg = "<img src=\"" + url + "\"/>";
    // ...
}

这篇关于Android HTML Jsoup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆