【发布时间】:2015-02-26 20:03:52
【问题描述】:
在我的代码中,我尝试多次连接到该网站,以便在每次尝试连接时从表中提取一条记录。
我尝试一次提取所有记录,然后将其解析为字符串,但不同类组的每个时间表都有一点不同的间距,并且算法消失了......
我在连接 JSoup 时遇到问题。该代码在 Eclipse 上运行良好,但它不想在 android studio 上运行,它会抛出 android.os.NetworkOnMainThreadException 。
我正在尝试在新线程中运行 Timetable 类,但它仍然给我带来同样的错误。有什么想法吗?
public class Timetable implements Runnable {
/* code not showed for simplicity */
公共无效运行(){ System.out.println("正在运行");
try {
days.add(new Day("Monday"));
days.add(new Day("Tuesday"));
days.add(new Day("Wednesday"));
days.add(new Day("Thursday"));
days.add(new Day("Friday"));
days.add(new Day("Saturday"));
days.add(new Day("Sunday"));
for (int y = 1; y <= 7; y++) {
for (int z = 1; z <= 56; z += 4) {
System.out.println("ATTEMPT NUMBER " + y + " " + z);
Document doc = Jsoup.connect("http://timetables.cit.ie:70/reporting/Individual;Student+Set;name;" + classgroup + "%0D%0A?weeks=" + weeks + "&days=" + y + "&periods=" + z + "&height=100&width=100").get();
String title = doc.title();
String css_path = "body > table > tbody > tr:nth-child(6) > td > table:nth-child(2) > tbody";
Elements tBody = doc.select(css_path);
String[] parts = tBody.text().split("\\s+");
if (parts.length > 3) {
for (Day d : days) {
if (parts[0].compareToIgnoreCase(d.getDayOfWeek()) == 0) {
String startTime = parts[1];
String module = parts[2];
String roomNumber = "";
if (parts.length > 3) {
for (int x = 3; x < parts.length; x++) {
if (parts[x].length() == 1) {
module += " " + parts[x];
} else if (parts[x].length() == 2 && (parts[x].charAt(1) != '0' || parts[x].charAt(1) != '1' || parts[x].charAt(1) != '2' || parts[x].charAt(1) != '3' || parts[x].charAt(1) != '4' || parts[x].charAt(1) != '5' || parts[x].charAt(1) != '6' || parts[x].charAt(1) != '7' || parts[x].charAt(1) != '8' || parts[x].charAt(1) != '9')) {
module += " " + parts[x];
} else {
if (parts[x].charAt(1) == '0' || parts[x].charAt(1) == '1' || parts[x].charAt(1) == '2' || parts[x].charAt(1) == '3' || parts[x].charAt(1) == '4' || parts[x].charAt(2) == '0' || parts[x].charAt(2) == '1' || parts[x].charAt(2) == '2' || parts[x].charAt(2) == '3' || parts[x].charAt(2) == '4' || parts[x].charAt(2) == '5' || parts[x].charAt(2) == '6' || parts[x].charAt(2) == '7' || parts[x].charAt(2) == '8' || parts[x].charAt(2) == '9') {
roomNumber = parts[x];
} else if (parts[x].charAt(0) == 'w' && parts[x].charAt(1) == 'k') {
} else module += " " + parts[x];
}
}
Timeslot t = new Timeslot(startTime, module, roomNumber);
d.addTimeslot(t);
}
}
}
}
}
}
}catch (Exception e)
{
System.out.println("Failed Failed");
}
}
}
在主要的java活动中
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Timetable t = null;
try {
t = new Timetable("CO.DNET3", 2);
} catch (IOException e) {
e.printStackTrace();
}
Thread download = new Thread(t);
t.run();
【问题讨论】:
-
太神奇了,我也犯了同样的错误...-.-