1

In need my application to login and keep the same session id until i logout. in mij login i use this:

     public class loginTask extends AsyncTask<String, String, String> {

            @Override
            protected String doInBackground(String... params) {
                String loginData = params[0];
                String text = "";
                BufferedReader reader = null;

                // Send data
                try {
                    // Defined URL  where to send data
                    URL url = new URL(URL);

                    // getting cookies:
                    URLConnection conn = url.openConnection();
                    conn.connect();

                    // setting cookies
                    cookieManager.setCookies(url.openConnection());
                    cookieManager.storeCookies(conn);
}

when the application is automatically logged in, it needs to send some data to a different URL:

Public class sendVisumNo extends AsyncTask<String, String, String> {

        @Override
        protected String doInBackground(String... params) {
            String visumNo = params[0];
            String text = "";
            BufferedReader reader = null;

            // Send data
            try {
                // Defined URL  where to send data
                URL send_visum_url= new URL(SEND_VISUM_URL);

                // getting cookies:
                conn = loginURL.openConnection();
                conn.connect();

                // setting cookies
                cookieManager.setCookies(loginURL.openConnection());
                cookieManager.storeCookies(conn);
}

How can i give the SEND_VISUM_URL the same cookie/session id as the first one? until i logout

Any help would be greatly appreciated!

bananana
  • 105
  • 3
  • 10

1 Answers1

0

You can use SharedPreferences to do that This is more complete answer to shared preferences.

private SharedPreferences preferences;
private SharedPreferences.Editor editor;
preferences = getSharedPreferences("com.example.yourpackagename.bla", Context.MODE_PRIVATE);`

Storing data:

String cookies="someweirdbigstring";
editor = preferences.edit();
editor.putString("cookies",cookies);
editor.apply()

Retrieving data:

String cookies = preferences.getString("cookies", null);
if (cookies!=null){
    //Do stuff with cookies
}else{
    // No cookies
}
Kaushal28
  • 5,377
  • 5
  • 41
  • 72