java - Cannot Get HTML Elements (JSOUP) -


i trying website title , elements site jsoup android application. can title cannot element (article count example) id. have tried select() , getelementbyid() methods both don't work.

related html source code:

<div id="articlecount">     <a href="/wiki/special:statistics"title="special:statistics">4,891,985</a> articles in      <a href="/wiki/english_language" title="english language">english</a> </div> 

i want article count , show in tv2 textview.

java code:

public class mainactivity extends actionbaractivity {  string url = "https://en.wikipedia.org/wiki/main_page"; string title; element article; textview tv1, tv2; progressdialog mprogressdialog;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      tv1 = (textview)findviewbyid(r.id.tv1);     tv2 = (textview)findviewbyid(r.id.tv2);      new fetchwebsitedata().execute(); }  private class fetchwebsitedata extends asynctask<void, void, void> {      @override     protected void onpreexecute() {         super.onpreexecute();         mprogressdialog = new progressdialog(mainactivity.this);         mprogressdialog.setmessage("loading...");         mprogressdialog.setindeterminate(false);         mprogressdialog.show();     }      @override     protected void doinbackground(void... params) {         try {             document doc = jsoup.connect(url).get();             title = doc.title();             article = doc.select("div#articlecount > a").first();         } catch (ioexception e) {             e.printstacktrace();         }         return null;     }      @override     protected void onpostexecute(void result) {         tv1.settext(title + " ...");         tv2.settext(article.text());         mprogressdialog.dismiss();     }     }      ... } 

program stopping execution , giving error like:

... 06-15 11:34:45.744  13540-13540/com.samet.webparser e/androidruntime﹕ fatal exception: main   process: com.samet.webparser, pid: 13540   java.lang.nullpointerexception: attempt invoke virtual method 'java.lang.string org.jsoup.nodes.element.text()' on null object reference   @ com.samet.webparser.mainactivity$fetchwebsitedata.onpostexecute(mainactivity.java:62)   @ com.samet.webparser.mainactivity$fetchwebsitedata.onpostexecute(mainactivity.java:36)   @ android.os.asynctask.finish(asynctask.java:632)   @ android.os.asynctask.access$600(asynctask.java:177)   @ android.os.asynctask$internalhandler.handlemessage(asynctask.java:645) ... 

thanks help.

did debug code? obvious

article = doc.select("div#articlecount > a").first(); 

returns null. documented in api:

public element first() first matched element. returns: first matched element, or null if contents empty.

so selector seems incorrect. first should debug code or post full html doc.

edit: set project , tested code. during compared html input original page use. problem user-agent. when testing mobile device wikipedia homepage delivered in special mobile version not match selector used. fake desktop agent , you're fine:

document doc = jsoup.connect(url).useragent("mozilla").get(); 

Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -