Android Asynchronous Http Client (loopj) and persistent cookie store

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! :)

+4
source share
1 answer

So here is what I have done so far. This works, but I'm not sure if this is the best practice.

1) The cookie store is initialized from sharedpreference. so just create a new one every time you need it. Remember to use the same context every time. I am using getApplicationContext()

2) and 4) The cookie store passes all this to you. Just create a new one with the same context as the one you created earlier. As long as you agree, cookies will be correctly initialized.

3) the cookie settings store general settings and local attributes, so just call (new PersistentCookieStore(getApplicationContext())).clear();

My code

RestClient.java

 public static void setCookieStore(PersistentCookieStore cookieStore) { client.setCookieStore(cookieStore); } 

LoginActivity.java

 RestClient.setCookieStore(new PersistentCookieStore(getApplicationContext())); 

MainActivity.java

 private void loginRouter() { PersistentCookieStore mCookieStore = new PersistentCookieStore( getApplicationContext()); List<Cookie> cookies = mCookieStore.getCookies(); for (Cookie c : cookies) { if (c.getName().equals("session")) { startActivity(new Intent(this, HomeActivity.class)); finish(); } } launchSplashPage(); } 
+5
source

Source: https://habr.com/ru/post/1481733/


All Articles