【发布时间】: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