gpt4 book ai didi

java - 尝试连接 jsoup android 时出现 android.os.NetworkOnMainThreadException

转载 作者:行者123 更新时间:2023-12-01 22:26:08 29 4
gpt4 key购买 nike

在我的代码中,我尝试多次连接到该网站,以便每次尝试连接时从表中提取一条记录。

我尝试一次提取所有记录,然后将其解析为字符串,但是不同类(class)组的每个时间表都有一点不同的间距,并且算法消失了......

我在连接 JSoup 时遇到问题。该代码在 Eclipse 上完美运行,但它不想在 android studio 上运行,并且会抛出 android.os.NetworkOnMainThreadException 。

我试图在新线程中运行 Timetable 类,但它仍然给我带来同样的错误。有什么想法吗?

    public class Timetable implements Runnable {
/* code not showed for simplicity */

公共(public)无效运行(){ 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 Activity 中

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();

最佳答案

这里

t.run();

使用 Timetable 类的 t 对象调用 run 方法,而不是启动 Thread

调用Thread.start() :

download.start();

关于java - 尝试连接 jsoup android 时出现 android.os.NetworkOnMainThreadException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28751719/

29 4 0
文章推荐: java - 如何使用内部有另一个策略的策略来实现策略模式?
文章推荐: java - 不兼容的类型 : list cannot be converted to List