0

I want to try to check my login program with Jsoup in Android Studio. But Something is wrong. At first I'm login with "Connection.Response". and then after login I get information and put it to element. but when i check with "if" it is not working. How can i check if login was successful? please help me.

this is my code

public class MainActivity extends Activity {
Button b1, b2;
EditText ed1, ed2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    b1 = (Button) findViewById(R.id.button);
    ed1 = (EditText) findViewById(R.id.editText);
    ed2 = (EditText) findViewById(R.id.editText2);
    b2 = (Button) findViewById(R.id.button2);

    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String cheak = null;
            Connection.Response response;
            try {
                response = Jsoup.connect("https://ecampus.smu.ac.kr/login/index.php")
                        .data("username", ed1.getText().toString(), "password", ed2.getText().toString())
                        .method(Connection.Method.GET)
                        .timeout(1000)
                        .execute();
                Document doc = Jsoup.connect("http://ecampus.smu.ac.kr/").cookies(response.cookies()).get();
                Elements element = doc.select(".course_subject");
                cheak = element.toString();

                if (cheak != null) {
                    Toast.makeText(getApplicationContext(),
                            "Username and password is correct", Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(MainActivity.this, Next.class));
                } else {
                    Toast.makeText(getApplicationContext(),
                            "Username and password is NOT correct", Toast.LENGTH_SHORT).show();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
    b2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });
}

}

Behzod Muslimov
  • 301
  • 1
  • 2
  • 7
  • *How can i check if login was successful?* for the start, not on main/UI thread – Selvin Nov 28 '16 at 16:32
  • i'm sorry but i don't understand your answer. @Selvin – Behzod Muslimov Nov 28 '16 at 16:39
  • Does this code works for you in standalone Java application with predefined values for username and password? Also have you tried adding headers for client agent? – Pshemo Nov 28 '16 at 16:39
  • "*i'm sorry but i don't understand your answer*" that wasn't answer but comment. Anyway I am not Android developer but if it is similar to swing in a way that there is only one thread which handles UI update and user events (like pressing a button) then it is not good idea to add to it tasks which may take very long time because it will give user impression that application froze while handling such long task. It is better to delegate such tasks to different thread and then it is done it should update UI. – Pshemo Nov 28 '16 at 16:43
  • Thank you for your explain))) I understand what do you mean. I think i need to create new class for login. and process should be in this class and check it. Am i right @Pshemo – Behzod Muslimov Nov 28 '16 at 16:54
  • Yes, that is general idea (although I am not sure how to implement it properly in Android). But that is another problem. Lets get back to ability to login on site. Some sites don't want to handle requests from non-browser clients and if such client will not send `user-agent` header with proper browser name, then it is most likely not using browser so it will not be handled normally. Also `referrer` header could be needed. More info: http://stackoverflow.com/questions/6581655/jsoup-useragent-how-to-set-it-right – Pshemo Nov 28 '16 at 17:00
  • Thank you very much for your help)) I will check it @Pshemo – Behzod Muslimov Nov 28 '16 at 17:08

0 Answers0