当我试图通过php mysql在android中将图像显示到imageview中时,我的代码出错了。我正在使用JSON来解析图像..
所以我有一张名为movie的表..
movie
id_movie | Tittle | link_poster
-------------------------------------------------------------------
MP01 | Frozen |http://10.0.2.2/cinemainfo/image/movie1.jpg
我将图像保存到htdocs中名为cinemainfo / image的文件夹中。
所以这是我的detail.php代码:
$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);
}
这是我的detail.java代码:
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());
}
}
}
这是日志猫:
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
当我运行这个网站时:"http://10.0.2.2/cinemainfo/detail.php"
我想确保我的PHP代码很好......它是..
这是我的PHP代码的结果:
{"detail":[
{"tittle":"Frozen",
"link_poster":"aHR0cDovLzEwLjAuMi4yL2NpbmVtYWluZm8vaW1hZ2UvZmlsbTEuanBn"
}
],"success":1
}
有谁知道如何解决我的问题?我陷入困境以掩盖这个问题..任何帮助都会非常有帮助 谢谢你:))
答案 0 :(得分:0)
您在json解析时遇到错误请尝试此代码。
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"));
}