在我的 Php 文件中,从 mysql db 中检索值时,如果 db 中存在 ny 行,则它返回正确的值,但是当数据库中没有行时,它通过 json_encode 返回错误值我的 php 代码是:
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("meetingschedulardata", $con);
$mdate=$_GET['mdate'];
//$mdate= '28-April-2013';
$date = strtotime($mdate);
$new_date = date('Y-m-d', $date);
$sql=mysql_query("select * from meetingdetails where mdate='$new_date'",$con);
$row=mysql_num_rows($sql);
if($row===0)
{
$output[][]=false;
print(json_encode($output));
}
else
{
while($row=mysql_fetch_assoc($sql))
$output[][]=$row;
print(json_encode($output));
}
mysql_close();
?>
我解析 json 值的 java 代码是:
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/meetingschedular/viewdetail.php?mdate="+currentDateandTime);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
catch(Exception e)
{
Toast.makeText(getBaseContext(),e.toString() ,Toast.LENGTH_LONG).show();
}
//Convert response to string
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result = sb.toString();
}
catch(Exception e)
{
Toast.makeText(getBaseContext(),e.toString() ,Toast.LENGTH_LONG).show();
}
//END Convert response to string
try
{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++)
{
JSONArray innerJsonArray = jArray.getJSONArray(i);
JSONObject json_data = innerJsonArray.getJSONObject(0);
String at= json_data.getString("attendees");
String at1=json_data.getString("title");
String at2=json_data.getString("mtime");
String at3=json_data.getString("venue");
s.append(j+"Attendees:"+at);
s.append("\nTitle:"+at1);
s1.append("Time:"+at2);
s1.append("\nVenue:"+at3);
r.add(s.toString());
r.add(s1.toString());
j++;
}
l.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, r));
}
catch(Exception e1)
{
Toast.makeText(getBaseContext(),e1.toString(),Toast.LENGTH_LONG).show();
}
如果 json_encode 返回 false 值并显示一些 toast 以响应 false 值,我不知道我将如何处理。请帮助我。:(