Reputation: 15
I'm working on my Android app and i have to check if the json string that i receive is null or not. I'm having problems because i wrote in php this:
$result = mysql_query("SELECT....");
$rows = mysql_num_rows($result);
if ($rows==0 or $result==null){
print(json_encode("no data"));
exit;
}
So in my android app i want to check if the result of the json string is "no data" or not. On my app i receive the jsonstring in my httpsend.class where there is the classic method:
@Override
protected String doInBackground(String... params) {
etc..
if(is != null){
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
Now if there aren't rows in the php file, if i do:
Log.i("result from php: ",result);
I get "no data" so ok. But when i give this string value with the:
return result;
to my MainActivity where there is:
String risjson=null;
HttpSend httas = new HttpSend(values...not important);
httas.execute();
try {
risjson=httas.get();
System.out.print("value is"+risjson); //i get "no data" in the Log
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//following one answer (but by now it doesn't work)
if (risjson.equals("\"no data\"")){
System.out.print("THERE AREN'T INFORMATIONS");
}
And my program doesn't go in this if! Why? Remember, the app works fine with right values. (so rows in php isn't zero)
If i do this:
for(int i=0;i<risjson.length();i++){
System.out.println(risjson.charAt(i));
}
System.out.println(risjson.length());
I get:
"
n
o
d
a
t
a
"
11
"no data" are 9 characters, what are the other two characters? This is very very strange.
I saw that if in my php file i do:
if ($righe==0 or $result==null){
print(json_encode(1));
exit;
}
and then the same stemps i get:
1 //what i get from risjson
3 //lenght
So two characters here too..
Upvotes: 0
Views: 338
Reputation: 1241
Try this code on php side:
$result = mysql_query("SELECT....");
$rows = mysql_num_rows($result);
if ($rows==0 or $result==null)
{
print(json_encode(array("data"=>"no data")));
exit;
}
Then client side(Android/Iphone/Web) guys will receive this kind of response(Json object)
{
"data": "nodata"
}
Upvotes: 2
Reputation: 91762
I am not familiar with android, but the problem is likely that your string is not no data
. When you use json_encode
to encode your string, the result will be "no data"
so you would need to check for the quotes as well.
In php you would use '"no data"'
or "\"no data\""
so you have to look for the equivalent in android.
Upvotes: 1