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

Reply
Contributor
tan_mel_viso
Posts: 10
Registered: ‎06-05-2012
My Device: 9900

How to Call Json Service in BlackBerry

i ahve a simple Json Service and now i want to parse the json service in my blackberry and fill the label with text

can anyone help me with that

my json data looks like

 

[

  • {
    • "Rate": 12,
    • "Type": " Bill",
    • "mission": "$150.00",
    • "Name": "Chase"
    }

]

 

 

can any give me sample code how i can parse this json data it is coming through wcf (.net) url

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: How to Call Json Service in BlackBerry

http://supportforums.blackberry.com/t5/Java-Development/Sample-Code-Implementing-JSON-in-your-applic...

just as a sidenote: if you enter 'json' in the search field you get this and numerous other helpful links.
----------------------------------------------------------
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
tan_mel_viso
Posts: 10
Registered: ‎06-05-2012
My Device: 9900

Re: How to Call Json Service in BlackBerry

[ Edited ]

NO Simon its not the solution

 it is actually converting all my data which i have selected into json form

 

 

but i already have a json data from a url say  www.simon/simonservice/getdata is returning me JSON data now i want to actually parse this data and further show in checkbox or in label feilds

 

 

 supoose my json data is

[

  • {
    • "Rate": 12,
    • "Type": " Bill",
    • "mission": "$150.00",
    • "Name": "Chase"
    }

]

 

 

 Now in my Blackbery display i want

 BasicEditField enterName = new BasicEditField("Rate: ", ""); <<<Show here Rate Price i.e 12

 

 BasicEditField enterName = new BasicEditField("Type: ", ""); <<<Show here Type  i.e Bill and so on

 

hope u got me

thanx

Developer
crispyoz
Posts: 223
Registered: ‎10-01-2011
My Device: Bold 9900

Re: How to Call Json Service in BlackBerry

There is nothing special about JSON on Blackberry,  your JSON code is funny though with those dots but anyway all my push stuff is done using JSON like so :

 

 

InputStream is = conn.openInputStream();
byte[] data = net.rim.device.api.io.IOUtilities.streamToBytes(is);
is.close();
String s = new String(data);
JSONObject jo =  new JSONObject(s);;

 

Now you have your JSONObject you treat it like any other JSONObject

 

LabelField = new LabelField(jo.getString("<key>") );

 

 

hth

 

Chris

 

 

 

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: How to Call Json Service in BlackBerry

take care not to execute your networking on the event thread, use a separate thread.
----------------------------------------------------------
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
Developer
crispyoz
Posts: 223
Registered: ‎10-01-2011
My Device: Bold 9900

Re: How to Call Json Service in BlackBerry

Peter is right,  The code sample is from the run method of  runnable class.

Contributor
tan_mel_viso
Posts: 10
Registered: ‎06-05-2012
My Device: 9900

Re: How to Call Json Service in BlackBerry

i m trying this but its not working whenever i run application as debus as

after reaching

conn = (HttpConnection)Connector.open(url); it says source not found

 

my code

please let me know if there is any error

 

public final class MyScreen extends MainScreen
{
    public MyScreen()
    {        
        // Set the displayed title of the screen       
        setTitle("MyTitle");
        String url="http://myjsonservice.com/jsondata";
        HttpConnection conn = null;
        try {
            conn = (HttpConnection)Connector.open(url);
            InputStream is = conn.openInputStream();
            byte[] data = net.rim.device.api.io.IOUtilities.streamToBytes(is);
            is.close();
            String s = new String(data);
            JSONObject jo =  new JSONObject(s);;
            
        } catch (Exception e) {
            System.out.print(e.toString());

        }
    }
    
    
}


Developer
crispyoz
Posts: 223
Registered: ‎10-01-2011
My Device: Bold 9900

Re: How to Call Json Service in BlackBerry

That's debugging on Blackberry for you :smileyhappy: 

 

Just put your break point  on String s =  and if it stops before that then just resume and it will stop at the breakpoint.

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: How to Call Json Service in BlackBerry

don't execute networking on the event thread, use a separate thread.
see
http://supportforums.blackberry.com/t5/Java-Development/What-is-the-Event-Thread/ta-p/446865
----------------------------------------------------------
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
Developer
crispyoz
Posts: 223
Registered: ‎10-01-2011
My Device: Bold 9900

Re: How to Call Json Service in BlackBerry

[ Edited ]

Yes that has been covered.  But for his test case it is not really an issue becasue he is just trying to understand how to use JSON.   The difference between debugging in Blackberry and debugging say a windows app is that when debugging windows when you step into or over, you are dealing with only visible code in the current thread. When debugging blackberry the debugger will drag you out to the event processing code which is not what you meant to step to, it's very confusing for a newbie. Hence my advice regarding the position of the break point.