关于尝试连接jsoup android的android.os.NetworkOnMainThreadException

时间:2015-02-26 20:03:52

标签: java android jsoup

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

我尝试一次拉出所有记录,然后将其解析为字符串,但不同类组的每个时间表都会出现一点点不同的间距,算法就消失了......

我遇到连接JSoup的问题。该代码完全适用于Eclipse,但它不想在android studio上工作,它会抛出android.os.NetworkOnMainThreadException。

我正在尝试在新线程中运行Timetable类,但它仍然会给我带来同样的错误。任何想法?

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

public void run(){         的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();

1 个答案:

答案 0 :(得分:3)

下面

t.run();

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

致电Thread.start()

download.start();