0

I'm experiencing a strange problem, when I send a String from PHP to Java (Android).

The whole story is rather simple: A Java application sends a keyword to a PHP script. The PHP script looks it up in a DB and sends a JSON encoded array (as a string) back to the Java application.

I can see this string in a TextView field in Android and it looks like this: [{"name":"Berlin"}]

But Java does not accept this as a valid JSON string (unlike some online JSON validators), because it keeps throwing the exception: "A JSONArray text must start with '[' at character 1". When I compare this strings to an identical string hardcoded in Java using "equal()" it turns out, they just aren't equal. And even more disturbing: Java returns the length of the JSON string as 20, not as 19.

Why could this be? There are no unusual characters like Umlauts. What could be the 20th character? I'm suspecting some encoding problem, but I'm pretty sure everything (PHP file, Java file) is UTF-8 encoded.

(Before someone asks: I can provide code, but I don't know which part could be relevant.)

4
  • 1
    Have you checked for invisible characters like BOM ? Commented May 8, 2013 at 14:02
  • use java equivilent of trim() around the incoming json Commented May 8, 2013 at 14:02
  • 2
    Note it is 'Java' not 'JAVA'. No need to shout it from the rooftops. Commented May 8, 2013 at 14:04
  • Turns out Java has a built in trim() same as php String s = " Hello World ".trim(); Commented May 8, 2013 at 14:05

2 Answers 2

1

If the lengths are different there may be differences due to leading/trailing whitespace.

Try looking at this question. The accepted answer provides a way of pre-processing the strings to ensure this problem does not occour.

Sign up to request clarification or add additional context in comments.

1 Comment

I already tried using trim() without success. But nevertheless the pre-processing idea is something I should have a look at. Thank you!
0

Try

int ascii_code = jsonString.codePointAt(0)

and see what you get

1 Comment

That's it! It was actually the BOM, but I had no way of making it visible. I spent three days reading JSON-parsing tutorials and questions/answers but that wasn't the problem at all. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.