Passing PHP sessions between Android activities

Vish :

I'm developing an Android app which has its own database in a web server. My problem is when I create a log-in session in one activity, the session variables are not passed to the next activity.

I use HttpURLConnection to establish a connection in both activities as following. (Parameter for URL changes)

URL url = new URL(si_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();

In my 2nd PHP file, isset( $_SESSION['memberid'] ) returns false.

<?php
require "conn.php";

session_start();
$value = $_SESSION['memberid'];

if(isset($_SESSION['memberid'])){
    echo (string)$_SESSION['memberid'];
}
else {
    echo "Error!";
}
?>

I suspect this occurs because I create 2 different connections for the 2 activities. Any solutions for this?

Vish :

Solved using CookieManager

CookieManager cookieManager = new CookieManager();
try{
    CookieHandler.setDefault(cookieManager);
}catch(UnsupportedOperationException ex){
    ex.printStackTrace();
}

Just put this code somewhere in your app before it executes the HttpURLConnection

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=107561&siteId=1