My php:
<?php
$con=mysqli_connect("localhost","xxx","xxx","xxx");
$result = mysqli_query($con, "SELECT username, x,y FROM user");
$jsonData = array();
while($array = mysqli_fetch_assoc($result)){
$jsonData[] = $array;
}
echo json_encode($jsonData);
mysqli_close($con);
?>
My Fetched data to JSONArray: [{"username":"one","x":"22.","y":"55"},{"username":"two","x":2,"y":5}]
Android code:
//Connect to mysql.
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http:example.com/fetchCoordinates.php");
//Getting response
HttpResponse response = httpClient.execute(httpPost);
String responseBody = EntityUtils.toString(response.getEntity());
//Trying to get fetch from array.
JSONArray array = new JSONArray(responseBody);
JSONObject jsonObject = array.getJSONObject(0);
double x = jsonObject.getDouble("x");
Eror:
org.json.JSONArray cannot be converted to JSONObject
It doesn't work. I'm stuck with it.