1. Download our Official Android App: Forums for Android!

Apps $_SESSION not working with Android application

Discussion in 'Android Development' started by cdubs, Feb 25, 2016.

  1. cdubs

    cdubs Newbie
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    13
    Joined:
    Feb 25, 2016

    Feb 25, 2016
    13
    1
    16
    Male
    I am trying to store the logged in user in a new session, get the info from the database, put it to a json array, then get the array in android and display the results in a layout. When I run the app however, the array being returned from the php file is empty. If I am to test the php file however in a different program such as Postman, I get the correct response which is,
    {"result":[{"id":"12","fname":"C","lname":"Wei","username":"cw","email":"werrs.‌com"}]}
    I found out that in order to retrieve a previously initiated session on server side, my request has to contain the session id, but how am I supposed to get the session id without the session id? Here is my getSessionId class and getUserData methods I am using as well as the PHP files they are referencing. I am currently getting an empty result from my stringRequest.

    getSessionId java
    Code (Text):
    1.  
    2. public class SessionId extends AppCompatActivity{
    3.  
    4.     String id;
    5.  
    6.     public String getSessionId(){
    7.         StringRequest stringRequest = new StringRequest(Request.Method.GET, Config.SERVER_ADDRESS + "GetSessionId.php",
    8.                 new Response.Listener<String>() {
    9.                     @Override
    10.                     public void onResponse(String response) {
    11.                         JSONObject jsonObject = null;
    12.                         try{
    13.                             jsonObject = new JSONObject(response);
    14.  
    15.                             id = response;
    16.                         }catch (JSONException e){
    17.                             e.printStackTrace();
    18.                         }
    19.                     }
    20.                 },
    21.                 new Response.ErrorListener() {
    22.                     @Override
    23.                     public void onErrorResponse(VolleyError error) {
    24.  
    25.                     }
    26.                 });
    27.  
    28.         RequestQueue requestQueue = Volley.newRequestQueue(this);
    29.         requestQueue.add(stringRequest);
    30.  
    31.         return id;
    32.     }
    33.  
    34. }
    35.  
    getUserData java
    Code (Text):
    1.  
    2. SessionId sessionId = new SessionId();
    3. private void getUserData() {
    4.     StringRequest stringRequest = new StringRequest(Request.Method.GET, Config.SERVER_ADDRESS + "GetUserData.php?PHPSESSID=" + sessionId.id,
    5.             new Response.Listener<String>() {
    6.                 public void onResponse(String response) {
    7.                     JSONObject jsonObject = null;
    8.                     try {
    9.                         //json string to jsonobject
    10.                         jsonObject = new JSONObject(response);
    11.  
    12.                         //get json sstring created in php and store to JSON Array
    13.                         result = jsonObject.getJSONArray(Config.json_array);
    14.  
    15.                         //get username from json array
    16.                         getUserInfo(result);
    17.                     } catch (JSONException e) {
    18.                         e.printStackTrace();
    19.                     }
    20.                 }
    21.             },
    22.             new Response.ErrorListener() {
    23.                 public void onErrorResponse(VolleyError error) {
    24.  
    25.                 }
    26.             });
    27.  
    28.     RequestQueue requestQueue = Volley.newRequestQueue(this);
    29.     requestQueue.add(stringRequest);
    30. }
    31.  
    getUserData PHP
    Code (Text):
    1.  
    2. <?php
    3. session_start();
    4.     if($_SERVER['REQUEST_METHOD'] == 'GET'){
    5.  
    6.         $username = $_SESSION['username'];
    7.  
    8.         $sql = "SELECT * FROM USER WHERE username = '$username'";
    9.  
    10.         require_once('connect.inc.php');
    11.  
    12.         $run = mysqli_query($conn, $sql);
    13.         $result = array();
    14.  
    15.         while($row = mysqli_fetch_array($run)){
    16.             array_push($result, array(
    17.                 'id' => $row['id'],
    18.                 'fname' => $row['fname'],
    19.                 'lname' => $row['lname'],
    20.                 'username' => $row['username'],
    21.                 'email' => $row['email'],
    22.             ));
    23.         }
    24.  
    25.         echo json_encode(array('result'=>$result));
    26.  
    27.         mysqli_close($conn);
    28.     }
    29. ?>
    30.  
    getSessionId PHP
    Code (Text):
    1.  
    2. <?php
    3.  
    4.     if($_SERVER['REQUEST_METHOD'] == 'GET'){
    5.         session_start();
    6.         $sessionid = session_id();
    7.         echo json_encode(['sessionId' => $sessionid]);
    8.     }
    9.  
    10. ?>
    11.  
     

    Advertisement

    #1 cdubs, Feb 25, 2016
    Last edited: Feb 25, 2016

Share This Page

Loading...