I am trying to sync messages of android device to server using Java script object notation, i am able to sync all of the columns e.g. address, date, status, read, type except message body, it gives Java script object notation syntax error. i have gone through many links, they are saying that there are some sort of characters/hidden characters, i tried their solutions but doesn't helped at all, i tried some of these:
json_decode returns JSON_ERROR_SYNTAX but online formatter says the JSON is OK
json_decode syntax error from valid json
I am trying to encode android sms message body using JSON and send over the php webservice, but on php end it was not interpreted as JSON because of hidden chracters inside the string of message body. Adding this one line of code to replace hidden characters in string on Android end solves the problem, here's the solution: body.replaceAll("\p{C}", "?");
Related
I'm working on a careers listing page for our company. We are using an API to retrieve the information from our HR software provider. The JSON appears to be invalid. I'm using the below to test it.
<?php
//Testing if ADPs json is valid
$json = json_decode($jsondata);
if (json_last_error() === JSON_ERROR_NONE) {
// $json contains a valid json string. It's ready to use.
print_r($jobdata);
} else {
// oops, it's not valid JSON.
echo '<h2>'.'We\'re sorry. We are unable to list jobs right now. Please contact'.' careers#domain.com'.'</h2>';
}
?>
Is there a way I can parse the invalid JSON? It appears that there is an unexpected bracket somewhere.
json_decode is failing for a reason. Do you really want to deserialize malformed data and further base your logic on such data?
Again, before manually decoding anything check the following:
Is the server returning data character-encoded in an encoding not expected by your application. Especially if your backend is running on an linux-based server and the 3rd party API on windows one
Is the JSON not further encoded by your application internal logic. A good try would be to check for encoded html entities and/or decode them with html_entity_decode()
From my experience - if such problems persist, try to use an wrapper which automates previously mentioned steps for you (and translated semi-json expressions, like mongodb queries, javascript expressions):
https://github.com/zendframework/zend-json
I have a program which does this:
Android app, in Java, receives input(in this case, emojis, UTF8)
This String is sent to a PHP script via POST
The PHP converts all emojis into question marks (?)
I can tell, using Android Logcat, that the java is not ruining it, it is definitely the PHP
The PHP is configured properly, because when I run a test webpage without a post request, which just echoes some emojis (the exact same ones) it works fine
I think your best solution is to use Emoji for PHP. It handles all of the conversions for you. Grab a copy at:
http://code.iamcal.com/php/emoji/
Here is a code example:
<?php
include('emoji.php');
# browser sniffing tells us that a docomo phone
# submitted this text
$clean_text = emoji_docomo_to_unified($_POST[message]);
...
# now we want to show it in a desktop browser
$html = emoji_unified_to_html($clean_text);
?>
We connect android with php server. Then we send and retrieve data using HTTPClient. HTTPPost specifies that our request method is post. The response is stored in HTTPResponse. ‘URL’ is the actual link where JSON is present.
Then we use inputstream to get data into bytes. We need to convert byte-stream to character stream. After that we build String with the help of StringBuilder-from:
http://techlovejump.com/connect-android-with-php-mysql-and-json/
I creating an app in which I'm using php in server side.When an Ajax call is made its calling an php to retrieve the data from the table but the issue is some table contains html & special characters tags so i get an error in the Ajax :
05-31 00:59:07.670: D/CordovaLog(872): Server is not responding... Please try again: SyntaxError: Unexpected token <
05-31 00:59:07.670: I/chromium(872): [INFO:CONSOLE(342)] "Server is not responding... Please try again: SyntaxError: Unexpected token <", source: file:///android_asset/www/home.html (342)
can any tell how to solve this issue.
Thanks in advance.
you may use escapeHtml which Returns an HTML escaped representation of the given plain text.
For more info see at escapeHtml
Or you may use TextUtils.htmlEncode("your json tag") to encode html.
Edit
As you are using phonegap so you may use htmlencoding is PHP also as
htmlentities($str, ENT_QUOTES);
For more information see at htmlentities in PHP
To encode html in php and decode back in your app,you may refer this link
Using PHP’s JSON encode and decode functions to handle data sent to and from your app
I was created JSON array from json_encode using PHP. I gave my array to json_encode . It's created JSON array very well. And i feed this JSON array to my android apps. When i am gonna read this url at android it's return following EXCEPTION ERROR.
Error parsing data org.json.JSONException: Value  of type java.lang.String cannot be converted to JSONObject
But when i am gonna create jason object at android adding this following line
jObj=new JSONObject(json.substring(json.indexOf("{"), json.lastIndexOf("}") + 1));
It's working perfectly.
But i don't want to need the my second type solution. I need the php solution when i am put my json_encode on php.
And also at IOS the JSON return NULL values. How can i fix in both IOS and Android
Thanks advance
The java.text.Normalizer is intended to do exactly this: remove unwanted unicode characters.
The normalize method will allow you to pass your CharSequence and return the "normalized", ASCII-only String.
I have a SOAP service I am calling with PHP 5.3.1's builtin SoapClient. The first operation I must perform on the service is a custom authentication operation, and one of the required parameters I must pass is a 3DES encrypted string which I am creating using PHP's mcrypt, like so:
$encryptionKey = '1234myKey1234';
$currentFormattedDate = date ("Y/m/d H:i");
$encryptedString = mcrypt_encrypt('tripledes', $encryptionKey, $currentFormattedDate, 'ecb');
If I try to just pass $encryptedString as I get it from mcrypt_encrypt() I get a fatal error on my side and no call is made:
Fatal error: SOAP-ERROR: Encoding: string 'd\xe0...' is not a valid utf-8 string in /path/to/file
However if I utf8_encode() the string as such:
$encryptedString = utf8_encode($encryptedString)
Then the call is made but their webservice responds with the following error:
The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:argStatusDate. The InnerException message was 'There was an error deserializing the object of type System.String. The byte 0x19 is not valid at this location. Line 2, position 318.'.
This is the closest I can get to success with this process after having tried so many things that I'm back to square one. I have verified I can just pass a bogus string which results in the expected response of not being able to authenticate.
I don't think this should make any difference since I believe the SOAP call is ultimately made as utf8, but I have tried setting 'encoding' => 'ISO-8859-1' when constructing my SoapClient in PHP and I get the same error. The call is made but the server responds with the deserialization error.
Does anyone know a better way for me to treat this encrypted string that will please both my PHP client and their .Net webservice?
Maybe the problem is on their end?
FWIW, I can also request that we change the encryption method to "Rijndael AES Block Cypher" per their documentation. Not sure if that would result in an easier to handle string.
You probably need to encode the data in a base 64 encoded CDATA segment inside the opening and closing tags. You might want to ask the creater of the service for a sample, or - if it is a webservice - try to download the definition or even create a client through discovery. Note that the last link was found using Google search, I've been out of PHP for a while.
[EDIT] changing the cipher won't help for this, although anything is better than ECB encoding XML