Android 如何检查登录状态?
我必须用我的应用程序登录。 登录后,如果正确,我想显示一个选项卡视图,其中包含: 我的数据 上传照片 地图视图 这是我的登录活动代码Android 如何检查登录状态?,android,post,login,Android,Post,Login,我必须用我的应用程序登录。 登录后,如果正确,我想显示一个选项卡视图,其中包含: 我的数据 上传照片 地图视图 这是我的登录活动代码 public class LoginActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedIns
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();
}
}
});
}
}创建一个login.php脚本,您可以在服务器上调用该脚本,并将身份验证发布到该脚本,让该脚本执行身份验证并将结果发送回应用程序
因此,基本上,您的应用程序会将身份验证发布到login.php脚本,然后无论是否成功,脚本都会返回到您的应用程序。我假设您希望在应用程序第一次启动时使用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周围实现一个单例包装器。如果您使用登录视图拦截启动,请确保添加属性。如果我的建议有帮助,请告诉我。ThanksHow我可以保留会话并传递到3选项卡活动吗?我需要用饼干吗?还是Android自动管理它们?