1

I have two activities.

The main activity runs the app and initialises the http get request code and parses the response from JSON into a string.

The getmethod activity uses a http get method to connect to the server and sends the response back to my main activity.

How can I create a log in method, where the user enters their username and password manually and this is passed onto the get method?

Lyan Rai
  • 23
  • 2
  • 6

1 Answers1

1

You can add this code to your main activity - it will do all the heavy lifting for you.

/**
 * Represents an asynchronous login/registration task used to authenticate
 * the user.
 */
public class UserLoginTask extends AsyncTask<String, Void, Boolean> {

    @Override
    protected void onPostExecute(final Boolean success) {
        if (success == true) {
            //Do whatever your app does after login

        } else {
            //Let user know login has failed
        }
    }

    @Override
    protected Boolean doInBackground(String... login) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(
                "YOUR_ADDRESS_HERE.COM");
        String str = null;
        String username = login[0];
        String password = login[1];

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("username", username));
        nameValuePairs.add(new BasicNameValuePair("password", password));
        try {
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
            return false;
        }

        try {
            HttpResponse response = httpclient.execute(httppost);
            str = EntityUtils.toString(response.getEntity());

        } catch (IOException e) {
            e.printStackTrace();
        }

                    //Whatever parsing you need to do on the response
                    //This is an example if the webservice just passes back a String of "true or "false"

        if (str.trim().equals("true")) {

            return true;
        } else {
            return false;

        }
    }

You can create this object by:

  UserLoginTask mAuthTask = new UserLoginTask();

Start the request with (perhaps put in an OnClick event from a login button?):

mAuthTask.execute(mUsername, mPassword);
Henry
  • 486
  • 1
  • 5
  • 16
  • but for me it does not allow to pass (String.. params) to the method , only void works – murali kurapati Jun 25 '15 at 11:14
  • I'm not sure about that - that overload (String...) mean you pass each individual string in as a parameter and the receiving method will get it as a array of all of the String you put in the method call. Are you sure you are calling it correctly? – Henry Jun 28 '15 at 05:34
  • thank u, i made a mistake i did not fill with proper template, my one was public class UserLoginTask extends AsyncTask which should be public class UserLoginTask extends AsyncTask – murali kurapati Jun 29 '15 at 09:22