三、User 用户数据的数据类型
1、用户数据
private String account=”未登录”;
private String password;
private String name=”没有名称”;
private String phone;
private String mail;
2、将账户名和密码数据输入User
public User (String account,String password)
3、get/set函数
public class User {
private String account="未登录";
private String password;
private String name="没有名称";
private String phone;
private String mail;
private static String JSON_ACCOUNT="account";
private static String JSON_PASSWORD="password";
private static final String masterPassword = "FORYOU"; // AES加密算法的种子
public User (String account,String password){
this.account=account;
this.password=password;
}
public String GetAccount(){ return account;}
public String GetName(){return name;}
public String GetMail(){ return mail;}
public String GetPassword(){
return password;
}
public String GetPhone(){
return phone;
}
public void SetAccount(String account){
this.account=account;
}
public void SetName(String name){
this.name=name;
}
public void SetMail(String mail){
this.mail=mail;
}
public void SetPassword(String password){
this.password=password;
}
public void SetPhone(String phone){
this.phone=phone;
}
public User(JSONObject json) throws Exception {
if (json.has(JSON_ACCOUNT)) {
account = json.getString(JSON_ACCOUNT);
password = json.getString(JSON_PASSWORD);
}
}
public JSONObject toJSON() throws Exception {
JSONObject json = new JSONObject();
try {
json.put(JSON_ACCOUNT, account);
json.put(JSON_PASSWORD, password);
} catch (JSONException e) {
e.printStackTrace();
}
return json;
}
}
四、LoginUser implements View.OnClickListener, AdapterView.OnItemClickListener , PopupWindow.OnDismissListener
静态数据存储用户信息,与界面显示关联
1、用户数据
public static String account=”账户”;
public static String password=”密码”;
public static String name=”用户名”;
public static String phone=”电话”;
public static String mail=”邮箱”;
public class LoginUser {
public static String account="账户";
public static String password="密码";
public static String name="用户名";
public static String phone="电话";
public static String mail="邮箱";
}
五、UserLoginSave 保存用户登录信息
1、保存用户登录信息列表
把User数据包装成JsonArray数组
public static void saveUserList(Context context, ArrayList<User>
users)
2、获取用户登录信息列表
public static ArrayList<User>
getUserList(Context context)
/**
*保存用户登录信息
*/
public class UserLoginSave {
private static final String LOGIN_FILE="userinfo.json";
private static final String TAG="UserLoginSave";
/**
* 保存用户登录信息列表
* 把User数据包装成JsonArray数组
* */
public static void saveUserList(Context context, ArrayList<User> users)
throws Exception {
Log.i(TAG, "正在保存");
Writer writer=null;
OutputStream out;
JSONArray array=new JSONArray();
for (User user:users){
array.put(user.toJSON());
}
try{
out=context.openFileOutput(LOGIN_FILE,Context.MODE_PRIVATE);//覆盖
writer=new OutputStreamWriter(out);
Log.i(TAG, "json的值:" + array.toString());
writer.write(array.toString());
}finally {
if (writer!=null){
writer.close();
}
}
}
/**
* 获取用户登录信息列表 */
public static ArrayList<User> getUserList(Context context){
/* 加载 */
FileInputStream in;
ArrayList<User> users=new ArrayList<User>();
try{
in=context.openFileInput(LOGIN_FILE);
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder jsonString = new StringBuilder();
JSONArray jsonArray;
String line;
while ((line = reader.readLine()) != null) {
jsonString.append(line);
}
Log.i(TAG, jsonString.toString());
jsonArray = (JSONArray) new JSONTokener(jsonString.toString()).nextValue();
// 把字符串转换成JSONArray对象
for (int i = 0; i < jsonArray.length(); i++) {
User user = new User(jsonArray.getJSONObject(i));
users.add(user);
}
}catch (FileNotFoundException e){
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return users;
}
}