我有我想从 Android 获取的 JSON 数据
{"success":1,"message":"Product successfully created."}
完整代码在这里:
<?php
header('Content-type: application/json');
mysql_connect("127.0.0.1","root","");
mysql_select_db("android");
$fname=$_POST['firstname'];
$lname=$_POST['lastname'];
$username=$_POST['username'];
$password=$_POST['password'];
$sql=mysql_query("select * from info ");
if($sql)
{
$response["success"] = 1
$response["message"] = "Product successfully created.";
echo json_encode($response);
}
else
{
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
$data[]=$response;
// echoing JSON response
echo json_encode($response);
echo json_encode($data);
}
while($row=mysql_fetch_array($sql))
$output[]=$row;
json_encode($output);
print(json_encode($output));
mysql_close();
?>
我到底想要做的是得到响应是否成功或失败其余的工作,例如从数据库中检索数据。
我试过这段代码:
JSONObject obj=new JSONObject(sb.toString())
然后
JSONObject objdata=obj.getJSONObject('success');
但出现“类型不匹配”和“无法转换”等错误