我有连接到 MySql 并将数据编码为 JSON 的 PHP 代码。稍后我会过滤它并获取特定的 JSON 对象。当我使用一个 NameValuePair 对象时它工作得很好,但现在我想使用用户名和密码等变量。现在我在 logcat 中收到此警报 解析数据时出错 .org.json.JSONException:org.json.JSONObject$1 类型的值 null 无法转换为 JSONArray。
我应该如何更改可以正常工作的代码?
$q=mysql_query("SELECT username, firstname, lastname, email, phone1, skype, city, description FROM mdl_user WHERE username LIKE '$username' AND password LIKE '$password'");
while($e=mysql_fetch_assoc($q))
$output[]=$e;
print(json_encode($output));
发送请求的代码:
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("usern",""+usr));
nameValuePairs.add(new BasicNameValuePair("passw",""+psw));
InputStream is = null;
String result = "";
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://ik.su.lt/~jbarzelis/Bdarbas/getUserInfo.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
已编辑
$username = mysql_real_escape_string($_REQUEST['usern']);
$password = mysql_real_escape_string($_REQUEST['passw']);
$q=mysql_query("SELECT username, firstname, lastname, email, phone1, skype, city, description
FROM mdl_user WHERE username LIKE '$username' AND password LIKE '$password'");
while($e=mysql_fetch_assoc($q))
$output[]=$e;
print(json_encode($output));