在我的Php文件中从mysql db中重新获取值时,如果db中存在ny行,则返回正确的值,但是当数据库中没有行时,则返回false值thriugh 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值,我将无法处理。请帮助我。 :(
答案 0 :(得分:0)
删除php代码中的false插入,删除if块,然后只允许发送空的json。您似乎已设置了适当的catch块来处理Toast消息。