I am new to Android development. If a few questions about how best to make the libraries mentioned above work optimally.
I currently have three actions in my application. MainActivity, LoginActivity, and HomeActivity. The application launches MainActivity, which should check if the user is registered. If a person is logged in, redirect to the main one, and then redirect to Login.
As mentioned in the documentation , I created the RestClient class. I can successfully make a request in my LoginActivity and get a response. This is my login code.
public void login() { RequestParams params = new RequestParams(); params.put(AUTH_PARAMETER_EMAIL, mEmail); params.put(AUTH_PARAMETER_PASSWORD, mPassword); RestClient.setCookieStore(new PersistentCookieStore(this)); RestClient.post(AUTH_URL, params, new JsonHttpResponseHandler() { @Override public void onFinish() { showProgress(false); } @Override public void onSuccess(JSONObject response) { String response_status = null; try { response_status = response.getString(AUTH_RESPONSE_STATUS); } catch (JSONException e) { Toast.makeText(LoginActivity.this, "ERROR: " + e.toString(), Toast.LENGTH_LONG).show(); Log.e(TAG, e.toString()); } if (response_status.equals(AUTH_SUCCESS_STATUS)) { finish(); } else { mPasswordView .setError(getString(R.string.error_incorrect_password)); mPasswordView.requestFocus(); } } @Override public void onFailure(Throwable e, String content) { Toast.makeText(LoginActivity.this, "ERROR: " + e.toString(), Toast.LENGTH_LONG).show(); Log.e(TAG, e.toString()); } }); }
Questions
- This will create a new cookie store every time a request is made. Where should I put it so that it is created only once? Should I put it in onCreate of MainActivity and then assign it a global variable? Is this the best practice?
- In my MainActivity, how can I check the session cookie that was sent from the server? I know that this is in general preference, but how can I get it? The documentation does not say which variable it will be stored in the SharedPreferences section.
- When do I need to log out, delete the general settings or erase the cookie storage or both? Do they automatically sync?
- When the application restarts, how to initialize the cookie store from the stored data in sharedpreferences?
- If you know any open source code that implements this correctly, I will be glad to look at it and answer these questions myself. Just provide a link!
I understand that my level of understanding here is very low, so please come with me! :)
nknj source share