这是表格enter image description here
我正在使用此代码
Document doc = Jsoup.parse(s);
Elements elements=doc.select("table#table1").select("tbody").select("tr");
for (int i = 0; i < elements.size(); i++) {
Elements row = elements.get(i).getElementsByClass("MTTD8");
Elements cols = row.select("td");
System.out.println("MTTD8---> "+ row.text().toString());
}
我收到了这个输出:
MTTD8--->
03-24 19:15:57.512 20390-20536/com.example.rushabh123453.attendance I/System.out: MTTD8--->
03-24 19:15:57.515 20390-20536/com.example.rushabh123453.attendance I/System.out: MTTD8---> INDUSTRIAL MANAGEMENT 31 29 93.55
03-24 19:15:57.517 20390-20536/com.example.rushabh123453.attendance I/System.out: MTTD8---> MINI PROJECT AND SEMINAR 0 0 0
03-24 19:15:57.519 20390-20536/com.example.rushabh123453.attendance I/System.out: MTTD8---> MINI PROJECT AND SEMINAR LAB 16 16 100
03-24 19:15:57.521 20390-20536/com.example.rushabh123453.attendance I/System.out: MTTD8---> ANTENA AND WAVE PROPAGATION 39 38 97.44
03-24 19:15:57.523 20390-20536/com.example.rushabh123453.attendance I/System.out: MTTD8---> EMBEDDED PROCESSORS 34 29 85.29
03-24 19:15:57.524 20390-20536/com.example.rushabh123453.attendance I/System.out: MTTD8---> INFORMATION THEORY AND CODING TECHNIQUES 42 30 71.43
03-24 19:15:57.526 20390-20536/com.example.rushabh123453.attendance I/System.out: MTTD8---> POWER ELECTRONICS 23 15 65.22
03-24 19:15:57.528 20390-20536/com.example.rushabh123453.attendance I/System.out: MTTD8---> POWER ELECTRONICS LAB 12 11 91.67
03-24 19:15:57.529 20390-20536/com.example.rushabh123453.attendance I/System.out: MTTD8---> ANTENA AND WAVE PROPAGATION LAB 10 9 90
03-24 19:15:57.531 20390-20536/com.example.rushabh123453.attendance I/System.out: MTTD8---> INFORMATION THEORY AND CODING TECHNIQUES LAB 11 10 90.91
03-24 19:15:57.532 20390-20536/com.example.rushabh123453.attendance I/System.out: MTTD8---> EMBEDDED PROCESSORS LAB 9 9 100
03-24 19:15:57.535 20390-20536/com.example.rushabh123453.attendance I/System.out: MTTD8---> Average : 88.55
但我只想要主题名称和出席率
我尝试了很多东西,但是得到了任何结果。 实际上我想存储主题名称,参加讲座和总讲座和百分比
这样我就能在应用中方便地展示这个
示例:
我想将所有科目存储在科目[]中
即subject[0]=Industrial managemnet
和
attended[0]=29
total[0]=31
avg[0]=93.55
更新帖子: -
添加此代码后
for (int i = 0; i < elements.size(); i++) { //first row is the col names so skip it.
Elements row = elements.get(i).getElementsByClass("MTTD8");
Elements cols = row.select("td");
t=row.text().toString();
ss1.append(t);
int j=0;
for (j = 0; j < cols.size(); j++) {
subjects[i] = cols.get(0).text().toString();
System.out.println("subjects "+ subjects[i] );
}
}
我收到了这个输出: -
03-24 20:41:11.928 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects INDUSTRIAL MANAGEMENT
03-24 20:41:11.928 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects INDUSTRIAL MANAGEMENT
03-24 20:41:11.928 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects INDUSTRIAL MANAGEMENT
03-24 20:41:11.928 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects INDUSTRIAL MANAGEMENT
03-24 20:41:11.930 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects MINI PROJECT AND SEMINAR
03-24 20:41:11.930 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects MINI PROJECT AND SEMINAR
03-24 20:41:11.930 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects MINI PROJECT AND SEMINAR
03-24 20:41:11.930 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects MINI PROJECT AND SEMINAR
03-24 20:41:11.934 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects MINI PROJECT AND SEMINAR LAB
03-24 20:41:11.934 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects MINI PROJECT AND SEMINAR LAB
03-24 20:41:11.934 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects MINI PROJECT AND SEMINAR LAB
03-24 20:41:11.934 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects MINI PROJECT AND SEMINAR LAB
03-24 20:41:11.936 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects ANTENA AND WAVE PROPAGATION
03-24 20:41:11.936 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects ANTENA AND WAVE PROPAGATION
03-24 20:41:11.936 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects ANTENA AND WAVE PROPAGATION
03-24 20:41:11.936 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects ANTENA AND WAVE PROPAGATION
03-24 20:41:11.938 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects EMBEDDED PROCESSORS
03-24 20:41:11.938 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects EMBEDDED PROCESSORS
03-24 20:41:11.939 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects EMBEDDED PROCESSORS
03-24 20:41:11.939 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects EMBEDDED PROCESSORS
03-24 20:41:11.940 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects INFORMATION THEORY AND CODING TECHNIQUES
03-24 20:41:11.941 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects INFORMATION THEORY AND CODING TECHNIQUES
03-24 20:41:11.941 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects INFORMATION THEORY AND CODING TECHNIQUES
03-24 20:41:11.941 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects INFORMATION THEORY AND CODING TECHNIQUES
03-24 20:41:11.946 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects POWER ELECTRONICS
03-24 20:41:11.946 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects POWER ELECTRONICS
03-24 20:41:11.946 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects POWER ELECTRONICS
03-24 20:41:11.947 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects POWER ELECTRONICS
03-24 20:41:11.948 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects POWER ELECTRONICS LAB
03-24 20:41:11.948 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects POWER ELECTRONICS LAB
03-24 20:41:11.948 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects POWER ELECTRONICS LAB
03-24 20:41:11.949 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects POWER ELECTRONICS LAB
03-24 20:41:11.950 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects ANTENA AND WAVE PROPAGATION LAB
03-24 20:41:11.950 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects ANTENA AND WAVE PROPAGATION LAB
03-24 20:41:11.950 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects ANTENA AND WAVE PROPAGATION LAB
03-24 20:41:11.951 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects ANTENA AND WAVE PROPAGATION LAB
03-24 20:41:11.952 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects INFORMATION THEORY AND CODING TECHNIQUES LAB
03-24 20:41:11.952 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects INFORMATION THEORY AND CODING TECHNIQUES LAB
03-24 20:41:11.953 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects INFORMATION THEORY AND CODING TECHNIQUES LAB
03-24 20:41:11.953 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects INFORMATION THEORY AND CODING TECHNIQUES LAB
03-24 20:41:11.954 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects EMBEDDED PROCESSORS LAB
03-24 20:41:11.954 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects EMBEDDED PROCESSORS LAB
03-24 20:41:11.954 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects EMBEDDED PROCESSORS LAB
03-24 20:41:11.955 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects EMBEDDED PROCESSORS LAB
03-24 20:41:11.956 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects Average :
03-24 20:41:11.956 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects Average :
我每次受试4次
新更新的帖子:
for (int i = 0; i < elements.size(); i++) { //first row is the col names so skip it.
Elements row = elements.get(i).getElementsByClass("MTTD8");
Elements cols = row.select("td");
t=row.text().toString();
// textView.setText(t);
// System.out.println("MTTD8---> "+t );
ss1.append(t);
//System.out.println("MTTD8cols---> "+ cols.toString());
int j=0;
for (j = 0; j < cols.size(); j++) {
subjects[i] = cols.get(0).text().toString();
total[i] = cols.get(2).text().toString();
attended[i] = cols.get(1).text().toString();
avg[i] = cols.get(3).text().toString();
}
System.out.println("subjects "+ subjects[i] );
System.out.println("subjects "+ attended[i] );
System.out.println("subjects "+ total[i] );
System.out.println("subjects "+ avg[i]);
}
return null;
}
输出: -
03-24 21:09:09.394 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects null
03-24 21:09:09.394 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects null
03-24 21:09:09.394 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects null
03-24 21:09:09.394 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects null
03-24 21:09:09.395 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects null
03-24 21:09:09.395 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects null
03-24 21:09:09.395 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects null
03-24 21:09:09.396 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects null
03-24 21:09:09.398 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects INDUSTRIAL MANAGEMENT
03-24 21:09:09.398 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects 31
03-24 21:09:09.398 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects 29
03-24 21:09:09.398 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects 93.55
03-24 21:09:09.400 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects MINI PROJECT AND SEMINAR
03-24 21:09:09.400 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects 0
03-24 21:09:09.400 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects 0
03-24 21:09:09.400 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects 0
03-24 21:09:09.402 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects MINI PROJECT AND SEMINAR LAB
03-24 21:09:09.402 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects 16
03-24 21:09:09.402 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects 16
03-24 21:09:09.402 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects 100
03-24 21:09:09.403 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects ANTENA AND WAVE PROPAGATION
03-24 21:09:09.403 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects 39
03-24 21:09:09.403 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects 38
03-24 21:09:09.404 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects 97.44
03-24 21:09:09.406 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects EMBEDDED PROCESSORS
03-24 21:09:09.406 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects 34
03-24 21:09:09.406 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects 29
03-24 21:09:09.406 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects 85.29
03-24 21:09:09.408 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects INFORMATION THEORY AND CODING TECHNIQUES
03-24 21:09:09.408 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects 42
03-24 21:09:09.408 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects 30
03-24 21:09:09.408 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects 71.43
03-24 21:09:09.411 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects POWER ELECTRONICS
03-24 21:09:09.411 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects 23
03-24 21:09:09.411 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects 15
03-24 21:09:09.411 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects 65.22
03-24 21:09:09.413 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects POWER ELECTRONICS LAB
03-24 21:09:09.413 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects 12
03-24 21:09:09.413 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects 11
03-24 21:09:09.413 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects 91.67
03-24 21:09:09.415 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects ANTENA AND WAVE PROPAGATION LAB
03-24 21:09:09.415 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects 10
03-24 21:09:09.415 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects 9
03-24 21:09:09.415 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects 90
03-24 21:09:09.419 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects INFORMATION THEORY AND CODING TECHNIQUES LAB
03-24 21:09:09.419 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects 11
03-24 21:09:09.419 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects 10
03-24 21:09:09.419 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects 90.91
03-24 21:09:09.421 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects EMBEDDED PROCESSORS LAB
03-24 21:09:09.421 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects 9
03-24 21:09:09.421 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects 9
03-24 21:09:09.421 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects 100
03-24 21:09:09.430 15310-17183/com.example.rushabh123453.attendance E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #3
Process: com.example.rushabh123453.attendance, PID: 15310
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:304)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at org.jsoup.select.Elements.get(Elements.java:544)
at com.example.rushabh123453.attendance.MainActivity.getjson(MainActivity.java:188)
at com.example.rushabh123453.attendance.MainActivity.access$000(MainActivity.java:40)
at com.example.rushabh123453.attendance.MainActivity$Networking1.doInBackground(MainActivity.java:126)
at android.os.AsyncTask$2.call(AsyncTask.java:292)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
答案 0 :(得分:0)
System.out.println("MTTD8---> "+ col.get(0).text().toString());
打印第一个td
值。
System.out.println("MTTD8---> "+ col.get(2).text().toString());
打印出勤率td
。
修改强>
for (int i = 0; i < elements.size(); i++) {
Elements row = elements.get(i).getElementsByClass("MTTD8");
Elements cols = row.select("td");
if (cols.lenght() ==4) {
subjects[i] = cols.get(0).text().toString();
total[i] = cols.get(1).text().toString();
attended[i] = cols.get(2).text().toString();
avg[i] = cols.get(3).text().toString();
}
}
答案 1 :(得分:0)
尝试使用此
for (int i = 2; i < elements.size()-1; i++) { //first row is the col names so skip it.
Elements row = elements.get(i).getElementsByClass("MTTD8");
Elements cols = row.select("td");
t=row.text().toString();
// textView.setText(t);
// System.out.println("MTTD8---> "+t );
ss1.append(t);
//System.out.println("MTTD8cols---> "+ cols.toString());
int j;
for (j = 2; j < cols.size(); j++) {
subjects[i] = cols.get(0).text().toString();
total[i] = cols.get(2).text().toString();
attended[i] = cols.get(1).text().toString();
avg[i] = cols.get(3).text().toString();
}