0

I am woking on a project . I am sending mysql data from server to my android app via json. Here is my code on server.

while ($r = mysql_fetch_assoc($result)) {
    $rows["sno"] = $r['sno'];
    $rows["sender"] = $r['sender'];
    $rows["subject"] = $r['subject'];
    $rows["message"] = $r['message'];
    $rows["date"] = date("M j, Y, g:i a",$r['date']); 
}
echo json_encode(array('mail' => $rows));

When message < 600 characters are encoded, I get it on my application. But any messages > 600 characters will be send as null to my application. Where am I doing mistake?

CSchulz
  • 10,882
  • 11
  • 60
  • 114
Nepal12
  • 583
  • 1
  • 12
  • 29
  • 2
    what's "very big"? multimegabytes? gigabytes? json itself has no length limit, and PHP's json_encode has no built-in limits other than PHP's own limits (memory_limit, recursion depth, etc...). – Marc B Feb 28 '14 at 18:07
  • I mean > 600 characters. – Nepal12 Feb 28 '14 at 18:10
  • again. what's "big"? Does 601 characters blow things up? maybe 602? or perhaps 245645623456252353 characters? – Marc B Feb 28 '14 at 18:11
  • 1
    Ok there you go , I edited my question,. And I don't get your point here ? You are emphasizing on the word "BIG", I don't think it does matters . Big here I mean more than 600 characters (i guess 1200 bytes). But still , Whenever message is more than 600 characters then I will get null result. H – Nepal12 Feb 28 '14 at 18:18
  • 1
    because PHP definitely does not have a 600/601 char limit imposed on it. If you're getting null in your client app, then something's wrong on the client side. PHP and json_encode() can spit out megabytes worth of json data if they have to. they don't care. as long as there's enough memory for it, they'll keep chugging away. – Marc B Feb 28 '14 at 18:20
  • But I guess I don't have any mistake on client side.`02-28 19:21:15.868: E/JSON(2349): {"mail":{"sno":"11","sender":"bio","subject":"Hello Mr How do you do ?","message":null,"date":"Feb 28, 2014, 12:53 pm"}} ` This is what I am getting on my log cat as the return of my json object. – Nepal12 Feb 28 '14 at 18:23
  • 1
    Did you do a `var_dump($r)`, then, to see what's coming out of your database? Had you shown that json to begin with, this could have all been avoided. It sounded like the ENTIRE json string was coming across as snull, not just the `message` component. – Marc B Feb 28 '14 at 18:24
  • With var_dump I am getting all data. You could check here `http://www.biotrack.net78.net/mobile/pm.php` – Nepal12 Feb 28 '14 at 18:31
  • about all I can think of is possibly a screwed PHP install that's confused by the accented characters in the message string. Try manually setting a message, e.g. `$rows['message'] = str_repeat('a', 700)` and see if that comes through. there's no reason that string SHOULDN'T get encoded. – Marc B Feb 28 '14 at 18:34
  • Well I am getting 700 a's . You could check. I have a doubt. Is it because of the German Characters that I am using like ä or ö ? – Nepal12 Feb 28 '14 at 18:38
  • possibly. start playing with various chars and see if any one(s) in particular trigger the bug. beyond that, I haven't got a clue. json itself won't care what chars are in the string (utf, ascii, raw binary garbage, etc...) as long as any json-metachars in that string are properly escaped. – Marc B Feb 28 '14 at 18:39
  • 1
    A couple of things to do is to change the content type to standard text so that you can view what PHP is sending in the browser. Also browsers, for instance Chrome, may have a JavaScript console that will let you see the object. I have used both of these when using JQuery on the browser client side and was seeing nothing coming across the json. In my case it was a JSON text not following the spec. – Richard Chambers Feb 28 '14 at 18:40
  • If you guys now look at `http://www.biotrack.net78.net/mobile/pm.php`. I am getting exactly what I want. So that was because of the German Characters. I use "latin1_general_ci" as the default collation of all my tables. Now Since I got the error. What could be the perfect solution. Shall I change the encode of my database (which I guess is a bit troublesome) Or shall I use str_replace to replace all my german characters ? – Nepal12 Feb 28 '14 at 18:45
  • @RichardChambers: You mentioned that you were getting trouble with Spec. How did you resolved it? – Nepal12 Feb 28 '14 at 19:12
  • @Nepal12, I looked at the JSON text output and stepped through the text to make sure that the syntax was correct. The first hint that things were wrong was when the Chrome Javascript console showed an empty object rather than the correct object. I was using JQuery on the client side and on the server side C since this is a built in component providing a subset of web services. There are two sides to this, server building the correct JSON and client receiving the correct JSON. I was using UTF-8 with the ANSI character set and you might be running into UTF-16 character or something as well. – Richard Chambers Feb 28 '14 at 22:32
  • 1
    Can you echo the json_encode() string produced and review it? How about tring JSON validator at http://jsonlint.com/? – Richard Chambers Feb 28 '14 at 22:36
  • @RichardChambers: I got the correct json validation . I keep on playing with various characters as Marc B suggested above. And fortunately I found I was getting trouble with the German chracters like ä , ö , ü . So I did str_replace for ä to "ae" (which are same) before saving to database , then the problem solved for me. But I know this is not the optimal solution but it worked. could it be the json_encode() error , that it is unable to encode those characters ? – Nepal12 Feb 28 '14 at 22:54
  • @Nepal12 see this stackoverflow with solution http://stackoverflow.com/questions/13602075/problems-with-german-umlauts-in-php-json-encode – Richard Chambers Feb 28 '14 at 23:02
  • Also take a look at this stackoverflow which provides a php function to do json enconding using strings http://stackoverflow.com/questions/15180155/phps-json-encode-and-character-representation – Richard Chambers Feb 28 '14 at 23:06
  • @RichardChambers: Thanks a lot. I am checking them. – Nepal12 Mar 01 '14 at 11:59

0 Answers0