I've got an error in my code when I was trying to display an image into imageview in android from php mysql..I'm using JSON to parse the image..
So I have a table named movie..
movie
id_movie | Tittle | link_poster
-------------------------------------------------------------------
MP01 | Frozen |http://10.0.2.2/cinemainfo/image/movie1.jpg
I save the image into a folder named cinemainfo/image in htdocs..
so here is my detail.php code:
$response = array();
$id_movie = $_REQUEST['id_movie'];
$sql="select tittle, link_poster from movie where id_movie = '".$id_movie."'";
$result = mysql_query($sql);
if (mysql_num_rows($result) > 0)
{
$response["detail"] = array();
while ($row = mysql_fetch_array($result))
{
$detail = array();
$detail["tittle"] = stripslashes($row["tittle"]);
$detail["link_poster"] = base64_encode($row["link_poster"]);
array_push($response["detail"], $detail);
}
$response["success"] = 1;
echo json_encode($response);
}
else {
$response["success"] = 0;
$response["message"] = "No data";
echo json_encode($response);
}
and here is my detail.java code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail);
Bundle b = this.getIntent().getExtras();
kode = b.getString("kode_intent");
tittle=(TextView)findViewById(R.id.tv_tittle);
poster=(ImageView)findViewById(R.id.img_poster);
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("id_movie", kode));
String response = null;
try {
response = CustomHttpClient.executeHttpPost("http://10.0.2.2/cinemainfo/detail.php", postParameters);
String result = response.toString();
try {
JSONArray jArray = new JSONArray(result);
JSONObject json_data=null;
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
ttl =json_data.getString("tittle");
pstr = json_data.getString("link_poster");
}
byte[] rawImage = Base64.decode(pstr, Base64.DEFAULT);
bmp = BitmapFactory.decodeByteArray(rawImage, 0, rawImage.length);
}
catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
try {
tittle.setText(ttl);
poster.setImageBitmap(bmp);
}
catch(Exception e){
Log.e("log_tag","Error in Display!" + e.toString());;
}
}
catch(Exception e){
Log.e("log_tag", "Error in http connection"+e.toString());
}
}
}
And here is the log cat:
E/log_tag(338): Error parsing data org.json.JSONException: Value {"success":1,"detail":[{"link_poster":"aHR0cDovLzEwLjAuMi4yL2NpbmVtYWluZm8vaW1hZ2UvZmlsbTEuanBn","tittle":"Frozen"}]} of type org.json.JSONObject cannot be converted to JSONArray
When I running this site : "http://10.0.2.2/cinemainfo/detail.php"
I want to make sure that my php code is fine..and it is.. here is the result of my php code :
{"detail":[
{"tittle":"Frozen",
"link_poster":"aHR0cDovLzEwLjAuMi4yL2NpbmVtYWluZm8vaW1hZ2UvZmlsbTEuanBn"
}
],"success":1
}
Does anyone know how to fix my problem? I'm getting stuck to cover this problem ..any help would be very helpful thank you :)
you having the error on json parsing try this code.
ArrayList<String> title= new ArrayList<String>();
ArrayList<String> imgurl=new ArrayList<String>();
String status="";
try {
JSONObject result=new JSONObject(jsonouput);
status = result.getJSONObject("success");
JSONArray jArray = new JSONArray(result.getString("detail"));
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i).toString();
title.add(json_data.getString("tittle"));
imgurl.add(json_data.getString("link_poster"));
}