DoctorDoom
DoctorDoom

Reputation: 591

Display certain contents of a website into webview

im try to load dynamic certain contents of a website into webview but i cant i use this code `public class MainActivity extends Activity {

// blog url
static final String BLOG_URL = "http://www.internationalnewscenter.com/";

@Override
public void onCreate(Bundle savedInstanceState) {
    // set layout view
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // process
    try {
        ((TextView)findViewById(R.id.textView1)).setText(getBlogStats());
    } catch (Exception ex) {
        ((TextView)findViewById(R.id.textView1)).setText("Error");
    }
}

protected String getBlogStats() throws Exception {
    String result = "";
    // get html document structure
    Document document = Jsoup.connect(BLOG_URL).get();
    // selector query
    Elements nodeBlogStats = document.select("div#lofslidecontent45");
    // check results
    if(nodeBlogStats.size() > 0) {
        // get value
        result = nodeBlogStats.get(0).text();
    }

    // return
    return result;
}

} but it display for me an " error "` is any one can help me or give me a link for full example to do that

Upvotes: 0

Views: 88

Answers (1)

ashatte
ashatte

Reputation: 5538

It will throw a NetworkOnMainThreadException if you call Jsoup.connect on the main thread.

Try using an AsyncTask to connect to the blog, retrieve the content, and then set it to display in the TextView.

See the selected answer here for a good example of this.

Also, don't forget to add the INTERNET permission to your manifest!

<uses-permission android:name="android.permission.INTERNET" /> 

Upvotes: 1

Related Questions