I am trying to make a login script for my Android application, the script will send my email address and password to the PHP server, verify the login and then create a PHP session so that the user remains logged in. This is my code,
HttpPost httppost = new HttpPost("http://server.com/login.php");
HttpClient httpclient = new DefaultHttpClient();
public String login() {
String userID = "";
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("email", "e@e.com"));
nameValuePairs.add(new BasicNameValuePair("password", "admin"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
userID = EntityUtils.toString(response.getEntity());
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
return userID;
}
This script successfully sends data to my server, and my PHP successfully logs the user. I posted "HttpClient httpclient = new DefaultHttpClient ();" outside my main login method. This helped save the session until I called another class and then reset the session again. Therefore, I am wondering how I can change the code so that the "httpclient" is somehow saved, so I can maintain a session and stay on my server. Thank!