I am having some really strange problems. So in my app, when someone logs into Google Plus, the name, profile picture, and id are sent in JSON to a webpage, where a PHP script takes the JSON and gets all the data from it, checks in the database if such a user already exists, and if not, adds him.
So I've already done this before, for a different purpose, and now when I'm trying to copy what I did before, it doesn't work. I'm not exactly sure what's going on, because the same code works for something else.
So this is my Android code. I've checked with the log to make sure that the JSON Object has everything.
public void updateData(String url, JSONObject jobj){
DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(defaultHttpClient.getParams(), 100000);
HttpPost httpPost = new HttpPost(url);
JSONArray sendJSON = new JSONArray();
sendJSON.put(jobj);
httpPost.setHeader("json", jobj.toString());
httpPost.getParams().setParameter("jsonpost", sendJSON);
try {
HttpResponse httpResponse = defaultHttpClient.execute(httpPost);
} catch (IOException e) {
e.printStackTrace();
}
}
And here is my PHP code:
<?php
require_once('connection.php');
if( isset($_SERVER['HTTP_JSON']) )
{
$data = json_decode($_SERVER['HTTP_JSON'], true);
$name = $data['name'];
$profilePicture = $data['profilePicture'];
$googleId = $data['googleId'];
//First see if the user is already in the database
$find_existing_query = "SELECT * FROM users WHERE googleId = $googleId";
$result = mysql_query($find_existing_query);
//Add user if there are no rows returned
if(mysql_num_rows($result) == 0)
{
$add_user_query = "INSERT INTO users (name, profilePicture, googleId) VALUES ($name, $profilePicture, $googleId)"
mysql_query($add_user_query);
}
$qry = "SELECT * FROM users";
$result = mysql_query($qry);
$rows = array();
while($r = mysql_fetch_assoc($result)){
$rows[] = $r;
}
echo json_encode($rows);
mysql_close();
}
else{
echo 'no JSON sent';
}
?>
Also, strangely, when I visit the webpage, I should be seeing "No JSON sent", but I am seeing a blank page. And any echo statements placed at the top of the file is not being done. Also, when I am running the SELECT * FROM users query, it outputs data from a different table in the database.
I really have no idea what is going on. Any help would be greatly appreciated!