0
我試圖做一個簡單的方法來檢查用戶的保存的登錄憑據是正確的......到目前爲止,我有:Android的檢查如果登錄
public boolean checkLogin() {
final SharedPreferences prefs = this.getSharedPreferences(AndroidGPSTrackingActivity.class.getSimpleName(),
this.MODE_PRIVATE);
String userName = prefs.getString("userName", "");
if (userName.isEmpty()) {
Log.i("UserName", "UserName not found.");
return false;
}
String password = prefs.getString("password", "");
if (password.isEmpty()) {
Log.i("Password", "Password not found.");
return false;
}
mCheckTask = new CheckSavedLogin(userName, password, this);
mCheckTask.execute((Void) null);
return mCheckTask.execute((Void) null);//make this return true/false
}
也:
public class CheckSavedLogin extends AsyncTask<Void, Void, Boolean> {
public final String mUserName;
public final String mPassword;
public boolean success;
public Context context;
CheckSavedLogin(String userName, String password, Context context) {
mUserName = userName;
mPassword = password;
this.context = context.getApplicationContext();
}
protected Boolean doInBackground(Void... params) {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://173.242.94.66/scripts/appLogin.php");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("userName", mUserName));
nameValuePairs.add(new BasicNameValuePair("password", mPassword));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
String responseStr = EntityUtils.toString(response.getEntity());
JSONObject json = new JSONObject(responseStr);
success = json.getBoolean("success");
Log.d("Response", responseStr);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return success;
}
@Override
protected void onPostExecute(final Boolean success) {
mAuthTask = null;
showProgress(false);
if (success) {
storeLoginDetails(mUserName, mPassword);
//RETURN TRUE HERE?
}
}
@Override
protected void onCancelled() {
mAuthTask = null;
showProgress(false);
}
}
我只是不知道如何使這一切都返回「真」或「虛假」的基礎上的迴應
嗨!爲什麼不使用'sessionToken'來檢查你是否已經登錄? –