我想爲我的android應用程序做一個登錄腳本,腳本將發送我的電子郵件和密碼到PHP服務器,驗證登錄,然後創建一個PHP會話,以便用戶停留登錄。這是我的代碼,使用PHP會話與我的Android應用程序登錄
HttpPost httppost = new HttpPost("http://server.com/login.php");
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
public String login() {
String userID = "";
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("email", "[email protected]"));
nameValuePairs.add(new BasicNameValuePair("password", "admin"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
userID = EntityUtils.toString(response.getEntity());
//Log.v("Login response", "" + userID);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
return userID;
}
這個腳本成功地將數據發送到我的服務器和我的PHP成功登錄的用戶。我已經放置了「HttpClient httpclient = new DefaultHttpClient();」我的主要登錄方法之外。這有助於存儲會話,直到我呼叫另一個班級,然後再重新設置會話。所以我想知道如何改變代碼,以便「httpclient」以某種方式存儲,這樣我可以保持會話並保持登錄到我的服務器。謝謝!
我想你可以保存的SessionID在'SharedPreferences'每次你需要發送一次請求剛剛從那裏 – monim 2015-11-06 15:22:57