原本写在数据库的接口示例里面,想了想感受不太稳当,因此新开博客。java
使用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)app
static public boolean signUp(String username, String password, Bundle bundle):注册。参数中username与password为用户名与密码,bundle用于返回服务器的结果;返回值表示可否与服务器通讯,为true表示与服务器通讯,为false表示不能与服务器通讯;假如返回值为true,bundle中有属性boolean success表示是否成功注册,String msg表示服务器返回的信息(详见Example)。函数
前提:name passwd是已有的字符串spa
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"); } }
前提:name passwd是已有的字符串code
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类对象
字段:token
接口:
static public MyAccount get(Context context):用于得到一个MyAccount对象。因为帐户有惟一性,这个类不容许直接new,只能经过这个方法得到惟一的一个对象。即,无论在什么地方,调用这个函数得到的都是同一个对象。
getter and setter:上面那些字段的getter和setter。详略。
save:为了减小文件操做,若是修改了字段,只有调用这个函数才会保存当前的全部属性,不然不会保存。
MyAccount myAccount = MyAccount.get(getContext()); String name = myAccount.getName(); // 相似能够调用其余getter方法获取其余属性
MyAccount myAccount = MyAccount.get(getContext()); myAccount.setName("hello"); myAccount.setSchool("USTC"); myAccount.save(); // 假如不save,这些信息不会保存,即本次仍是可以获得设置的值,但下次打开app时不会获得设置的值