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
Highlighted
New Developer
Posts: 13
Registered: ‎07-11-2013
My Device: Curve 6
My Carrier: mtn
Accepted Solution

How to trim headers from JSON response in blackberry simulator

I am building an app that uses a JSON RESPONSE from a php page in order to load a listfield. The issue is that the response String contains html headers : ( !DOCTYPE.......HTML......).....this obviously is Not valid JSON and so can not be parsed. How can i rectify this?. I tried opening The php page from The simulator's Browser and got a valid JSON response . Please help me out .
Developer
Posts: 121
Registered: ‎05-31-2012
My Device: 9900
My Carrier: Airtel

Re: How to trim headers from JSON response in blackberry simulator

post ur code here so we can help.

Rishikesh Singh
Developer
Posts: 16,999
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: How to trim headers from JSON response in blackberry simulator

maybe you get an error message from the server. before processing you should check the responsecode to be 200 (http ok), otherwise handle the error (without attempting to parse json).
----------------------------------------------------------
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
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to trim headers from JSON response in blackberry simulator

Sorry, it has been a while since I wrote any php, but from memory, if you create your response correctly, it will not include the html.  The fact that the php is syrrounded by html is, I believe, something you should be able to change at the Server end. 

 

You should be able to test your php on a PC based Browser and do a "view source" to confirm that you are sending just JSON. 

New Developer
Posts: 13
Registered: ‎07-11-2013
My Device: Curve 6
My Carrier: mtn

Re: How to trim headers from JSON response in blackberry simulator

This is part of  the php code :

 

        

                            

<?php
                  $response=array();
                   include 'connect.php';
                   $cat=mysqli_real_escape_string($link,$_POST['category']);
                 ------------
                   array_push($response["items"],$item);
                   echo $json_encode($response);
                   ?>

 and this is the response:

 

 <!DOCTYPE  html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
       <head>
       <meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"/>
       <title>Untitled Document</title>
      </head>
      <body>
      </body>
      </html>
      {"items":[{"id":"1","item_name":"Sparkling Wine"}],"success":1}

 

I will like the response in this format:

{"items":[{"id":"1","item_name":"Sparkling Wine"}],"success":1}

 I do not know if i have to use the Jsoup library. Any help please?

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

Re: How to trim headers from JSON response in blackberry simulator

Sorry I won't be able to do any playing this week.  But if you look at your output, you can see that the html has been 'echo'ed before your echo.  So there is something else in your processing that is creating that response - it is being run before your code.  Search around, I suspect you wil find it.

New Developer
Posts: 13
Registered: ‎07-11-2013
My Device: Curve 6
My Carrier: mtn

Re: How to trim headers from JSON response in blackberry simulator

I resolved the issue....it was in the connect file that i was including which contained the connection parameters.