Android检查如果登录

时间:2014-03-18 20:29:34

标签: android android-asynctask return

我试图制作一个简单的方法来检查用户保存的登录凭据是否正确...到目前为止,我已经:

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);
        }
    }

我无法弄清楚如何让这一切全部回归&#34; true&#34;或&#34;假&#34;基于反响

0 个答案:

没有答案
相关问题