Issues with Processing2.0b8, Android mode, JSON
in
Android Processing
•
4 months ago
Just wanted to document an issue and fix I discovered today with Android and JSON parsing.
It seems the processing version of JSONObject library does not play nicely with Android, and worst of all it overwrites the usage of the JSONObject class. If you try to import the built in Android JSON library (as far as I understand it to be), org.json.* or org.json.JSONObject then an error is thrown with 2 versions of JSONObject. If you rely on just the processing.data.JSONObject then it crashes Android. This gave me hours of headaches, until I realized that I could prefix the JSONObject with org.json so that it forces the usage of the Android version of JSONObject. I had to change my code in order for it to work, but in the end it is all working.
I was trying this:
JSONObject json = new JSONObject(result);
^ This yielded an error that JSONObject in processing.data.JSONObject is protected access. This is troublesome because that syntax is suggested in all Java forums.
In order to compile, I changed it to this:
JSONObject json = JSONObject.parse(result);
^ This would compile, but throw an error in Android Logcat saying something about processing.data.JSONObject not existing.
In the end I did this:
org.json.JSONObject = new org.json.JSONObject(result);
^ This works.
I'm not sure where the fundamental issues are arising, Processing version, Android version, etc. But I wanted to document it in case someone else got stuck by this.