任何人都可以帮助我,我真的需要一个解决方案,我已经搜索过SO,无法成功!
我想将我从服务器接收的自动生成的user_id存储为我的共享首选项中的JSON响应,然后再次从共享首选项获取到其他活动并作为参数发送到服务器
这是我的登录活动,其中我在发送用户名并传递给服务器时在JSON响应中收到唯一用户ID
public void connect(String useremail, String userpassword) throws JSONException, IOException
{
RestClient1 client = new RestClient1(Constants.serverPath + Constants.loginMethod);
client.addParam("email", useremail);
client.addParam("password", userpassword);
client.addHeader("content-type", "application/json");
try
{
String response = client.executePost();
JSONObject jsonResponse = new JSONObject(response);
String jsonData = jsonResponse.getString("code");
String jData = jsonResponse.getString("ResponseStatus");
jsData = jData;
if (jsonData.equals("200"))
{
System.out.println("responseStatus =" + jData);
startActivity(new Intent(LoginMainActivity.this, DashBoardActivity.class));
finish();
}
In the "ResponseStatus" I am geting "Login Succsesfull , user_id=1" from server <**<< want to store this response on Share Preference**
我的基本会话管理器类没有完全构造用于在JSON中存储响应
public class SessionManager {
// Shared Preferences
SharedPreferences pref;
// Editor for Shared preferences
Editor editor;
// Context
Context _context;
// Shared pref mode
int PRIVATE_MODE = 0;
// Sharedpref file name
private static final String PREF_NAME = "Apppersonal";
// All Shared Preferences Keys
private static final String KEY_USERID = "user_id";
// public static final String KEY_FULLNAME = "fullName";
// public static final String KEY_EMAIL = "email";
// public static final String KEY_DOB = "dateofbirth";
// public static final String KEY_ADDRESS = "address";
// Constructor
public SessionManager(Context context){
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
public void createloginSession(int user_id)
{
// Storing id in pref
editor.putInt(KEY_USERID, user_id);
editor.commit();
}
public HashMap<String, String> getPersonalDetails() {
HashMap<String, String> userPersonal = new HashMap<String, String>();
userPersonal.put(KEY_FULLNAME, pref.getString(KEY_FULLNAME, null));
userPersonal.put(KEY_DOB, pref.getString(KEY_DOB, null));
userPersonal.put(KEY_EMAIL, pref.getString(KEY_EMAIL, null));
userPersonal.put(KEY_ADDRESS, pref.getString(KEY_ADDRESS, null));
// return user
return userPersonal;
// TODO Auto-generated method stub
// return null;
}
// Clear session details
public void logoutUser(){
// Clearing all data from Shared Preferences
editor.clear();
editor.commit();
// After logout redirect user to Loing Activity
Intent i = new Intent(_context, LoginMainActivity.class);
// Closing all the Activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add new Flag to start new Activity
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Staring Login Activity
_context.startActivity(i);
}
}
答案 0 :(得分:0)
获取user_id后,请执行以下操作:
SessionManager mSessionManager = new SessionManager(context);
mSessionManager.createloginSession(user_id);
答案 1 :(得分:0)
在延长活动后放置
SessionManager session;
并在你的方法中把这个
session.createLoginSession(json.getString(user_id));