本來寫在數據庫的接口示例里面,想了想感覺不太妥當,所以新開博客。
目錄
登陸、注冊
使用com.example.dell.auth.ServerAuthenticator類
接口:
static public boolean signIn(String username, String password, Bundle bundle):登陸。參數中username與password為用戶名與密碼,bundle用於返回服務器的結果;返回值表示能否與服務器通信,為true表示與服務器通信,為false表示不能與服務器通信;假如返回值為true,bundle中有屬性boolean success表示是否成功登陸,String msg表示服務器返回的信息,若屬性boolean success為true,bundle中還有屬性String token表示token(詳見Example)
static public boolean signUp(String username, String password, Bundle bundle):注冊。參數中username與password為用戶名與密碼,bundle用於返回服務器的結果;返回值表示能否與服務器通信,為true表示與服務器通信,為false表示不能與服務器通信;假如返回值為true,bundle中有屬性boolean success表示是否成功注冊,String msg表示服務器返回的信息(詳見Example)。
Example. 登陸
前提:name passwd是已有的字符串
Bundle bundle = new Bundle();
boolean status = ServerAuthenticator.signIn(name, passwd, bundle);
if(status == false) {
// 接口錯誤或者網絡錯誤
}
else {
boolean success = bundle.getBoolean("success");
String msg = bundle.getString("msg");
// success表示操作成功與否;msg表示服務器返回信息
if(success) {
// 只有success時才有token
String token = bundle.getString("token");
}
}
Example. 注冊
前提:name passwd是已有的字符串
Bundle bundle = new Bundle();
boolean status = ServerAuthenticator.signUp(name, passwd, bundle);
if(status == false) {
// 接口錯誤或者網絡錯誤
}
else {
boolean success = bundle.getBoolean("success");
String msg = bundle.getString("msg");
// success表示操作成功與否;msg表示服務器返回信息
}
用戶信息
使用com.example.dell.auth.MyAccount類
字段:
- String name(用戶名)
- String token(token)
- String nickname(昵稱)
- String gender(性別)
- String birthday(生日)
- String email(郵箱)
- String school(學校)
- String signature(個性簽名)
- int image(頭像)
接口:
static public MyAccount get(Context context):用於獲得一個MyAccount對象。由於賬戶有唯一性,這個類不允許直接new,只能通過這個方法獲得唯一的一個對象。即,不管在什么地方,調用這個函數獲得的都是同一個對象。
getter and setter:上面那些字段的getter和setter。詳略。
save:為了減少文件操作,如果修改了字段,只有調用這個函數才會保存當前的所有屬性,否則不會保存。
Example. 獲取用戶信息
MyAccount myAccount = MyAccount.get(getContext());
String name = myAccount.getName(); // 類似可以調用其他getter方法獲取其他屬性
Example. 編輯用戶信息
MyAccount myAccount = MyAccount.get(getContext());
myAccount.setName("hello");
myAccount.setSchool("USTC");
myAccount.save(); // 假如不save,這些信息不會保存,即本次還是能夠得到設置的值,但下次打開app時不會得到設置的值