有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java 安卓。操作系统。NetworkOnMainThread尝试与jsoup 安卓连接时出现异常

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

我试着一次提取一个all记录,然后将其解析为字符串,但不同类组的每个时间表都会产生一点不同的间距,算法就不存在了

我与JSoup的连接有问题。这段代码在Eclipse上运行得很好,但它不想在安卓 studio上运行,它抛出了安卓。操作系统。NetworkOnMainThread异常

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

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

公开募捐{ 系统出来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();

共 (1) 个答案

  1. # 1 楼答案

    这里

    t.run();
    

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

    调用Thread.start()

    download.start();