3

I am trying to do this, and I really need help from expirienced fellows.

  1. App with 2 texboxes. One for username, and one for password.
  2. Button for login.
  3. When user press button, login information is sent to webpage (m.bonbon.hr), and that webpage is opened in browser.
  4. After first login, login information is saved so that user doesn't have to enter that information again.

Where to start from, please any guidelines, advices, i'll take anything.

EDIT:

I created this in my main acctivity

DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost httpost = new HttpPost("https://www.bonbon.hr/registracija?direct=1");

      try {
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("email", "login"));
        nvps.add(new BasicNameValuePair("password", "pw"));
        nvps.add(new BasicNameValuePair("autologin", "true"));
        httpost.setEntity(new UrlEncodedFormEntity(nvps));

        }
        catch (UnsupportedEncodingException e) {
            // writing error to Log
            e.printStackTrace();
        }
        try {
            HttpResponse response = httpclient.execute(httpost);

            // writing response to log
            Log.d("Http Response:", response.toString());

        } catch (ClientProtocolException e) {
            // writing exception to log
            e.printStackTrace();

        } catch (IOException e) {
            // writing exception to log
            e.printStackTrace();
        }

Under the onCreate part. So now , when i click on my button (already created and ready for use) i need to login to given URL with given information... But I don't know how to makle it happen using asyntask :(

Goran
  • 1,239
  • 4
  • 23
  • 36

2 Answers2

0

It is the exact same thing as logging in from a browser: you just need to send a POST request to the correct url. Once the user is logged in, its session cookie is stored in the http client.

tfjgeorge
  • 101
0

Design the layout of the appliaction with the two text boxes and one button to login.

Now, when that button is pressed get the text entered in the two TextBoxes using getText() and make POST request to the webpage URL and maintain a session using HttpClient.

If you are planning to save user login information, SharedPreferences is the best way to do it and you can store the username and the password in the SharedPreference variables and use them later to make later requests to log in.

And in case you want to know more about how to use HttpClient to establish a session, this question would provide you with a lot of answers.

Community
  • 1
  • 1
Swayam
  • 16,294
  • 14
  • 64
  • 102
  • I have a questions about httpclient and post. – Goran Sep 23 '12 at 19:31
  • In this part of code, params.add(new BasicNameValuePair("user", "kris")); and params.add(new BasicNameValuePair("pass", "xyz")); ... What is user and pass ?? Is it id of field int targeted web page, or ?? – Goran Sep 23 '12 at 19:34
  • To make any request to a web page, you would need some parameters or fields. The "user" and "pass" are the names of the fields that the web page takes on a request. – Swayam Sep 23 '12 at 19:39
  • See this http://stackoverflow.com/questions/2959316/how-to-add-parameters-to-a-http-get-request-in-android to understand more about the parameters. – Swayam Sep 23 '12 at 19:40
  • 1
    So, i just need to open my webpage in browser, i click "view page source" and i just find what is the id of fields that are used for login to that webpage ?? – Goran Sep 23 '12 at 19:40
  • Yes, sir! Exactly! That should do! – Swayam Sep 23 '12 at 19:41
  • Ok, I understand that part. If I may ask another question, sorry if I am boring. On one tutorial about httpclient I saw this comment -> In latest of version of android you can't send a http request on MainThread. You should use Async task send request.... I am not quite sure I understand it :( – Goran Sep 23 '12 at 19:45
  • No, no. It's okay to have doubts. And yeah, whatever you read there is quite true. Android 4+ does not allow you to do network activity on the main ui thread, it throws NetworkOnMainThread error. Previous versions of Android allowed you to do so, but it is not a good practice, as if the network takes too long then the application UI becomes unresponsive and can get killed by the Android OS. So, you need to do the network activity on a separate thread and AsyncTask is the solution to it. – Swayam Sep 23 '12 at 19:49
  • Can you please explain that little better. Ok i have situation like this. My app has one main layout with few buttons. Every button does it's own work. So now i am trying to add one more button, and when user press this new button i need to send their username and pass (stored in app ) to one https webpage, and open this webpage afterwards. – Goran Sep 23 '12 at 19:55
  • Have a look at this question and my answer. http://stackoverflow.com/questions/12521456/android-app-crashes-when-trying-to-get-txt-file-from-webserver – Swayam Sep 24 '12 at 12:54
  • Define an AsyncTask which would make the http request when the button is pressed. `execute()` this AsyncTask inside the onClick() of the button. That way the network activity takes place on a separate thread and does not make the UI unresponsive. – Swayam Sep 24 '12 at 12:57
  • Please just tell me, where do I define http client ?? in the main activity or ?? – Goran Sep 25 '12 at 20:34
  • The client can be defined anywhere. Only the call to the network needs to be in the AsyncTask. You can make the client as a data member of your MainActivity class and then use it inside the doInBackground of the AsyncTask. – Swayam Sep 26 '12 at 14:21
  • Please refer to main question. I edited with part i done by now. Can you help me now to create this async task, under my onClick button part ?? Please. – Goran Sep 26 '12 at 23:24
  • Put the whole code snippet that you have given here and put it inside `doInBackground()` of the AsyncTask. And refer to this to know about the structure of your AsyncTask. http://stackoverflow.com/questions/12521456/android-app-crashes-when-trying-to-get-txt-file-from-webserver – Swayam Sep 27 '12 at 09:24
  • A more save way to save user credentials (username, password) would be to use EncryptedSharedPreferences instead: https://developer.android.com/jetpack/androidx/releases/security – C.Schone Apr 13 '21 at 15:24