【问题标题】:Scraped Data not Printing- Android Studio抓取的数据不打印 - Android Studio
【发布时间】:2016-04-01 12:52:20
【问题描述】:

从 html 文件中抓取数据,当我运行我的应用程序时,会出现默认屏幕。请不要告诉我阅读 jsoup 文档,请阅读全部内容。有人知道如何让它在屏幕上打印吗?

   import android.app.Activity;
   import android.os.Bundle;
   import java.io.File;
   import java.io.IOException;
   import org.jsoup.Jsoup;
   import org.jsoup.nodes.Document;
   import org.jsoup.nodes.Element;
   import org.jsoup.select.Elements;
   import android.widget.TextView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    try {
        File input = new File("C:\\Users\\user\\Desktop\\Mobile Newest\\JSoup\\app\\src\\main\\assets\\filename.html");
        Document doc = Jsoup.parse(input, "UTF-8");
        Elements tableElements = doc.select("td");
        TextView textView = (TextView)findViewById(R.id.text_view);
        for (Element td : tableElements) {
            textView.setText(td.text());
            System.out.println(td.text());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    //System.out.println("Testing");
}

} 活动主要

<android.support.design.widget.AppBarLayout      android:layout_height="wrap_content"
    android:layout_width="match_parent"   android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
        android:layout_width="match_parent" android:layout_height="?  attr/actionBarSize"
        android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

<android.support.design.widget.FloatingActionButton android:id="@+id/fab"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"          android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

内容主要

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"   android:layout_width="match_parent"
android:layout_height="match_parent"   android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main" tools:context=".MainActivity">

<TextView android:text="Hello World!" android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

【问题讨论】:

  • 它是主活动屏幕,它只是说“Hello World”而不是我抓取的数据
  • 发布activity_main
  • content_main 也是主要的。顺便说一句,我已经发布了答案。检查一下。
  • 是的,在“TextView textView = (TextView)findViewById(R.id.text_view);”中添加只是 text_view 和未使用的 textView 出错了?
  • 检查更新的答案。那是一个例子。您需要为视图定义 id 以获取其引用。

标签: java android android-studio web-scraping jsoup


【解决方案1】:

根据问题的cmets:

如果您想向TextView 显示任何消息,请获取它的引用,然后为其设置文本。

TextView textView = (TextView)findViewById(R.id.text_view);

for (Element td : tableElements) {
   textView.setText(td.text());
}

content_main 中为您的TextView 定义一个ID:

<TextView
    android:id="@+id/text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

【讨论】:

  • 添加了这个,一旦我在手机上运行该应用程序仍然无法打印
  • 这是打印System.out.println(td.text())吗?
  • 如果System.out.println(td.text()) 不起作用,则存在解析问题。与在TextView中显示无关@
  • 对解决方案有任何想法吗?那和我的 html 有关系吗?
猜你喜欢
  • 1970-01-01
  • 2015-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-05
  • 1970-01-01
  • 2020-10-28
相关资源
最近更新 更多