I love coding
I love coding

Reputation: 1191

How to store cookie Android like in Java?

I want to use the same class I use in a Java application, which works with a Cookie mechanism over a PHP server. The actual class, works great in Java:

public class Connection {

private HttpURLConnection connection;
private String username;
private String password;

public Connection(String username, String password) {
    super();
    this.username = username;
    this.password = password;
    CookieHandler.setDefault(new CookieManager());
    login();
}

public Connection() {
    CookieHandler.setDefault(new CookieManager());
}

public void setCredentials(String username, String password) {
    this.username = username;
    this.password = password;
    login();

}

public String login() {
    String urlParameters = "username=" + username + "&password=" + password
            + "&ac=log";
    return sendPost(
            my url.php",
            urlParameters);

}

public String sendPost(String destination, String post) {
    try {
        URL url = new URL(destination);
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setDoInput(true);
        connection.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(
                connection.getOutputStream());

        wr.writeBytes(post);
        wr.flush();
        wr.close();

        InputStream is = connection.getInputStream();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        String line;
        StringBuffer response = new StringBuffer();
        while ((line = rd.readLine()) != null) {
            response.append(line);
            response.append('\r');
        }
        rd.close();
        return response.toString();

    } catch (Exception e) {

        return null;

    } finally {

        if (connection != null) {
            connection.disconnect();
        }
    }
}

}

Under Java I'm able to manage the cookie received from the server, without any problem; in Android, I was obtain a new PHPsession when I execute the method login(); so how can fix the problem ? I just want to keep the connection with Authentication between Android and the PHP server.

Upvotes: 0

Views: 304

Answers (1)

osayilgan
osayilgan

Reputation: 5893

So as far as I could understand, the idea is to store the Token you received from your server. You can save the Token to shared preferences with the code below, and whenever you need to make a request again, read the token and sign your request with it.

To Write the Token to Shared Preferences :

SharedPreferences settings = context.getSharedPreferences(SHARED_PREFERENCES_NAME, 0);
SharedPreferences.Editor editor = settings.edit();

editor.putString(ACCESS_TOKEN_STRING, token);

/* Commit the edits */
editor.commit();

To Read the Token :

SharedPreferences settings = context.getSharedPreferences(SHARED_PREFERENCES_NAME, 0);
return settings.getString(ACCESS_TOKEN_STRING, "");

Upvotes: 1

Related Questions