我正在编写一个 android 应用程序来检索属于特定用户的数据。我在 Activity2 中使用 Activity1 的 SharedPreference。
这是我存储在 Activity1 中的 SharedPreference
SharedPreferences sp = getSharedPreferences("login details", 0);
SharedPreferences.Editor spedit = sp.edit();
spedit.putString("sPhone",sPhone);
spedit.putString("sPassword", sPassword);
spedit.commit();
我在 Activity2 中使用上述 SharedPreference 并将其作为字符串发送到 PHP 文件: Activity2 代码:
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();
这是PHP代码:
<?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();
?>
但 SQL 查询返回“null”。我收到一条错误消息“未定义的变量输出”。由于这个空值,应用程序通过说“FATAL EXCEPTION AsyncTask #1”在模拟器上崩溃。