0

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()

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?

Jos Sat
  • 83
  • 1
  • 9
  • Also based on comments from that question: are you sure that you are using same versions of jsoup in both android-studio and eclipse? – Pshemo Apr 02 '15 at 17:44
  • am using the same version of jsoup latest 1.8.1.help e to solve this issue – Jos Sat Apr 03 '15 at 04:58
  • @Pshemo http://stackoverflow.com/questions/29390379/couldnt-parse-the-html-tags-from-live-site-in-android take a look now – Jos Sat Apr 03 '15 at 07:12

1 Answers1

1

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();
    }
}

}

and add your mainActivity this

new FetchWebsiteData().execute();
parithi
  • 270
  • 3
  • 15