我有一个PHP类,可以从数据库中检索数据并将其编码为 JSONARRAY 以后在Android应用中使用它。由于某种原因是返回 null ,我不知道它是什么问题。
这是文件:
<?php
$response = array();
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
$dbh = $db->connect(); // here you get the connection
if(isset($_GET['TAG_ID'])){
$id = $_GET['TAG_ID'];
$query = "SELECT *FROM lost_pets WHERE id = '$id'";
$result = $dbh->prepare($query);
$result->execute();
if ($result->fetchAll() > 0) {
foreach($dbh->query($query) as $row){
$pet["name"] = $row['name'];
$pet["breed"] = $row['breed'];
$pet["type"] = $row['type'];
$pet["description"] = $row['description'];
$pet["pictures"] = $row['pictures'];
$pet["location"] = $row['location'];
$pet["locality"] = $row['locality'];
$pet["userid"] = $row['userid'];
echo json_encode($result->fetchAll(PDO::FETCH_ASSOC));
}
}
}
?>
Android侧:
else if(method.equals("GET")){
// request method is GET
if (sbParams.length() != 0) {
url += "?" + sbParams.toString();
}
try {
urlObj = new URL(url);
conn = (HttpURLConnection) urlObj.openConnection();
conn.setDoOutput(false);
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept-Charset", charset);
conn.setConnectTimeout(15000);
conn.connect();
is = conn.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
is.close();
json = sb.toString();
elements = new JSONArray(json);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
return elements;
}
ID由应用程序给出,我已经检查了可能的价值。我对PHP的了解很差,因此解决可能不是一个困难的问题,无论如何,我希望您能为此提供帮助,谢谢!
我不知道这里确实发生了什么,但是如果条款解决了问题,则删除该问题。感谢您的所有评论