JSOUP等待页面解析

时间:2016-06-08 18:00:07

标签: java android jsoup

我有一个JSOUP登录程序,可以登录网站并从页面中获取信息。它运行良好,但是由于JSOUP需要一段时间,因此需要大约3秒的时间将信息解析为ArrayLists。

我还检查是否正确加载了正确的页面。 (它只是检查ArrayLists以查看它们是否为空意味着页面未被加载)

public void onClick(View v) {

            SourcePage sp = new SourcePage(user.getText().toString(), pass.getText().toString());

            if(sp.isConnected()) { //Refer to the bottom of the next code box

                Toast.makeText(getApplicationContext(), sp.getGradeLetters().get(0), Toast.LENGTH_SHORT).show();
                startActivity(new Intent(MainActivity.this, gradepage.class));
            }else {

                Toast.makeText(getApplicationContext(), "Login Failed", Toast.LENGTH_SHORT).show();
            }


        }
private void login() {

    Thread th = new Thread() {

        public void run() {

            try {

                HashMap<String, String> cookies = new HashMap<>();
                HashMap<String, String> formData = new HashMap<>();


                Connection.Response loginForm = Jsoup.connect(URL)
                        .method(Connection.Method.GET)
                        .userAgent(userAgent)
                        .execute();

                Document loginDoc = loginForm.parse();

                String pstoken = loginDoc.select("#LoginForm > input[type=\"hidden\"]:nth-child(1)").first().attr("value");
                String contextData = loginDoc.select("#contextData").first().attr("value");
                String dbpw = loginDoc.select("#LoginForm > input[type=\"hidden\"]:nth-child(3)").first().attr("value");
                String serviceName = "PS Parent Portal";
                String credentialType = "User Id and Password Credential";


                cookies.putAll(loginForm.cookies());


                //Inserting all hidden form data things
                formData.put("pstoken", pstoken);
                formData.put("contextData", contextData);
                formData.put("dbpw", dbpw);
                formData.put("serviceName", serviceName);
                formData.put("credentialType", credentialType);
                formData.put("Account", USERNAME);
                formData.put("ldappassword", PASSWORD);
                formData.put("pw", PASSWORD);


                Connection.Response homePage = Jsoup.connect(POST_URL)
                        .cookies(cookies)
                        .data(formData)
                        .method(Connection.Method.POST)
                        .userAgent(userAgent)
                        .execute();


                mainDoc = Jsoup.parse(homePage.parse().html());

                //Get persons name
                NAME = mainDoc.select("div#sps-stdemo-non-conf").select("h1").first().text();

                //Getting Grades for Semester 2
                Elements grades = mainDoc.select("td.colorMyGrade").select("[href*='fg=S2']");
                System.out.println(grades);
                for (Element j : grades)
                {

                    if (!j.text().equals("--")) {
                        String gradeText = j.text();
                        gradeLetter.add(gradeText.substring(0, gradeText.indexOf(" ")));
                        gradeNumber.add(Double.parseDouble(gradeText.substring(gradeText.indexOf(" ") + 1)));
                    }
                }

                Elements teachers = mainDoc.select("td[align='left']");
                for (int i = 1; i < teachers.size(); i += 2)
                {

                    String fullText = teachers.get(i).text().replaceAll("//s+", ".");
                    teacherList.add(fullText);


                }

            }catch (IOException e) {
                System.out.println(e);
            }

        }
    };

    th.start();


}

public boolean isConnected() {
    return (!(gradeLetter.isEmpty() || gradeNumber.isEmpty() || teacherList.isEmpty()));
}

最大的问题是程序(onClick)正在给Toast&#34;登录失败&#34;因为isConnected方法不等待页面加载。我该如何解决这个问题?

0 个答案:

没有答案