I'm writing an android application that retrieves data belonging to a particular user. I'm using SharedPreference of Activity1 in Activity2.
here's the SharedPreference that I'm storing in Activity1
SharedPreferences sp = getSharedPreferences("login details", 0);
SharedPreferences.Editor spedit = sp.edit();
spedit.putString("sPhone",sPhone);
spedit.putString("sPassword", sPassword);
spedit.commit();
I'm using the above SharedPreference in Activity2 and sending it as a String to a PHP file: The Activity2 code:
SharedPreferences prefs = getSharedPreferences("login details", 0);
String str = prefs.getString("sPhone", "");
nameValuePairs.add(new BasicNameValuePair("userid", str));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httppost.setHeader("Content-type", "application/json");
response = httpclient.execute(httppost);
entity = response.getEntity();
is = entity.getContent();
Here's the PHP code:
<?php
mysql_connect("localhost","root","");
mysql_select_db("assigndroid");
header('content-type=application/json; charset=utf-8');
//$userID = isset($_POST['userid']) ? $_POST['userid'] : '';
$userID = mysql_real_escape_string($_POST['userid']);
$sql=mysql_query("select * from newassignment where issuedBy='$userID';");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print(json_encode($output));
mysql_close();
?>
But the SQL query is returning 'null'. I get an error saying "Undefined variable output". And due to this null value, the application is crashing on the emulator by saying a "FATAL EXCEPTION AsyncTask #1".