我正在创建一个应用程序,其中我使用php从相应的sql表中获取用户数据。
<?php
$email = $_POST["email"];
@mysql_connect("localhost","root","root") or die(@mysql_error());
@mysql_select_db("dtbse") or die(@mysql_error());
$x = mysql_query("select * from dtbse where email = '$email' ") or die(@mysql_error());
$result = array();
while ($y=mysql_fetch_array($x)) {
echo $y["uname"]."<br>";
echo $y["gender"]."<br>";
echo $y["pass"]."<br>";
echo $y["address"]."<br>";
echo $y["email"]."<br>";
}
?>
任何帮助都会非常感激。我知道这个问题很多次,但我不认为有一些东西可以复制这个问题。感谢。
以下是负责获取和解析的代码段。 final ArrayList arr = new ArrayList(); arr.add(new BasicNameValuePair(“email”,uname));
try {
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("http://xxyoxx.esy.es/getDetails.php");
httppost.setEntity(new UrlEncodedFormEntity(arr));
HttpResponse hr = httpclient.execute(httppost);
HttpEntity ent = hr.getEntity();
is = ent.getContent();
Toast.makeText(getApplicationContext(),"1 wrk ",Toast.LENGTH_LONG).show();
} catch (Exception fl) {
Toast.makeText(getApplicationContext(),"First Try error "+fl,Toast.LENGTH_LONG).show();
}
/*// Depends on your web service
httppost.setHeader("Content-type", "application/json");*/
String result=null;
try {
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
result = sb.toString();
Toast.makeText(getApplicationContext(),"2 str\n "+result,Toast.LENGTH_LONG).show();
} catch (Exception sl) {
sl.printStackTrace();
Toast.makeText(getApplicationContext(),"2 err\n "+sl,Toast.LENGTH_LONG).show();
}
try{
String aa = "", b = "", c = "";
JSONArray ar = new JSONArray(result);
for (int i = 0; i < ar.length(); i++) {
JSONObject jo = ar.getJSONObject(i);
aa = jo.getString("uname");
b = jo.getString("address");
c = jo.getString("email");
}
nm.setText(aa);
addr.setText(b);
mail.setText(c);
Toast.makeText(getApplicationContext(),"3 wrk"+result,Toast.LENGTH_LONG).show();
}
catch (Exception tl){
Toast.makeText(getApplicationContext(),"3 err "+tl,Toast.LENGTH_LONG).show();
}
答案 0 :(得分:1)
由<br>
分隔的字符串不是有效的JSON数组。 PHP可以使用json_encode
如果您需要在Android中读取JSON数组,则需要从PHP回显JSON数组:
<?php
$email = $_POST["email"];
mysql_connect("localhost","root","root") or die(mysql_error());
mysql_select_db("dtbse") or die(mysql_error());
$x = mysql_query("select * from dtbse where email = '$email' ") or die(mysql_error());
$result = array();
$res=[];
while ($y=mysql_fetch_array($x)) {
$res[] = [
$y["uname"],
$y["gender"],
$y["pass"],
$y["address"],
$y["email"]
];
}
echo json_encode($res); //Make PHP return a valid JSON response
此外,错误抑制操作符可能会隐藏有价值的调试信息,这可能有助于您诊断其他问题。
如果您更喜欢将JSON对象传递给Java,那么您可以执行以下(更简单)的操作。
<?php
$email = $_POST["email"];
mysql_connect("localhost","root","root") or die(mysql_error());
mysql_select_db("dtbse") or die(mysql_error());
$x = mysql_query("select * from dtbse where email = '$email' ") or die(mysql_error());
$result = array();
$res=[];
while ($y=mysql_fetch_array($x)) {
$res[] = $y;
}
echo json_encode($res); //Make PHP return a valid JSON response