将网站的某些内容显示在webview中

时间:2013-09-23 10:18:01

标签: javascript android html webview

我尝试将网站的动态某些内容加载到webview中,但我不能 我使用此代码`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;
}

} 但它显示给我一个"错误"` 是任何人可以帮助我或给我一个完整的例子链接

1 个答案:

答案 0 :(得分:1)

如果在主线程上调用 Jsoup.connect ,它将抛出NetworkOnMainThreadException。

尝试使用AsyncTask连接到博客,检索内容,然后将其设置为在TextView中显示。

请参阅选定的答案here,以获得相关的好例子。

另外,不要忘记将INTERNET权限添加到您的清单中!

<uses-permission android:name="android.permission.INTERNET" /> 
相关问题