Scraped Data not Printing-Android Studio

时间:2016-04-01 12:52:20

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

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

   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" />

1 个答案:

答案 0 :(得分:0)

根据对问题的评论:

如果您要向TextView显示任何消息,请获取该消息,然后将文本设置为该消息。

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

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

TextView中为content_main定义ID:

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