How to parse simple html code with jsoup? android -
How to parse simple html code with jsoup? android -
this part of html code
<div class="entry themeform"> <h3>dr james – opiekun naukowy</h3> <p><a href="http://www.page.com/picture.jpg"><img class="alignnone size-medium wp-image-31" alt="grynia" src="http://www.page.com/picture.jpg" width="200" height="300" /></a></p> <h3>kevin – prezes</h3> <p><a href="http://www.page.com/picture.jpg"><img class="alignnone size-medium wp-image-35" alt="prezes" src="http://www.page.com/picture.jpg" width="217" height="300" /></a></p> <h3>lucy – wice prezes</h3> <p><a href="http://www.page.com/picture.jpg"><img class="alignnone size-medium wp-image-36" alt="lucy" src="http://www.page.com/picture.jpg" width="225" height="300" /></a></p> <h3>zarząd</h3> <p><a href="http://www.page.com/picture.jpg"><img class="alignnone wp-image-37" alt="zarzad_knse" src="http://www.page.com/picture.jpg" width="489" height="256" /></a></p> <div class="clear"></div > </div><!--/.entry-->
firstly, want parse text tags in div. nice if help me parsing images in div (i changed image urls, because of privacy). new in jsoup, grateful if write me code, parsing text android activity.
edit ok, origin trying parse title shown in yours (smr) tutorial.
here code:
import java.io.ioexception; import org.jsoup.jsoup; import org.jsoup.nodes.document; import com.example.uwbnewapptest.r; import android.app.activity; import android.os.asynctask; import android.os.bundle; import android.view.view; import android.widget.textview; public class knseactivity extends activity { //textview title; string url="http://www.google.com"; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.knse_main); //title = (textview) findviewbyid(r.id.textview1); } public void bt(view v){ new title().execute(); } private class title extends asynctask<void, void, void> { string title; @override protected void doinbackground(void... params) { seek { // connect web site document document = jsoup.connect(url).get(); // html document title title = document.title(); } grab (ioexception e) { e.printstacktrace(); } homecoming null; } @override protected void onpostexecute(void result) { // set title textview textview txttitle = (textview) findviewbyid(r.id.textview1); txttitle.settext(title); } } }
but when run app , click on button, have error
edit 2:
06-21 16:18:01.808: e/androidruntime(28063): fatal exception: asynctask #2 06-21 16:18:01.808: e/androidruntime(28063): process: com.example.uwbnewapptest, pid: 28063 06-21 16:18:01.808: e/androidruntime(28063): java.lang.runtimeexception: error occured while executing doinbackground() 06-21 16:18:01.808: e/androidruntime(28063): @ android.os.asynctask$3.done(asynctask.java:300) 06-21 16:18:01.808: e/androidruntime(28063): @ java.util.concurrent.futuretask.finishcompletion(futuretask.java:355) 06-21 16:18:01.808: e/androidruntime(28063): @ java.util.concurrent.futuretask.setexception(futuretask.java:222) 06-21 16:18:01.808: e/androidruntime(28063): @ java.util.concurrent.futuretask.run(futuretask.java:242) 06-21 16:18:01.808: e/androidruntime(28063): @ android.os.asynctask$serialexecutor$1.run(asynctask.java:231) 06-21 16:18:01.808: e/androidruntime(28063): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1112) 06-21 16:18:01.808: e/androidruntime(28063): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:587) 06-21 16:18:01.808: e/androidruntime(28063): @ java.lang.thread.run(thread.java:841) 06-21 16:18:01.808: e/androidruntime(28063): caused by: java.lang.noclassdeffounderror: org.jsoup.jsoup 06-21 16:18:01.808: e/androidruntime(28063): @ com.uwbapp.knseactivity$title.doinbackground(knseactivity.java:43) 06-21 16:18:01.808: e/androidruntime(28063): @ com.uwbapp.knseactivity$title.doinbackground(knseactivity.java:1) 06-21 16:18:01.808: e/androidruntime(28063): @ android.os.asynctask$2.call(asynctask.java:288) 06-21 16:18:01.808: e/androidruntime(28063): @ java.util.concurrent.futuretask.run(futuretask.java:237) 06-21 16:18:01.808: e/androidruntime(28063): ... 4 more
there various ways extract info using jsoup. check http://jsoup.org/cookbook/extracting-data/selector-syntax.
in case text , image sources like
document doc = jsoup.connect(url).get(); for(element div : doc.select("div")){ system.out.println(div.text()); for(element img : div.select("img")){ system.out.println(img.attr("src")); } }
android html html-parsing jsoup
Comments
Post a Comment