使用Android的Jsoup解析HTML的麻烦 [英] Android parse trouble HTML using Jsoup

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

问题描述

我们得到麻烦解析Android中studio.the同一code。使用jsoup直播网址将在eclipse.we合作得到了错误

we get trouble parsing live url using jsoup in android studio.the same code will be working in eclipse.we got the error

Method threw 'java.lang.NullPointerException' exception.can not evaluate org.jsoup.parser.HtmlTreebuilder.tostring()

在这里我的活动code

here my activity code

String title=null;
    Document document;
    try {
        document= Jsoup.connect("https://www.facebook.com/")
                .userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6")
                .referrer("http://www.google.com")
                .get();
        title=document.title();
        TextView text=(TextView)findViewById(R.id.textView);
        text.setText(title);

    } catch (Exception e) {
        e.printStackTrace();
        Log.d("tag","document");

    }

如何解决这个问题,从实际网址拿到冠军?

how to solve this issue and get the title from the live url?

推荐答案

您可以使用的AsyncTask 类,以避免 NetworkOnMainThreadException
在这里你可以试试这个code。

you can use AsyncTask class to avoid NetworkOnMainThreadException here you can try this code.

private class FetchWebsiteData extends AsyncTask<Void, Void, Void> {
    String websiteTitle, websiteDescription;

    @Override
    protected void onPreExecute() {
        //progress dialog
    }

    @Override
    protected Void doInBackground(Void... params) {
        try {
            // Connect to website
            Document document = Jsoup.connect(URL).get();
            // Get the html document title
            websiteTitle = document.title();
            Elements description = document.select("meta[name=description]");
            // Locate the content attribute
            websiteDescription = description.attr("content");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // Set title into TextView
        TextView txttitle = (TextView) findViewById(R.id.txtData);
        txttitle.setText(websiteTitle + "\n" + websiteDescription);
        mProgressDialog.dismiss();
    }
}

}

和添加mainActivity此

and add your mainActivity this

new FetchWebsiteData().execute();

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

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