I have some images in my db. I´m trying to populate a gridview but nothing is loaded. This is the logcat:
org.json.JSONException: Value http://fisioterapeutacamila.com/iMoveis/img/apto01.jpg at images of type java.lang.String cannot be converted to JSONArray
10-28 20:29:41.055 1679-1695/? W/System.err: at org.json.JSON.typeMismatch(JSON.java:100)
10-28 20:29:41.059 1679-1695/? W/System.err: at org.json.JSONObject.getJSONArray(JSONObject.java:553)
10-28 20:29:41.063 1679-1695/? W/System.err: at com.javatechig.gridviewexample.GridViewActivity.parseResult(GridViewActivity.java:160)
This is my json parser
private void parseResult(String result) {
try {
JSONObject response = new JSONObject(result);
JSONArray posts = response.optJSONArray("posts");
GridItem item;
for (int i = 0; i < posts.length(); i++) {
JSONObject post = posts.optJSONObject(i);
//String title = post.optString("title");
item = new GridItem();
//item.setTitle(title);
JSONArray attachments = post.getJSONArray("images");
if (null != attachments && attachments.length() > 0) {
JSONObject attachment = attachments.getJSONObject(0);
if (attachment != null)
item.setImage(attachment.getString("url"));
}
mGridData.add(item);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
Php file
<?php
include_once 'db_connect.php';
$sql = "SELECT * FROM iMoveis";
$result = mysqli_query($mysqli, $sql);
$v = '{"posts":[';
while($row = mysqli_fetch_array($result)){
$v .= '{"images":"'.$row['img1'].'", "images":"'.$row['img2'].'", "images":"'.$row['img3'].'",
"images":"'.$row['img4'].'", "images":"'.$row['img5'].'"},';
}
$v .= ']}';
echo $v;
?>
What is wrong here? PHP or the parser on java class? I have changed my parseResult but still got nothing loaded on gridview. Could anyone help me with php json format? Thanks in advance.