我是 JAVA / Android 开发的新手。
我制作了一个从 HTML 类中提取文本的应用程序;
protected List<String> doInBackground(String... url) {
try {
Document doc = Jsoup.connect(
"http://example/test.html").get();
Elements st1 = doc.select("a[class*=subject_rating_details");
for (Element element : st1) {
sgrade[0] = st1.get(0).text();
sgrade[1] = st1.get(0).text();
sgrade[2] = st1.get(0).text();
sgrade[3] = st1.get(0).text();
sgrade[4] = st1.get(0).text();
}
} catch (IOException e) {
e.printStackTrace();
}
List<String> pinfo = null;
return pinfo;
}
@Override
protected void onPostExecute(List<String> pinfo) {
prog.dismiss();
}
}
List<ListData> varlist = new ArrayList<ListData>();
String sgrade[] = new String[] {};
我使用 JSoup 从我的网页中提取 HTML class="subject_rating_details" 中的不同文本。
但它强制关闭上面的代码。
我可以使用单个字符串成功提取它,例如:
for (Element element : st1) {
stringname = st1.get(0).text();
stringname = st1.get(1).text();
stringname = st1.get(2).text();
stringname = st1.get(3).text();
stringname = st1.get(4).text();
}
但它只存储最后一个( stringname = st1.get(4).text(); )
我也试过:
for (Element element : st1) {
stringname1 = st1.get(0).text();
stringname2 = st1.get(1).text();
stringname3 = st1.get(2).text();
stringname4 = st1.get(3).text();
stringname5 = st1.get(4).text();
}
但我需要来自 st1 的文本在一个变量中。
我能做些什么?
谢谢
编辑
我想要这样的东西:
字符串 sgrade[] = 新字符串[] {};
for (Element element : st1) {
sgrade[0] = st1.get(0).text();
sgrade[1] = st1.get(0).text();
sgrade[2] = st1.get(0).text();
sgrade[3] = st1.get(0).text();
sgrade[4] = st1.get(0).text();
}
女巫后来我可以阅读每个文本并将其显示在 TextView 中:
textview1.setText(sgrade[0]); <--/// This would display "Ford"
textview2.setText(sgrade[1]); <--/// This would display "Mustang"
textview3.setText(sgrade[2]); <--/// This would display "2013"
/// HTML ///
...
<p class="subject_rating_details">Ford</p>
<p class="subject_rating_details">Mustang</p>
<p class="subject_rating_details">2013</p>
...
/// HTML ///