<uses-permission android:name="android.permission.INTERNET"/>
import org.htmlcleaner.CleanerProperties; import org.htmlcleaner.HtmlCleaner; import org.htmlcleaner.SimpleHtmlSerializer; import org.htmlcleaner.TagNode; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.select.Elements;
Document doc = Jsoup.connect(url).get(); Elements newsRawTag = doc.select("div#cntent"); newPage=newsRawTag.html();
HtmlCleaner cleaner = new HtmlCleaner(); CleanerProperties props = cleaner.getProperties(); TagNode tagNode = new HtmlCleaner(props).clean(newPage); SimpleHtmlSerializer htmlSerializer = new SimpleHtmlSerializer(props); //the cleaned html is then passed to the webview widget for rendering browser.loadDataWithBaseURL(null, htmlSerializer.getAsString(tagNode), "text/html", "charset=UTF-8",null);
public class PortionofPageViewerActivity extends Activity { private WebView browser; private ProgressBar loadingProgressBar; private TextView txtLoading; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); browser = (WebView) findViewById(R.id.webView1); LoadContent("http://ayobamiadewole.com/Blog/ Android/How-to-Select-and-format-Portion-of-a-Webpage- Using-Jsoup-and-Htmlcleaner-in-Android.aspx"); } @SuppressLint({ "ParserError", "ParserError" }) private void LoadContent(final String url) { loadingProgressBar = (ProgressBar) findViewById(R.id.progressBar1); loadingProgressBar.setVisibility(View.VISIBLE); txtLoading=(TextView) findViewById(R.id.textView1); new Thread(new Runnable() { public void run() { final String newPage; try { Document doc =Jsoup.connect(url).get(); Elements newsRawTag = doc.select("div#postcontent"); newPage=newsRawTag.html(); runOnUiThread(new Runnable() { public void run() { try { HtmlCleaner cleaner = new HtmlCleaner(); CleanerProperties props = cleaner.getProperties(); TagNode tagNode = new HtmlCleaner(props).clean(newPage; SimpleHtmlSerializer htmlSerializer = new SimpleHtmlSerializer(props); browser.loadDataWithBaseURL(null,htmlSerializer. getAsString(tagNode), "text/html", "charset=UTF-8",null; loadingProgressBar.setVisibility(View.GONE); txtLoading.setVisibility(View.GONE); } catch (IOException e) { } } }); } catch (ClientProtocolException e) { } catch (IOException e) { } } }).start(); } }
Share this page on
25
Comment(s)
22
People Like(s) This Page
Permalink
comments powered by Disqus
Older Comment(s)