Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development


Thank you for visiting the BlackBerry Support Community Forums.

BlackBerry will be closing the BlackBerry Support Community Forums Device Forums on April 1st (Developers, see below)

BlackBerry remains committed to providing excellent customer support to our customers. We are delighted to direct you to the CrackBerry Forums, a well-established and thorough support channel, for continued BlackBerry support. Please visit http://forums.crackberry.com or http://crackberry.com/ask. You can also continue to visit BlackBerry Support or the BlackBerry Knowledge Base for official support options available for your BlackBerry Smartphone.

"When we launched CrackBerry.com 10 years ago, we set out to make it a fun and useful destination where BlackBerry Smartphone owners could share their excitement and learn to unleash the full potential of their BlackBerry. A decade later, the CrackBerry community is as active and passionate as ever and I know our knowledgeable members and volunteers will be excited to welcome and assist more BlackBerry owners with their questions."

- Kevin Michaluk, Founder, CrackBerry.com

Developers, for more information about the BlackBerry Developer Community please review Join the Conversation on the BlackBerry Developer Community Forums found on Inside BlackBerry.


Reply
Contributor
Posts: 28
Registered: ‎11-13-2012
My Device: none
My Carrier: 27768965251
Accepted Solution

JSON Results Giving Error

[ Edited ]

I'm currently getting json data from my web service, This part is a success, it's working perfectly.

Secondly, I run this code to process the JSON.

 

JSONArray arr = new JSONArray(jsonList);
for (int i=0, len1 = arr.length(); i < len1; i++) {
   JSONObject obj = arr.getJSONObject(i);
   RichTextField showJson = new RichTextField("Username: " + obj.getString("Username"));
   add(showJson);
   add(new RichTextField("--------------"));
}

 

But I get an error

error104.gif

 

But when I comment out:

JSONObject obj = arr.getJSONObject(i);
RichTextField showJson = new RichTextField("Username: " + obj.getString("Username"));

 

Then it works perfectly by going through each record in the JSON.

But then it obviously only displays this

add(new RichTextField("--------------"));

4 times, as many as there are in the JSON

 

Does anyone perhaps know why, i'm getting the ERROR, when I display the JSON records.

I got the code from the thread, where they state that it works.

http://supportforums.blackberry.com/t5/Java-Development/Parse-JSON/td-p/1314943

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: JSON Results Giving Error

Code looks fine to me.

 

Run this in debug mode.  Then when it breaks, it will stop at the error and you look at the stack trace to find out exactly which statement caused the problem.  Let us know.

 

Also how have you got JSON support in this?  Is this the built in JSON?

Developer
Posts: 17,025
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: JSON Results Giving Error

json parsing should not be done on the event thread, btw.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Contributor
Posts: 28
Registered: ‎11-13-2012
My Device: none
My Carrier: 27768965251

Re: JSON Results Giving Error

Yeah the built in one.

 

But I got it working! whoo hoo!

I changes this code:

RichTextField showJson = new RichTextField("Username: " + obj.getString("Username"));

to:

StringBuffer buildBubbles = new StringBuffer().append(obj.getString("Username"));

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: JSON Results Giving Error

OK, can you mark this as solved?  Instructions in the Help link near the top of the screen if you need them. 

 

Just repeating Simon's point, you need to make sure that you download and parse the JSON from a Background Thread, i.e. a Thread that you create and then

<thread>.start();

 

This of course causes a problem - what does the user do while waiting for this download?  Search the forum for answers to this, look for things like progressbar or 'PleaseWait', or loading.