这是来自mysql数据库的Json编码:
[
{"id":"1","title":"test_worked","desc":"the first project","done":"0"},
{"id":"2","title":"mohsen","desc":"Zikey01","done":"1"},
{"id":"3","title":"ms","desc":"tl","done":"1"}
]
这是我的代码。但有一个错误org.json.JSONException:Value<类型java.lang.String无法转换为JSONArray
类ReadJSON扩展了AsyncTask {
protected String doInBackground(String... args) {
InputStream jsonStream = getStreamFromURL(url1, "GET");
String jsonString = streamToString(jsonStream);
if (jsonString!=null){
try {
JSONArray tasks = new JSONArray(jsonString);
for ( int i=0;i<tasks.length();i++){
JSONObject task =tasks.getJSONObject(i);
Log.i("myLog1403","array is"+task.getString("desc"));
}
} catch (JSONException e) {
e.printStackTrace();
Log.i("my-Log","cant use json Array");
}
}
return null;
}
}
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "note_db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * from task";
$result = $conn->query($sql);
$Output = array();
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$record=array();
$record['id']=$row['task_id'];
$record['title']=$row['task_title'];
$record['desc']=$row['task_desc'];
$record['done']=$row['task_done'];
$output[]=$record;
}
echo json_encode($output);
} else {
echo "0 results";
}
$conn->close();
?>