如何检查登录状态?

时间:2010-11-15 23:21:02

标签: android post login

我必须使用我的应用程序登录。 登录后,如果正确,我想显示一个tabview:

  • 我的统计
  • 上传照片
  • MapView类

这是我的登录活动代码

public class LoginActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    String url, user, pwd, user_field, pwd_field;

    url = "http://myurl.com/login/";

    user_field = "username";
    pwd_field = "password";
    user = "myuser";
    pwd = "mypass";

    List<NameValuePair> myList = new ArrayList<NameValuePair>(2);
    myList.add(new BasicNameValuePair(user_field, user)); 
    myList.add(new BasicNameValuePair(pwd_field, pwd));

    final HttpParams params = new BasicHttpParams();
    final HttpClient client = new DefaultHttpClient(params);
    final HttpPost post = new HttpPost(url);
    //final HttpResponse end = null;
    //String endResult = null;

    Button login_button = (Button)findViewById(R.id.login_button);

    try {
        post.setEntity(new UrlEncodedFormEntity(myList));
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 

    login_button.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            try {
                HttpResponse response = client.execute(post);
                //HttpResponse end = response;
                System.out.println(response.getStatusLine());
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }  
        }
    });
}

}

2 个答案:

答案 0 :(得分:0)

创建一个login.php脚本,您可以在服务器上调用该脚本并将您的身份验证发布到脚本中,让脚本进行身份验证并将结果发送回您的应用程序。

基本上,您的应用程序会将身份验证发布到login.php脚本,然后脚本将返回您的应用程序,无论它是否成功。

答案 1 :(得分:0)

我假设您想在应用首次启动时使用“startActivityForResult”调用您的登录活动。您可以使用此骨架并填充空白:

public class YourLoginActivity extends Activity {

private ProgressDialog executingDialog;
private Button loginButton;
private EditText username, password;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.login);

    loginButton = (Button) this.findViewById(R.newTransaction.loginButton);
    loginButton.setOnClickListener(new LoginButtonListener());

    username = (EditText) findViewById(R.login.username);
    password = (EditText) findViewById(R.login.password);
}


private class LoginButtonListener implements OnClickListener {
    @Override 
    public void onClick(View v) {

        executingDialog = ProgressDialog.show(this, "", getString(R.string.message_executingLogin), true);

        new Thread() {
            public void run() {

                try {

                    HttpClient httpClient = HttpClient.getInstance();
                    Response response = httpClient.doLogin(username.getText().toString(), password.getText().toString());

                    if(response.getCode() == 200){
                        // TODO: insert more checks, e.g. validating the answer here!
                        handler.post(loginDone);
                    }
                    else {
                        // TODO: handle error codes etc.
                        handler.post(loginFailed);
                    }

                } catch (Exception e) {
                    Log.e(TAG, "", e);
                    handler.post(loginFailed);
                }
            }
        }.start();
    }
}

private final Runnable loginDone = new Runnable() {
    public void run() {
        executingDialog.dismiss();
        setResult(RESULT_OK);
        finish();
    }
};

private final Runnable loginFailed = new Runnable() {
    public void run() {
        executingDialog.dismiss();
        // TODO: show error dialog
    }
};

我建议在HttpClient周围实现一个单例包装器。如果您使用登录视图拦截启动,请确保向其添加“android:noHistory”属性。