Quantcast
Channel: Question and Answer » json
Viewing all articles
Browse latest Browse all 148

Android, PHP session and JSON object retrieval

$
0
0

I am working on an app in android studio.. it currently has a login and registration system that are linked to a database and both work perfectly.

I am now trying to carry over the user id value using SESSION in php so that the activity past the login will have all the information it needs to personalize the app. To do so I made a class called Profile that, when instantiated, retrieves and stores all user information based on the id stored as a session variable.

The profile class is accompanied by it’s own php file, buildprofile.php. Any class can instantiate Profile and use to to get information (hopefully).

Currently when I attempt to instantiate Profile the app crashes. There are probably 50 different things causing this and I have changed things over and over to no avail. I started a session in the login activity and stored a session variable ‘id’ upon login success.

The Profile constructor (based on method in a JSONParser class):

try {
        JSONObject jobj = jsonParser.getJSONFromUrl(PROFILE_URL);
        username = jobj.getString("username");
    } catch (JSONException e) {
        e.printStackTrace();
    }

buildprofile.php:

<?php
session_start();

require("config.inc.php");

if (isset($_SESSION['id'])){
    $query = "SELECT id,email,username,bio FROM users WHERE id = :id";    
    $query_params = array(':id' => $_SESSION['id']);

    try {
        $stmt   = $db->prepare($query);
        $result = $stmt->execute($query_params);
    }
    catch (PDOException $ex) {
        $response["success"] = 0;
        $response["message"] = "Database Error1. Please Try Again!";
        die(json_encode($response));
    }
}

$row = $stmt->fetch();
json_encode($row);

?>

Viewing all articles
Browse latest Browse all 148

Trending Articles