I am brand new to the Stack Exchange API and Android (well, self taught in the last four months). I have been working on creating a school project focused around Android and JSON. My initial idea was to create an app for Stack Exchange but many have been there, done that and it turned out to be far too complicated for my current programming skills.
I finally have a simple working app to explain and demonstrate JSON parsing with Android for my project using the Stack Exchange API, but I just encountered an error I don't know how to handle.
I trimmed my project down drastically to create an app that shows the top ten users on Stack Overflow and displays their name, age and location. I know this app isn't useful for much, but it is just a demo of parsing the JSON in Android (and also gzip the JSON which I didn't know until I got outside help).
Using the documentation usage help I formed a query to find only the top ten users of Stack Overflow with their
location listed. However 8 of the 10 entries do not contain the key-value pairs for age and/or location so now my app is not working and returns
Could not fetch data from server. Eclipse LogCat showed me the
org.json.JSONException error that there is
No value for age.
Now, I know I have to research how to handle this exception but I was wondering if anyone had a easy to understand answer on how to handle this exception.
I am using
- Windows 7
- Eclipse Indigo Service Release 2
- Android SDK 22.6.2 (hate it because I don't know how to use it as well as the SDK I started with)
running on a Galaxy Tab S3 with Android 4.1.2.
Thanks for any tips!
Update: I resolved this by putting if/else statements with the variables that were missing and setting the values to 0 or null if the key-value pair was missing.
This is the URL I used to return Stack Overflow's top twenty users sorted by reputation and listing only their name, age and location: