gpt4 book ai didi

php - 使用 Json 将 PHP MySQL 中的图像显示到 Android 中的 imageview 中(解析数据时出错)

转载 作者:行者123 更新时间:2023-11-30 00:48:11 24 4
gpt4 key购买 nike

当我尝试从 php mysql 将图像显示到 android 中的 imageview 时,我的代码中出现错误。我正在使用 JSON 来解析图像。

所以我有一个名为电影的表..

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
}

有人知道如何解决我的问题吗?我很难解决这个问题..任何帮助都会非常有帮助谢谢你:)

最佳答案

您在 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"));

}

关于php - 使用 Json 将 PHP MySQL 中的图像显示到 Android 中的 imageview 中(解析数据时出错),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21199766/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com