【问题标题】:Accessing content of news using jsoup使用 jsoup 访问新闻内容
【发布时间】:2014-02-21 19:55:57
【问题描述】:

我正在连接到一个网站,我从该网站获取新闻并将它们放入列表中。当我点击新闻(它的标题)时,我想转到另一个页面,在那里我可以看到新闻的内容。我正在使用 jsoup 执行此操作。 Logcat 没有给我任何错误,但是当我单击标题时应用程序崩溃了。

这是我的活动:

公共类 TargetActivity1 扩展 Activity { ListView 列表;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.web);

    Parse parsing = new Parse();
    parsing.execute();

}

private class Parse extends AsyncTask<Void, Void, Void> {

    String desc;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    @Override
    protected Void doInBackground(Void... params) {
        try {
            // Connect to the web site
            Document document = Jsoup.connect(
                    "http://www.polimi.it/en/news/").get();
            // Using Elements to get the Meta data
            Elements description = document.select("div .news-list-item a");
            // Locate the content attribute
            desc = description.attr("href");

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // Set description into TextView
        WebView txtdesc = (WebView) findViewById(R.id.webView2);

        txtdesc.addView(txtdesc);

        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(desc));
        startActivity(i);
        txtdesc.removeAllViews();

    }

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:

        Intent myIntent = new Intent(getApplicationContext(),
                MainActivity.class);
        startActivityForResult(myIntent, 0);

        return true;

    }
    return super.onOptionsItemSelected(item);
}

}

【问题讨论】:

  • 如果你的应用崩溃了,你应该在 logcat 中有一些东西
  • 是的。它说:没有找到处理意图的活动{act=android.intent.action.VIEW dat=(我正在解析的网站)}。所以问题出在 Intent 但我不明白为什么。

标签: android listview android-intent webview jsoup


【解决方案1】:

你的Parse类错了,应该是这样的:

private class Parse extends AsyncTask<Void, Void, String> {

    @Override
    protected String doInBackground(Void... params) {
        String desc = "";
        try {
            // Connect to the web site
            Document document = Jsoup.connect(
                    "http://www.polimi.it/en/news/").get();
            // Using Elements to get the Meta data
            Elements description = document.select("div .news-list-item a");
            // Locate the content attribute
            desc = description.attr("href");

        } catch (IOException e) {
            e.printStackTrace();
        }
        return desc;
    }

    @Override
    protected void onPostExecute(String result) {
        // Set description into TextView
        WebView txtdesc = (WebView) findViewById(R.id.webView2);

        txtdesc.addView(txtdesc); // adding txtdesc to txtdesc? it's wrong, bug

        Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(result));
        startActivity(i);

        txtdesc.removeAllViews();

    }
}

请在http://developer.android.com/reference/android/os/AsyncTask.html 阅读有关 AsyncTask 的信息。来自doInBackground 的结果可以像上面一样传递给onPostExecute。另请注意,您正在尝试向自身添加视图,我已对其进行了评论。

【讨论】:

    猜你喜欢
    • 2016-10-10
    • 2011-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多