2016-08-24 34 views
0

你好,我是用dynamodb,我想知道如何维护会话时英寸如何存储用户的细节作为会话

用户登录以下是我的登录代码

Runnable runnable = new Runnable() { 
     public void run() { 
      Users user = mapper.load(Users.class,username); 

      System.out.println(user.getPassword()); 
      System.out.println(username); 
      if (user != null && user.getPassword().equals(password)){ 
       //System.out.println("Correct"); 
       runOnUiThread(new Runnable() { 

        @Override 
        public void run() { 
         setContentView(R.layout.activity_account); 
        } 
       }); 
      } 

这是我的用户类,我设置并获取所有实体

public class Users { 

private String id; 
private String email; 
private String username; 
private String password; 
private String role; 


@DynamoDBHashKey(attributeName = "id") 
public String getId() { 
    return id; 
} 

public void setId(String id) { 
    this.id = id; 
} 

@DynamoDBAttribute(attributeName = "email") 
public String getEmail() { 
    return email; 
} 

public void setEmail(String email) { 
    this.email = email; 
} 

对于所有其他变量也是如此。

请看看这些代码,并告诉我如何我可以存储会议,因为我想登录的用户的ID。

+0

请参阅本http://stackoverflow.com/questions/23024831/android-shared-preferences-example – vinoth12594

+0

是的,共享的偏好将是你的好问题,上面的链接很好,跟着它 –

回答

2

V1

可以使用SharedPreferences像下面

public class Prefs { 

public static void putPref(String key, String value, Context context){ 
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); 
    SharedPreferences.Editor editor = prefs.edit(); 
    editor.putString(key, value); 
    editor.commit(); 
} 

public static String getPref(String key, Context context) { 
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); 
    return preferences.getString(key, null); 
} 
} 

您可以选择存储

Prefs.putPref("the key","the value",your context); 

后能得到你的存储数据后

String userId = Prefs.getPref("the key",your context); 

你可以在你的应用程序中使用它。

V2

使用GSON 添加到您的build.gradle

compile 'com.google.code.gson:gson:1.7.2' 

和用户对象转换成JSON这样

Gson gson = new Gson(); 

User user = ...; 
String json = gson.toJson(user); 
存储用户后

Prefs.putPref("user",json,your context); 

,让您的用户

String json = = Prefs.getPref("user",your context); 

User user = null; 
if(json != null) 
user = gson.fromJson(json,User.class); 

多数民众赞成