登陆、注册、用户信息等接口文档与示例

本来写在数据库的接口示例里面,想了想感觉不太妥当,所以新开博客。

目录

登陆、注册
用户信息


登陆、注册

使用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时不会得到设置的值
posted @ 2018-07-15 12:27  HeartTrace  阅读(3670)  评论(0编辑  收藏  举报