0

I have a JSOUP Login program that logs into a website and grabs info from the page. It works well, but it takes ~3 seconds for the information to be parsed into ArrayLists as JSOUP takes a while.

I also have a check to see if the correct page is loaded correctly. (It's just checking the ArrayLists to see if they are empty meaning the page isn't loaded)

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

The big problem is that the program (onClick) is giving the Toast "Login Failed" because the isConnected method doesn't wait for the page to load. How can I fix this?

Jop
  • 90
  • 11
  • It looks like you need to set some bigger (or unlimited -> `0`) timeout: http://stackoverflow.com/questions/6571548/i-get-a-sockettimeoutexception-in-jsoup-read-timed-out. Also `//s+` in `replaceAll` should probably be `\\s+` if you want to replace one or more whitespaces. – Pshemo Jun 08 '16 at 18:23
  • Try to give Thread.sleep(2000).Which will give your Program 2 seconds to wait for the loading.But this is not the very best implemetation ,to do that kind of work – Rahal Kanishka Jun 16 '16 at 17:55

0 Answers0