日志显示如下响应:
我从服务器发送了一个使用JSON编码的PHP数组,输出:
如何从意外的响应代码中获取数组?该数组以JSON编码方式发送。
PHP:
<?php
require "conn.php";
class person {
public $id;
public $age;
public $name;
public $lat;
public $longi;
public $email;
public $image;
public $number;
}
$bloodgroup = $_POST["bloodgroup"];
$sql = "Select * from UserDetails where bloodgroup = '$bloodgroup'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$donorsArray = array();
while($row = $result->fetch_assoc()) {
$donor = new person;
$donor->id = $row["ID"];
$donor->age = $row["age"];
$donor->name = $row["Name"];
$donor->lat = $row["lat"];
$donor->longi = $row["longi"];
$donor->email = $row["email"];
$donor->image = $row["image"];
$donor->number = $row["number"];
$donorsArray[] = $donor;
}
} else {
}
echo json_encode(array_values($donorsArray));
?>
爪哇:
OkHttpClient client = new OkHttpClient();
RequestBody formBody = new FormBody.Builder()
.add("bloodgroup",bloodgroup)
.build();
Request request = new Request.Builder()
.url("http://blooddonation.byethost6.com/GetDonorsDetails.php")
.post(formBody)
.build();
try {
Response response = client.newCall(request).execute();
String res = response.body().string();
Log.e("responseaaa",res);
// Do something with the response.
} catch (IOException e) {
e.printStackTrace();
Log.e("responseaaa","with error "+e.toString());
}