【问题标题】:Store json response array in to session将 json 响应数组存储到会话中
【发布时间】:2016-10-20 02:41:10
【问题描述】:

我的 LoginActivity 正在将用户名和密码发送到 URL 并获得成功状态。我想在会话中存储 json 响应,如用户 ID、姓名、电子邮件信息,并在 MainActivity 中检索它。我正在使用以下代码进行 json 响应。

JSONObject jObj = new JSONObject(response);
String userId = jObj.getString("user_id");

我的回答是

{"tag":"login",
 "error":false,
 "user_id":"5",
     "user":{"name":"Chandra shankar",
             "email":"chandra@gmail.com"
            }
}

下面是我在 Session.java 中的 SharedPreferences

public class Session {
    private SharedPreferences sp;
    private SharedPreferences.Editor spEditor;


    public Session(Context context) {
        sp = PreferenceManager.getDefaultSharedPreferences(context);

    }


    public boolean setLogin(boolean status) {
        spEditor = sp.edit();
        spEditor.putBoolean("is_logged_in", status);
        spEditor.commit();
        return true;
    }

    public boolean getLoggedIn() {
        return sp.getBoolean("is_logged_in", false);
    }

}

【问题讨论】:

  • SharedPreferences 并将其保存为具有私有访问权限的字符串
  • 兄弟,我是安卓新手,你能回答我的问题吗?我还需要在我的 MainActivity 中打印存储的数据。
  • 离线也能自动登录吗?
  • Bcoz 如果您进行登录,您只需将它们存储在 JSON 变量中,一旦会话时间到期,用户就会超时,清除变量并再次登录.. 变量可以是静态的跨度>
  • 是的,现在看到我的登录活动在我进入时具有用户名和密码,它将转到我的 index.php 并通过 JSON 获取状态和用户数据。如果用户 ID 不为空,我想将用户 ID、姓名、电子邮件存储到会话中并在 MainActivity 中显示。我已经在使用会话来检查用户是否登录,如果登录,它将转到 mainactivity,如果不是 loginactivtiy。请查看我的 JSON 回复

标签: java android json


【解决方案1】:

使用SharedPreferences

SharedPreferences preferences = this.getSharedPreferences("APP_NAME", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();

editor.putString("ID", userId).apply();

如果你想取回价值。

String id = preferences.getString("ID");

编辑

public class Session {
    private SharedPreferences sp;
    private SharedPreferences.Editor spEditor;
    private String ISLOGIN = "is_logged_in";
    private String USER_ID = "user_id";

    public Session(Context context) {
        sp = PreferenceManager.getDefaultSharedPreferences(context);

    }


    public boolean setLogin(boolean status) {
        spEditor = sp.edit();
        spEditor.putBoolean(ISLOGIN, status);
        //use apply for async process
        spEditor.apply();
        return status;
    }

    public boolean getLoggedIn() {
        return sp.getBoolean(ISLOGIN, false);
    }

    public void setUserId(String userid){
        sp.edit().putString(USER_ID,userid).apply();
    }


    //get user id
    public String getUserId(){
       return sp.getString(USER_ID);
    }

}

//在活动中

Session session = new Session(this);
String id = session.getUserId();

p/s:我推荐你使用这个TinyDBSharedPreferences

【讨论】:

  • 好的,谢谢您的回复。请查看我的代码,我只是添加了我正在使用的 SharedPreferences。我需要将用户标识添加为会话
【解决方案2】:

因此,您实际上需要两个步骤。您需要解析响应,然后您需要保存它。

为了解析 json 响应,你可以构建一个 POJO 类。 GSON 将对此有所帮助。您也可以按照this 链接解析响应。

然后您可以将数据保存到Shared Preference/Sqlite/局部变量/文件或任何其他地方,并根据您的需要使用保存的值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-02
    • 1970-01-01
    • 1970-01-01
    • 2020-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多