【发布时间】:2016-04-03 19:40:23
【问题描述】:
我正在从 Android Studio 中的本地 HTML 页面抓取数据,而不是抓取所有显示“text_view”的正确信息。有人知道如何显示我抓取的数据吗?下面是一段主要代码。
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();
}*/
try {
StringBuilder buf=new StringBuilder();
InputStreamReader inputStream = new InputStreamReader(getAssets().open("filename.html"));
BufferedReader bufferedReader = new BufferedReader(inputStream);
String str;
while ((str=bufferedReader.readLine()) != null) {
buf.append(str);
}
Document doc = Jsoup.parse(buf.toString());
//other code parts goes next
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();
}
}
下面是content_main.xml
<?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="text_view1"
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
【问题讨论】:
标签: java android android-studio web-scraping jsoup